【发布时间】:2014-05-21 20:32:50
【问题描述】:
我正在尝试返回 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,"/");
}
.
.
.
我希望这可以帮助别人,比如帮助我!
【问题讨论】:
标签: javascript php json jstree