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);
          }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计