编程语言
首页 > 编程语言> > javascript – 从节点jstree返回路径

javascript – 从节点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

我错过了什么?

UPDATE
@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,"/");
}
                         .
                         .
                         .

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

解决方法:

您正在使用该功能错误.你必须像这样使用它:

$(\'#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,"/");
 }

希望有所帮助.

标签:jstree,javascript,php,json
来源: https://codeday.me/bug/20190725/1530669.html