• ZTree节点的常用操作


    在以前项目改造过程中涉及到ZTree的简单应用,在接下来的项目中有个业务模块涉及到ZTree节点的操作,包含新增,删除,修改,留代码备忘。

    首先是ztree的配置信息

       var setting = {
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                edit: {
                    enable: true,
                    showRemoveBtn: false,
                    showRenameBtn: false
                }
            };

    新增方法

     var newCount = 1;
     function addNode(e) {
                var zTree = $.fn.zTree.getZTreeObj("groupTree");
                if (!zTree) {
                    var zNodes = [];
                    zTree = $.fn.zTree.init($("#groupTree"), setting, zNodes);
                }
                var isParent = e.data.isParent,
                nodes = zTree.getSelectedNodes(),
                treeNode = nodes[0];
                if (treeNode) {
                    treeNode = zTree.addNodes(treeNode, { id: (100 + newCount), pId: treeNode.id, isParent: isParent, name: "new node" + (newCount++) });
                } else {
                    treeNode = zTree.addNodes(null, { id: (100 + newCount), pId: 0, isParent: isParent, name: "new node" + (newCount++) });
                }
            
            };

    删除方法

     function removeNode(e) {
                var zTree = $.fn.zTree.getZTreeObj("groupTree"),
                nodes = zTree.getSelectedNodes(),
                treeNode = nodes[0];
                if (nodes.length == 0) {
                    alert("请先选择一个节点");
                    return;
                }
                zTree.removeNode(treeNode, false);
            };

    修改方法

     function UpdateNode(stext) {
                var zTree = $.fn.zTree.getZTreeObj("groupTree"),
                nodes = zTree.getSelectedNodes(),
                treeNode = nodes[0];
                if (nodes.length == 0) {
                    alert("请先选择一个节点");
                    return;
                } else {
                    treeNode.id = stext;
                    treeNode.name = stext;
                    zTree.updateNode(treeNode, false);
                }
            }

    页面加载完成绑定事件

      window.onload = function () { 
                $("#addParent").bind("click", { isParent: true }, addNode);
                $("#addLeaf").bind("click", { isParent: false }, addNode);
                $("#removeNode").bind("click", removeNode);
            }
  • 相关阅读:
    上一篇、下一篇之实现思路
    sql优化
    简易的文件上传 tp5
    phpqrcode生成二维码
    php 发送邮件(2)qq邮箱开通
    php 发送邮件(1)代码逻辑
    SELECT list is not in GROUP BY clause and contains nonaggregated
    TP5 未定义变量:XXX
    CentOS 7下载
    FFT算法实现——基于GPU的基2快速傅里叶变换
  • 原文地址:https://www.cnblogs.com/jingsha/p/5756962.html
Copyright © 2020-2023  润新知