JsTree复选框选择具有相应父级的所有字段

问题描述 投票:0回答:1

我正在使用JsTree进行过滤选项。

现在,在选择父节点时,它将所有子节点和子子节点放在一个数组中。

所以我无法确定哪个孩子属于哪个父母。

这是我的示例代码,

$(function () {
    $("#tree").jstree({
        "checkbox": {
            "keep_selected_style": false
        },
            "plugins": ["checkbox"],
            'core' : {
                'data' :  [
  {
    "id": "ALL",
    "text": "ALL",
    "children": [

      {
        "id": "TWO",
        "text": "TWO",
        "children": [
          {
            "id": "C2",
            "text": "C2",
            "children": [
              {
                "id": "S2",
                "text": "S2"
              }
            ]
          }
        ]
      }
    ]
  }
]
            }
    });

    var selected_items = {}

    $("#tree").bind("select_node.jstree", function (event, data) { 
               console.log(data.node);
                         });
});

它给孩子们,

[
  "S2",
  "C2",
  "S3",
  "T3",
  "C3",
  "D3",
  "ONE",
  "TWO",
  "THREE"
]

那么如何识别孩子和他们的父母。

javascript html jstree
1个回答
0
投票

您可以使用选定的nodeId从树中返回节点,为您提供所有子关系和父关系

$("#tree").bind("select_node.jstree", function (event, data) {    
        var jsonOfSelectedNode = $('#tree').jstree(true).get_json(data.node, {flat:true})
        // debug here and view 
});

jsonOfSelectedNode将为您提供所有必需的父子数据

© www.soinside.com 2019 - 2024. All rights reserved.