【问题标题】:get Selected Value in JavaScript from ztree从 ztree 获取 JavaScript 中的选定值
【发布时间】:2014-10-16 07:34:27
【问题描述】:

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

<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>

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

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

【问题讨论】:

    标签: javascript ztree


    【解决方案1】:

    在 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
    };
    

    【讨论】:

    • "onClick" 而不是 onCheck 对我有用,参数相同。
    【解决方案2】:

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

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

    然后从“节点”你可以得到必要的数据。

    【讨论】:

    • 这个答案如果能解释代码会更好。
    • 虽然我找到了替代方案,但如果你能以更详细的方式解释这一点,我会更喜欢在我的应用程序中使用它。谢谢!
    • @ammarali29 我做了一些改变。
    猜你喜欢
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2020-02-19
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多