ztree-可拖拽可编辑的树

<!DOCTYPE html>
<HTML>
<HEAD>
    <TITLE> ZTREE DEMO - addNodes / editName / removeNode / removeChildNodes</TITLE>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="../../../css/demo.css" type="text/css">
    <link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
    <script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="../../../js/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="../../../js/jquery.ztree.excheck.js"></script>
    <script type="text/javascript" src="../../../js/jquery.ztree.exedit.js"></script>
    <SCRIPT type="text/javascript">
        var setting = {
            view: {
                selectedMulti: false
            },
            edit: {
                drag: {
                    autoExpandTrigger: true,
                    prev: dropPrev,
                    inner: dropInner,
                    next: dropNext
                },
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false
            },
            data: {
                keep: {
                    parent:true,
                    leaf:true
                },
                simpleData: {
                    enable: true
                }
            },
            callback: {
                beforeDrag: beforeDrag,
                beforeRemove: beforeRemove,
                beforeRename: beforeRename,
                onRemove: onRemove,
                beforeDrop: beforeDrop,
                beforeDragOpen: beforeDragOpen,
                onDrag: onDrag,
                onDrop: onDrop,
                onExpand: onExpand
            }
        };        

        var zNodes =[
            { id:1, pId:0, name:"节点1", open:false},
            { id:11, pId:1, name:"节点1-1"},
            { id:12, pId:1, name:"节点1-2"},
            { id:121, pId:12, name:"节点1-2-1"},
            { id:122, pId:12, name:"节点1-2-2"},
            { id:123, pId:12, name:"节点1-2-3"},
            { id:3, pId:0, name:"节点3", open:false},
            { id:31, pId:3, name:"节点3-1"},
            { id:32, pId:3, name:"节点3-2"},
            { id:33, pId:3, name:"节点3-3"}
        ];

        var log, className = "dark";
        function beforeDrag(treeId, treeNodes) {
            return false;
        }
        function beforeRemove(treeId, treeNode) {
            className = (className === "dark" ? "":"dark");
            showLog("[ "+getTime()+" beforeRemove ] " + treeNode.name);
            console.log()
            var children = treeNode.children;
            if(children!=undefined && children.length>0){
                alert("请先删除子节点");
                return false;
            }
            return confirm("确认删除 节点 -- " + treeNode.name + " 吗?");
        }
        function onRemove(e, treeId, treeNode) {
            showLog("[ "+getTime()+" onRemove ] " + treeNode.name);
        }
        function beforeRename(treeId, treeNode, newName) {
            if (newName.length == 0) {
                alert("节点名称不能为空.");
                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                setTimeout(function(){zTree.editName(treeNode)}, 10);
                return false;
            }
            return true;
        }
        function showLog(str) {
            if (!log) log = $("#log");
            log.append("<li class=‘"+className+"‘>"+str+"</li>");
            if(log.children("li").length > 8) {
                log.get(0).removeChild(log.children("li")[0]);
            }
        }
        function getTime() {
            var now= new Date(),
            h=now.getHours(),
            m=now.getMinutes(),
            s=now.getSeconds(),
            ms=now.getMilliseconds();
            return (h+":"+m+":"+s+ " " +ms);
        }

        var newCount = 1;
        function add(e) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            isParent = e.data.isParent,
            nodes = zTree.getSelectedNodes(),
            treeNode = nodes[0];
            if (treeNode) {alert(1)
                treeNode = zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, isParent:isParent, name:"new node" + (newCount++)});
            } else {alert(2)
                treeNode = zTree.addNodes(null, {id:(100 + newCount), pId:0, isParent:isParent, name:"new node" + (newCount++)});
            }
            if (treeNode) {alert(3)
                zTree.editName(treeNode[0]);
            } else {alert(4)
                alert("叶子节点被锁定,无法增加子节点");
            }
        };

        function addParent(e) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            isParent = e.data.isParent,
            nodes = zTree.getSelectedNodes(),
            treeNode = nodes[0];
            treeNode = zTree.addNodes(null, {id:(100 + newCount), pId:0, isParent:isParent, name:"new node" + (newCount++)});
            zTree.editName(treeNode[0]);
        };
        function edit() {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            nodes = zTree.getSelectedNodes(),
            treeNode = nodes[0];
            if (nodes.length == 0) {
                alert("请先选择一个节点");
                return;
            }
            zTree.editName(treeNode);
        };
        function remove(e) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            nodes = zTree.getSelectedNodes(),
            treeNode = nodes[0];
            if (nodes.length == 0) {
                alert("请先选择一个节点");
                return;
            }
            var callbackFlag = $("#callbackTrigger").attr("checked");
            zTree.removeNode(treeNode, callbackFlag);
        };
        function clearChildren(e) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            nodes = zTree.getSelectedNodes(),
            treeNode = nodes[0];
            if (nodes.length == 0 || !nodes[0].isParent) {
                alert("请先选择一个父节点");
                return;
            }
            zTree.removeChildNodes(treeNode);
        };

        function dropPrev(treeId, nodes, targetNode) {
            var pNode = targetNode.getParentNode();
            if (pNode && pNode.dropInner === false) {
                return false;
            } else {
                for (var i=0,l=curDragNodes.length; i<l; i++) {
                    var curPNode = curDragNodes[i].getParentNode();
                    if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
                        return false;
                    }
                }
            }
            return true;
        }
        function dropInner(treeId, nodes, targetNode) {
            if (targetNode && targetNode.dropInner === false) {
                return false;
            } else {
                for (var i=0,l=curDragNodes.length; i<l; i++) {
                    if (!targetNode && curDragNodes[i].dropRoot === false) {
                        return false;
                    } else if (curDragNodes[i].parentTId && curDragNodes[i].getParentNode() !== targetNode && curDragNodes[i].getParentNode().childOuter === false) {
                        return false;
                    }
                }
            }
            return true;
        }
        function dropNext(treeId, nodes, targetNode) {
            var pNode = targetNode.getParentNode();
            if (pNode && pNode.dropInner === false) {
                return false;
            } else {
                for (var i=0,l=curDragNodes.length; i<l; i++) {
                    var curPNode = curDragNodes[i].getParentNode();
                    if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
                        return false;
                    }
                }
            }
            return true;
        }

        var log, className = "dark", curDragNodes, autoExpandNode;
        function beforeDrag(treeId, treeNodes) {
            className = (className === "dark" ? "":"dark");
            for (var i=0,l=treeNodes.length; i<l; i++) {
                if (treeNodes[i].drag === false) {
                    curDragNodes = null;
                    return false;
                } else if (treeNodes[i].parentTId && treeNodes[i].getParentNode().childDrag === false) {
                    curDragNodes = null;
                    return false;
                }
            }
            curDragNodes = treeNodes;
            return true;
        }
        function beforeDragOpen(treeId, treeNode) {
            autoExpandNode = treeNode;
            return true;
        }
        function beforeDrop(treeId, treeNodes, targetNode, moveType, isCopy) {
            className = (className === "dark" ? "":"dark");
            return true;
        }
        function onDrag(event, treeId, treeNodes) {
            console.log("拖拽节点")
            className = (className === "dark" ? "":"dark");

        }
        function onDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {
            className = (className === "dark" ? "":"dark");

        }
        function onExpand(event, treeId, treeNode) {
            if (treeNode === autoExpandNode) {
                className = (className === "dark" ? "":"dark");

            }
        }
        function getTime() {
            var now= new Date(),
            h=now.getHours(),
            m=now.getMinutes(),
            s=now.getSeconds(),
            ms=now.getMilliseconds();
            return (h+":"+m+":"+s+ " " +ms);
        }

        function setTrigger() {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            zTree.setting.edit.drag.autoExpandTrigger = $("#callbackTrigger").attr("checked");
        }

        $(document).ready(function(){
            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            $("#addParent").bind("click", {isParent:true}, addParent);
            $("#addLeaf").bind("click", {isParent:false}, add);
            $("#edit").bind("click", edit);
            $("#remove").bind("click", remove);
            $("#clearChildren").bind("click", clearChildren);
            $("#callbackTrigger").bind("change", {}, setTrigger);
        });
    </SCRIPT>
