从节点jstree返回路径

时间:2014-05-21 20:32:50

标签: javascript php json jstree

我正在尝试返回jstree中所选节点的路径。我需要节点的整个路径。

我有一个生成json的php文件,如下所示:

header("Content-Type: application/json; charset=utf8");
echo json_encode(dir_to_jstree_array("/my_path"));

function dir_to_jstree_array($dir, $order = "a", $ext = array()) {      
   if(empty($ext)) {
      $ext = array ("jpg", "gif", "jpeg", "png", "doc", "xls", "pdf", "tif", "ico", "xcf", "gif87", "scr" );
   }

  $listDir = array(
            'text' => basename($dir),
            'icon' => 'jstree-folder',
            'children' => array()
  );

  $files = array();
  $dirs = array();

  if($handler = opendir($dir)) {
     while (($sub = readdir($handler)) !== FALSE) {
        if ($sub != "." && $sub != "..") { 
           if(is_file($dir."/".$sub)) {
              $extension = trim(pathinfo($dir."/".$sub, PATHINFO_EXTENSION));
              $files []= $sub;
           } 
           elseif (is_dir($dir."/".$sub)) {
              $dirs []= $dir."/".$sub;
           }
        }
     }
     if($order === "a") {
       asort($dirs);
     } 
     else {
        arsort($dirs);
     }

     foreach($dirs as $d) {
        $listDir['icon'] = 'jstree-file';
        $listDir['children'][]= dir_to_jstree_array($d);
     }

     if($order === "a") {
        asort($files);
     } 
     else {
        arsort($files);
     }

     foreach($files as $file) {
        $listDir['icon'] = 'jstree-file';
        $listDir['children'][]= $file;
     }

     closedir($handler);
  }
  return $listDir;
}

我加载javascript的php函数是:

function load_js() {
  echo ' <link rel="stylesheet" type="text/css" href="/css/jstree/dist/themes/default/style.min.css" />
         <script type="text/javascript" src="/js/jquery.js"></script>
     <script type="text/javascript" src="/js/jstree/dist/jstree.min.js"></script>


     <script type="text/javascript">

       function on_load_padrao() {
          $(\'#jstree_demo_div\').jstree({
                \'core\' : {                  
                    \'data\' : {
                        \'type\' : "POST",
                        \'url\' : \'mypath/dir2json.php\',
                        \'data\' : function (node) {
                            return { \'id\' : node["id"]};
                         }
                     },
                     \'dataType\' : \'json\'
                 },
                \'plugins\' : ["checkbox" ]
          })
           .on("changed.jstree", function (e, data) {
           console.log(data.selected.get_path());
           });

       </script> ';
}

我尝试使用我在文档中看到的函数获取路径: get_path(),但这不起作用。当我调试这个时,我收到以下错误:

ReferenceError:未定义get_path

我错过了什么?

更新 @oerl告诉我,我使用的函数是错的,所以这是正确的方法:

                        .
                        .
                        .
$(\'#jstree_demo_div\').jstree(true).get_path(data.node, "/")
$(\'#jstree_demo_div\').jstree(true).get_path(data.node,"/");

var selectedNodes = $(\'#jstree_demo_div\').jstree(true).get_selected();

for(var node in selectedNodes) {
   var path = $(\'#jstree_demo_div\').jstree(true).get_path(data.node,"/");
}
                         .
                         .
                         .

我希望这可以帮助别人,就像帮助我一样!

2 个答案:

答案 0 :(得分:6)

您使用的功能错误。你必须像这样使用它:

$(\'#jstree_demo_div\').jstree(true).get_path(data.node,"/");

此外,您还必须使用所选节点:

var selectedNodes = $(\'#jstree_demo_div\').jstree(true).get_selected();

最后回答你的问题迭代你的selectedNodes并在每个节点上调用get_path:

 for(var node in selectedNodes) {
   var path = $(\'#jstree_demo_div\').jstree(true).get_path(node,"/");
 }

希望有所帮助。

答案 1 :(得分:2)

这是一个经过测试的实际例子,我使用帖子和评论中的信息,因为似乎没有在帖子中指定完整的解决方案。我正在jsTree期望它的libs文件夹下使用jQuery v1.11.0作为jsTree。 pyCharm已经检查了代码语法。

var file_data = [];
// object that represents the DIV for the jsTree
var objTreeView = $("#div_treeview");
// returns a list of <li> ID's that have been sected by the user
var selectedNodes = objTreeView.jstree(true).get_selected();
// This is the best way to loop object in javascript
for (var i = 0; i < selectedNodes.length; i++) {
  // get the actual node object from the <li> ID
  var full_node = objTreeView.jstree(true).get_node(selectedNodes[i]);
  // Get the full path of the selected node and put it into an array
  file_data[i] = objTreeView.jstree(true).get_path(full_node, "/");
}
// Convert the array to a JSON string so that we can pass the data back to the server 
// once the user submits the form data
alert(JSON.stringify(file_data));