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
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);
}