doubijiao2094 2012-09-24 23:29
浏览 38
已采纳

EXT JS - 使用PHP将目录树结构导入JSON对象

I am using EXT JS for a simple tree that will show a single directory and its contents. For whatever reason I have gone through all of their examples and am not able to replicate what I want. I want to create a JSON object based on the directory structure so it can be fed into the EXT JS Tree.

Here is my script that opens up the directory, creates the parent node then attempts to create the child nodes. Inside this directory are just .xml files. I have gotten it to work with opening the directory and just creating nodes for the files but it doesnt show the parent or root level and loses the nifty collapsing affect.

Here is my code:

if($handler = opendir($dir."/$market_desc"))
          {

          while (($sub = readdir($handler)) !== FALSE)
          {
              if ($sub !== "." && $sub !== ".." && $sub !== ".svn")
              {
                  if(is_file($dir."/$market_desc/".$sub))
                  {
                      $subDir[] = array(
                       'text' => $sub,
                       'id'   => $sub,
                       'checked' => false,
                       'leaf' => true,
                       'cls'  => 'file'
                           );
                  }
              }

          }

           $listDir[] = array(
           'text' => $market_desc,
           'id'   => $market_desc,
           'checked' => false,
           'cls'  => 'folder',
           'children' => array($subDir)
        );


          closedir($handler);
          unset($handler);

The directory structure only goes 1 level deep with some files at the root level followed by a number of directories that also contain files but never going more than 1 level down.

I am a php back-end developer by trade so I apologize for my noobness when it comes to javascript and the correct formation of this JSON needed by EXT

  • 写回答

1条回答 默认 最新

  • dqeznd1697 2012-09-25 16:49
    关注

    After some trial and error I have this PHP script in the correct format for EXT JS. It will open the directory and build the nested array which can the be json_encoded and sent to EXT JS Tree for rendering

              // Get market specific features and make nodes
          if($handler = opendir($dir."/$market_desc"))
          {
              while (($sub = readdir($handler)) !== FALSE)
              {
                  if ($sub !== "." && $sub !== ".." && $sub !== ".svn")
                  {
                      if(is_file($dir."/$market_desc/".$sub))
                      {
                                    $subDir[] = array(
                                    'text' => $sub,
                                    'id'   => $sub,
                                    'checked' => false,
                                    'leaf' => true,
                                    'parent' => $market_desc,
                                    'children' => NULL,
                                    'cls'  => 'file'
                                    );
                      }
    
                  }
    
              }
                              $listDir[] = array(
                              'text' => $market_desc,
                              'id'   => $market_desc,
                              'checked' => false,
                              'cls'  => 'folder',
                              'parent' => NULL,
                              'expanded' => true,
                              'children' => $subDir
                                  );
    
    
              closedir($handler);
              unset($handler);
          }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来