步骤
1、引入ztree的css与js
2、setting 配置与相关的拖动配置
代码
<!DOCTYPE html> <html> <head> <title>系统安装</title> <link href="/style/v3/layui/css/layui.css" type="text/css" rel="stylesheet" /> <link href="/style/v3/ztree/css/zTreeStyle.css" type="text/css" rel="stylesheet" /> <script src="/style/v3/layui/layui.js"></script> <script src="/style/v3/js/jquery.min.js?t=287"></script> <script src="/style/v3/js/adfutil.js?t=287"></script> <script src="/style/v3/ueditor/ueditor.config.js?t=287"></script> <script src="/style/v3/ueditor/ueditor.all.js"></script> <script src="/style/v3/ztree/js/jquery.ztree.all.js?t=287"></script> <script> var table = layui.table; </script> </head> <body> <div class="layui-form" id="curForm" lay-filter="curForm"> <div class="layui-row"> Ztree使用 </div> <div class="layui-row"> <div class="layui-col-md2"> <ul id="treeDemo" class="ztree"></ul> </div> <div class="layui-col-md10"></div> </div> </div> <script> $(function() { var setting = { edit: { enable: true, showRemoveBtn: true, showRenameBtn: true }, data: { simpleData: { enable: true } }, callback: { beforeDrag: beforeDrag, beforeDrop: beforeDrop } }; var zNodes =[ { id:1, pId:0, name:"随意拖拽 1", open:true}, { id:11, pId:1, name:"随意拖拽 1-1"}, { id:12, pId:1, name:"随意拖拽 1-2", open:true}, { 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:13, pId:1, name:"禁止拖拽 1-3", open:true, drag:false}, { id:131, pId:13, name:"禁止拖拽 1-3-1", drag:false}, { id:132, pId:13, name:"禁止拖拽 1-3-2", drag:false}, { id:133, pId:13, name:"随意拖拽 1-3-3"}, { id:2, pId:0, name:"随意拖拽 2", open:true}, { id:21, pId:2, name:"随意拖拽 2-1"}, { id:22, pId:2, name:"禁止拖拽到我身上 2-2", open:true, drop:false}, { id:221, pId:22, name:"随意拖拽 2-2-1"}, { id:222, pId:22, name:"随意拖拽 2-2-2"}, { id:223, pId:22, name:"随意拖拽 2-2-3"}, { id:23, pId:2, name:"随意拖拽 2-3"} ]; function beforeDrag(treeId, treeNodes) { for (var i=0,l=treeNodes.length; i<l; i++) { if (treeNodes[i].drag === false) { return false; } } return true; } function beforeDrop(treeId, treeNodes, targetNode, moveType) { return targetNode ? targetNode.drop !== false : true; } function setCheck() { var zTree = $.fn.zTree.getZTreeObj("treeDemo"); var isCopy = $("#copy").attr("checked"); var isMove = $("#move").attr("checked"); var prev = $("#prev").attr("checked"); var inner = $("#inner").attr("checked"); var next = $("#next").attr("checked"); isMove = true; isCopy = true; prev = true; inner = true; next = true; zTree.setting.edit.drag.isCopy = isCopy; zTree.setting.edit.drag.isMove = isMove; showCode(1, ['setting.edit.drag.isCopy = ' + isCopy, 'setting.edit.drag.isMove = ' + isMove]); zTree.setting.edit.drag.prev = prev; zTree.setting.edit.drag.inner = inner; zTree.setting.edit.drag.next = next; showCode(2, ['setting.edit.drag.prev = ' + prev, 'setting.edit.drag.inner = ' + inner, 'setting.edit.drag.next = ' + next]); } function showCode(id, str) { var code = $("#code" + id); code.empty(); for (var i=0, l=str.length; i<l; i++) { code.append("<li>"+str[i]+"</li>"); } } $.fn.zTree.init($("#treeDemo"), setting, zNodes); setCheck(); $("#copy").bind("change", setCheck); $("#move").bind("change", setCheck); $("#prev").bind("change", setCheck); $("#inner").bind("change", setCheck); $("#next").bind("change", setCheck); var form = layui.form; form.render(); }); </script> </body> </html>