从ztree获取JavaScript中的选定值

时间:2014-10-16 07:34:28

标签: javascript ztree

我有一棵树,我从zTree中取样,但我无法从这棵树中获取所选值。虽然我已经对原始代码进行了更改,但在这里我发布了官方Ztree演示的原始代码。任何帮助!!

<SCRIPT type="text/javascript">
    var setting = {
        data: {
            simpleData: {
                enable: true
            }
        }
    };

存储树节点的数组

    var zNodes =[
        { id:1, pId:0, name:"pNode 1", open:true},
        { id:11, pId:1, name:"pNode 11"},
        { id:111, pId:11, name:"leaf node 111"},
        { id:12, pId:1, name:"pNode 12"},
        { id:121, pId:12, name:"leaf node 121"},
        { id:13, pId:1, name:"pNode 13 - no child", isParent:true},
        { id:2, pId:0, name:"pNode 2"},
        { id:21, pId:2, name:"pNode 21", open:true},
        { id:211, pId:21, name:"leaf node 211"},
        { id:22, pId:2, name:"pNode 22"},
        { id:221, pId:22, name:"leaf node 221"},
        { id:23, pId:2, name:"pNode 23"},
        { id:231, pId:23, name:"leaf node 231"},
        { id:3, pId:0, name:"pNode 3 - no child", isParent:true}
    ];

从该数组初始化树

    $(document).ready(function(){
        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    });
</SCRIPT>

这里Tree显示在div中,id为treeDemo。在这里,我想从此树中提醒警报框中的选定值。

<BODY>
     <div class="content_wrap">
          <div class="zTreeDemoBackground left">
               <ul id="treeDemo" class="ztree"></ul>
          </div>
     </div>
</BODY>

2 个答案:

答案 0 :(得分:2)

在zTree v3.x上, 你需要两件事:

1)在设置中,添加回调

var setting = {
        ...
        callback: {
            onCheck: zTreeOnCheck
        }
    };

2)回调中的ztreeOnCheck,每次检查/取消选中所有选定的节点。样品

function zTreeOnCheck(event, treeId, treeNode) {
  var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
  var nodes = treeObj.getCheckedNodes(true);
  console.log(nodes); //Here are all of the selected nodes
};

答案 1 :(得分:0)

获取zTree中所选节点的JSON数据对象集合。 请使用zTree对象执行该方法。

var treeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes); var nodes = treeObj.getSelectedNodes();

然后从&#34;节点&#34;你可以获得必要的数据。