</HEAD>
<BODY>
<div class="content_wrap">
    <div>
        <input type="checkbox" id="callbackTrigger" checked /> removeNode 方法是否触发 callback<br/>
        [ <a id="addParent" href="#" title="增加父节点" onclick="return false;">增加父节点</a> ]
        [ <a id="addLeaf" href="#" title="增加叶子节点" onclick="return false;">增加叶子节点</a> ]
        [ <a id="edit" href="#" title="编辑名称" onclick="return false;">编辑名称</a> ]<br/>
        [ <a id="remove" href="#" title="删除节点" onclick="return false;">删除节点</a> ]
        [ <a id="clearChildren" href="#" title="清空子节点" onclick="return false;">清空子节点</a> ]<br/>
    </div>
    <div class="zTreeDemoBackground left">
        <ul id="treeDemo" class="ztree"></ul>
    </div>
</div>
</BODY>
</HTML>

原文地址:https://www.cnblogs.com/excellencesy/p/11808151.html

时间: 2024-10-29 12:53:04

ztree-可拖拽可编辑的树的相关文章

zTree的拖拽排序

ztree本身是可以支持拖拽的,但是却没有找到明确的支持拖拽的排序,也就是说,在拖拽过程中,需要自定义维护拖拽后的顺序并保存至后台. 在这样一个比较常规的需求情况下,网上也有朋友给出了一些解决方案,比如自定义一个tree传入ztree, 不使用ztree的识别字段,而是使用自定义字段,然后当ztree的节点发生变化时,维护这些自定义字段,从而使得整个tree是有序的,并支持拖拽. 上边的这个解决方案,有一个问题,就是使用javascript构造的tree,传入ztree之后,会发生堆栈溢出,初步

