jQuery 利用ztree实现树形表格的实例代码
程序员文章站
2022-04-09 21:54:39
最近公司的项目中要做一个树形表格,因为之前一直在用ztree实现基本的树形结构,理所当然的首先想到利用ztree来做。
网上找了一下别人做的树形表格,有使用ztree的,...
最近公司的项目中要做一个树形表格,因为之前一直在用ztree实现基本的树形结构,理所当然的首先想到利用ztree来做。
网上找了一下别人做的树形表格,有使用ztree的,也有使用treetable的,但效果都不太好,于是参考使用ztree的做法自己做了一个,贴出来供大家参考,请看注释说明,效果如下所示。
<!doctype html> <html> <head> <link href="https://cdn.bootcss.com/ztree.v3/3.5.29/css/ztreestyle/ztreestyle.min.css" rel="external nofollow" rel="stylesheet"> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.bootcss.com/ztree.v3/3.5.29/js/jquery.ztree.all.min.js"></script> <style> body { overflow: auto; } .ztree *{ font-family: "微软雅黑","宋体",arial, "times new roman", times, serif; } .ztree { padding: 0; border-left: 1px solid #e3e3e3; border-right: 1px solid #e3e3e3; border-bottom: 1px solid #e3e3e3; } .ztree li a { vertical-align: middle; height: 32px; padding: 0px; } .ztree li > a { width: 100%; } .ztree li a.curselectednode { padding-top: 0px; background-color: #ffe6b0; border: 1px #ffb951 solid; opacity: 1; height: 32px; } .ztree li ul { padding-left: 0px } .ztree div.divtd span { line-height: 30px; vertical-align: middle; } .ztree div.divtd { height: 100%; line-height: 30px; border-top: 1px solid #e3e3e3; border-left: 1px solid #e3e3e3; text-align: center; display: inline-block; color: #6c6c6c; overflow: hidden; } .ztree div.divtd:first-child { text-align: left; text-indent: 10px; border-left: none; } .ztree .head { background: #e8eff5; } .ztree .head div.divtd { color: #393939; font-weight: bold; } .ztree .ck{ padding: 0 5px; margin: 2px 3px 7px 3px; } li:nth-child(odd){ background-color:#f5fafa; } li:nth-child(even){ background-color:#ffffff; } </style> </head> <body> <div class="layer"> <div id="tablemain"> <ul id="datatree" class="ztree"> </ul> </div> </div> </body> </html> <script type="text/javascript"> var newopen =null; $(function () { //初始化数据 var data = [{"id":"20170525091439001010","name":"企业注册","pid":null,"status":"1","typecode":"02"},{"id":"20170724174119005610","name":"部门沟通演练","pid":"20170525091439001010","status":"1","typecode":"2"},{"id":"20170725085455000110","name":"测试12","pid":null,"status":"1","typecode":"11"},{"id":"20170731171011000410","name":"审批流程","pid":null,"status":"1","typecode":"222"},{"id":"20170803133941018010","name":"单位登记","pid":null,"status":"1","typecode":"188"},{"id":"20170804085419000110","name":"模拟","pid":null,"status":"1","typecode":"122"},{"id":"20170809090321000110","name":"审批模拟(新)测试测试测试测试测试","pid":"20170525091439001010","status":"1","typecode":"110"},{"id":"20170809105407009210","name":"测测测测测测测测测测测测测测测测测测","pid":"20170809090321000110","status":"1","typecode":"123"},{"id":"20170814183837000210","name":"企业登记","pid":null,"status":"1","typecode":"111"},{"id":"20170822183437000710","name":"单事项-部门沟通","pid":"20170814183837000210","status":"1","typecode":"822"},{"id":"20170922112245000510","name":"23","pid":null,"status":"1","typecode":"03"},{"id":"20170922143810000010","name":"sdfa","pid":null,"status":"1","typecode":"04"},{"id":"20170922145203000110","name":"64526","pid":null,"status":"1","typecode":"34262"},{"id":"20170922155403001610","name":"333","pid":null,"status":"1","typecode":"33354"},{"id":"20170922171750000210","name":"4441234","pid":null,"status":"1","typecode":"44444"},{"id":"20170925160636007410","name":"测试数据","pid":"20170731171011000410","status":"1","typecode":"231"},{"id":"20170925163306007510","name":"23462111","pid":null,"status":"1","typecode":"2345"},{"id":"20170925163959007610","name":"242345","pid":"20170922112245000510","status":"1","typecode":"3625346"}]; queryhandler(data); }); var setting = { view: { showline: false, adddiydom: adddiydom, }, data: { simpledata: { enable: true } } }; /** * 自定义dom节点 */ function adddiydom(treeid, treenode) { var spacewidth = 15; var liobj = $("#" + treenode.tid); var aobj = $("#" + treenode.tid + "_a"); var switchobj = $("#" + treenode.tid + "_switch"); var icoobj = $("#" + treenode.tid + "_ico"); var spanobj = $("#" + treenode.tid + "_span"); aobj.attr('title', ''); aobj.append('<div class="divtd swich fnt" style="width:60%"></div>'); var div = $(liobj).find('div').eq(0); //从默认的位置移除 switchobj.remove(); spanobj.remove(); icoobj.remove(); //在指定的div中添加 div.append(switchobj); div.append(spanobj); //隐藏了层次的span var spacestr = "<span style='height:1px;display: inline-block;width:" + (spacewidth * treenode.level) + "px'></span>"; switchobj.before(spacestr); //图标垂直居中 icoobj.css("margin-top","9px"); switchobj.after(icoobj); var editstr = ''; //宽度需要和表头保持一致 editstr += '<div class="divtd" style="width:20%">' + (treenode.typecode == null ? '' : treenode.typecode ) + '</div>'; editstr += '<div class="divtd" style="width:10%">' + (treenode.status == '1' ? '有效' : '无效' ) + '</div>'; editstr += '<div class="divtd" style="width:10%">' + opt(treenode) + '</div>'; aobj.append(editstr); } //初始化列表 function queryhandler(ztreenodes){ //初始化树 $.fn.ztree.init($("#datatree"), setting, ztreenodes); //添加表头 var li_head = ' <li class="head"><a><div class="divtd" style="width:60%">类型名称</div><div class="divtd" style="width:20%">类型编码</div>' + '<div class="divtd" style="width:10%">是否有效</div><div class="divtd" style="width:10%">操作</div></a></li>'; var rows = $("#datatree").find('li'); if (rows.length > 0) { rows.eq(0).before(li_head) } else { $("#datatree").append(li_head); $("#datatree").append('<li ><div style="text-align: center;line-height: 30px;" >无符合条件数据</div></li>') } } function opt(treenode) { var htmlstr = ''; htmlstr += '<input type="button" class="ck" onclick="doedit(\'' + treenode.tid + '\',\'' + treenode.id + '\')" value="编辑"/>'; htmlstr += '<input type="button" class="ck" onclick="dodelete(\'' + treenode.tid + '\',\'' + treenode.id + '\', \'' + treenode.name + '\')" value="删除"/>'; return htmlstr; }
总结
以上所述是小编给大家介绍的jquery 利用ztree实现树形表格的实例代码,希望对大家有所帮助
下一篇: js实现数组和对象的深浅拷贝