How to get path of children when parent is selected in jstree

梦想与她 提交于 2019-12-24 06:35:55

问题


I'm trying to get the path of the parent and children nodes when the parent is selected. To get the path of the node selected I did the following:

 $("#jstree_demo_div").jstree({
    "core" : {
       "data" : {
          "type" : "POST",
          "url" : "myurl.php",
          "data" : function (node) {
                  return { "id" : node.id };
              }
       },
       "dataType" : "json",
    },
    "plugins" : [ "wholerow", "checkbox", "types" ]
 })
 .on("changed.jstree", function (e, data) {
    var path = $("#jstree_demo_div").jstree(true).get_path(data.node,"/");
    console.log ("path = " + path + "\n");
 });""

But I don't know how to get the path the children's parent.

I tried to get the id's of children's parent and then apply the split()function to get the id's and then apply the get_path() but this is not working, because it said that split is not a function.

var test = data.node.children;
var spt_tst = test.split(',');
alert("node = " + spt_tst[0]);

What am I doing wrong?

**** EDIT ****

My tree is like in the image:

What I want to do is: select the folder images and the get the path of all the content, for example, I want to get the path of the things that are inside .xvpics folder and the others too. Because I will have to send these paths to an .txt file.


回答1:


data.node.childrenreturns an array, which you can access by index. To get the i-th element use data.node.children[i]. For example, to iterate over all children of data.nodedo this:

for (var i = 0; i < data.node.children.length; i++) {
  //for example call get_path of the child
  var path = $(\'#jstree_demo_div\').jstree(true).get_path(data.node.children[i],"/");
}

I hope I understood you right and that is what you are looking for.

EDIT: You can use children_d to get all children of the node. Even the children of the children.

for (var i = 0; i < data.node.children_d.length; i++) {
  //for example call get_path of the child
  var path = $(\'#jstree_demo_div\').jstree(true).get_path(data.node.children_d[i],"/");
}

Hope I got you right this time. Just let me know if not.



来源:https://stackoverflow.com/questions/23829903/how-to-get-path-of-children-when-parent-is-selected-in-jstree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!