jsTree :如何从 jstree 获取所有节点

jsTree : How to get all nodes from jstree?

本文关键字:获取 节点 jstree jsTree      更新时间:2023-09-26

如何获取jsTree中存在的所有节点?

我正在使用xml构建jsTree

Root
     -----A
          -----A1
               -----A1.1
               -----A1.2
          -----A2
               -----`A2.1`
               -----A2.2
     -----B
          -----B1
          -----B2
     -----C
          -----C1
               -----C1.1
               -----C2.2

我希望jsTree中存在的所有节点(ID)的数组如下

预期产出:[根、A、A1、A1.1、A1.2、A2、

A2.1、A2.2、B、B1、B2、C、C1、C1.1、C2.2]

来自文档:

.get_json ( node , li_attr , a_attr )

此函数返回转换回 JSON 的树节点数组。

有关此文档中相同函数的更多信息:

此函数遍历整个树并将其导出为 JSON。指 执行"数据源"部分以查看输出的格式。

如果将节点指定为第一个参数,则仅该节点及其 子项包含在导出中,否则整个树为 出口。

只要搜索一下,你就会找到! :)

您可以遍历每个节点元素并将其 id 放在数组中:

var idList = [];
var jsonNodes = $('#tree').jstree(true).get_json('#', { flat: true });
$.each(jsonNodes, function (i, val) {
    idList.push($(val).attr('id'));
})

带有示例的解决方案:)

var xmlString = $("#tree").jstree("get_xml");   
    var xmlDOM = $.parseXML(xmlString);
    var IDList =[];
var items = $(xmlDOM).find('root item');
$.each (items, function(key, val){
    IDList.push($(val).attr('id'));
})
IDList.pop();

xml字符串 =

<root>  
        <item id="A" parent_id="0" state="close">  
            <content><name>Charles Madigen</name></content>                
        </item>
        <item id="A1" parent_id="A" state="close">  
            <content><name>Charles Madigen</name></content>                
        </item>
          .
          .
</root>
输出: 根, A, A1, A1.1, A1.2, A2, A2.1,

A2.2, B, B1, B2, C, C1, C1.1, C2.2

:)

var treeData = $('#MyTree').jstree(true).get_json('#', {flat:false})
// set flat:true to get all nodes in 1-level json
var jsonData = JSON.stringify(treeData );

我需要同样的东西,并提出了以下解决方案,因为 jstree3 中不再提供get_xml

function get_jstree_order(root_ul_selector, children) {
    var output = [];
    var _this = this;
    if (typeof children === 'undefined') {
        children = $(root_ul_selector).find('> li');
    }
    children.each(function() {
        if ($(this).find('ul').length > 0) {
            output.push({
                id: $(this).attr('id'),
                children: get_jstree_order(root_ul_selector, $(this).find('ul > li'))
            });
            return;
        }
        output.push({
            id: $(this).attr('id'),
            children: false
        })
    });
    return output;
}
console.log(get_jstree_order('#mytree > ul'));

输出(转换为 JSON 以提高可读性):

[
  {
    "id": "1",
    "children": false
  },
  {
    "id": "2",
    "children": false
  },
  {
    "id": "5",
    "children": [
      {
        "id": "6",
        "children": false
      },
      {
        "id": "7",
        "children": false
      }
    ]
  },
  {
    "id": "8",
    "children": false
  },
  {
    "id": "9",
    "children": false
  },
  {
    "id": "10",
    "children": false
  },
  {
    "id": "11",
    "children": false
  }
]

根据需要进行修改;以包括需要的内容,但我的目的只是为服务器端处理获取正确的项目顺序。

当延迟加载时,如果子 id 独立于其父母,这很好(例如,父母的第一个孩子总是从 1 开始)