ztree实现拖拽移动和复制

1.官网下载ztree:http://www.treejs.cn/v3/api.php 2.引入jquery.ztree.all.min.js 注意,这是基于jQuery的插件,请引入相关js 3.设置setting var setting = { data: { simpleData: { enable: true, idKey: "id", pIdKey: "parentId", rootPId: -1 }, key: { url: "nourl&qu

as3文本框的动态拖拽和编辑

现在很多软件都支持了编辑界面的文本拖拽和点击编辑来直接修改数值, 这样便于操作, 而且体验性也好, 抛砖引玉吧 于是就用好久没编写的as3来写了一下: 因为用的flash ide写的没有提示, 就暂时不做细节处理了, 如果用于工程上会有点小问题, 不过不影响本效果展示 代码: import flash.text.TextField; import flash.events.* var txtDragValue:TextField; txtDragValue.selectable = false;

100行代码解析Dojo树控件拖拽案例

案例设定: 创建2个树控件,左右排列. 使用拖动的方式,将树节点从左侧树控件拖拽的右侧树控件. 拖拽过程中右侧树控件要进行验证,确认是否可以方式拖拽中的节点. 放置的处理,识别要放置的节点,获取其信息并动态创建新的节点(基于基础类型进行实例化的过程). 右侧树控件内(实例化之后的节点),同类型节点间支持拖动排序. Dojo版本 1.10.3 图例1:创建2个树控件,左右排列. 图例2.3: 使用拖动的方式,将树节点从左侧树控件拖拽的右侧树控件. 拖拽过程中右侧树控件要进行验证,确认是否可以方式拖

MVC小系列(十五)【MVC+ZTree实现对树的CURD及拖拽操作】

根据上一讲的可以加载一棵大树,这讲讲下如果操作这颗大树 <link href="../../Scripts/JQuery-zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" /> <script src="../../Scripts/JQuery-zTree/js/jquery-1.4.4.min.js"></script> <script src=

通过Ztree生成页面html元素Dom树,以及拖拽改变元素的位置

zTree 是一款依靠 jQuery 实现的多功能 "树插件",http://www.treejs.cn/v3/main.php#_zTreeInfo,功能强大,不多赘述. 下面我将介绍如何实现使用该插件生成HTML元素Dom树,并对其进行多样操作. 先贴上一个简单的HTML页面(直接拿的ztree的用的,画面简单实用,里面的文字内容不用在意) 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-e

如何构建逻辑清晰的可拖拽树的数据结构

在为知笔记上的每日计划已经有好几天没有写实际内容了,抓狂脸.时间真是过得超级快呀,这几天双十一,疯狂的看喜欢的东西和看快递.其实并不是为了双十一降价而买东西,而是近来本来就有买东西的打算,而且撞到了双十一前,那就干脆放购物车里好了,还有就是半年没见降价的零食也打折啦啦啦~~^_^.好喜欢双十一结束后淘宝的宣传片<再见双十一>,接下来的这个星期,就静静的等待幸福来敲门~ 言归正传,今天来分享一下关于如何构建数据结构使得页面逻辑更为清晰的思路. 事情的起因是这样的,boss安排我做一个列表拖拽页面

Jquery 可拖拽的Ztree

比较懒,就只贴关键代码吧,自己把有用的属性全部打印出来了,也加了不少注释. 保存后涉及到的排序问题,刷新问题还未涉及到,后面有的话再加. 1 $.fn.zTree.init($("#ztree"), { 2 data: { 3 simpleData: { 4 enable: true 5 } 6 }, 7 view:{ 8 selectedMulti :false 9 }, 10 edit: { //此属性添加后,树才可以被拖拽 11 enable: true, 12 showRemo

结构-行为-样式 - Angularjs 环境下Ztree结合JqueryUI实现拖拽

新的项目中有一个需求是要求客户标签可以自定义,于是就想到了客户体验,让客户自己拖拽标签进行组合查询.但是理想很丰满,现实很骨感.一开始就遇到了问题,各个插件之间的结合问题,折腾一翻之后终于实现了这个功能,这里记录一下. 首先,在Angularjs+AMD+RequireJs的环境下引入插件: require.config({ baseUrl: "", urlArgs: 'ver=' + (+new Date()), waitSeconds: 0, paths: { 'jqueryUI'