当前位置:   article > 正文

使用ztree实现左侧树勾选添加到右侧列表_treewidget树形列表定位右侧表格

treewidget树形列表定位右侧表格

仅作记录

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>树形菜单表格</title>
  6. <link rel="stylesheet" type="text/css" href="../lib/layui/css/layui.css"/>
  7. <link rel="stylesheet" href="../lib/zTree_v3/css/metroStyle/metroStyle.css" type="text/css">
  8. <link rel="stylesheet" type="text/css" href="../css/new-common.css"/>
  9. <link rel="stylesheet" type="text/css" href="../css/new-public.css"/>
  10. <style type="text/css">
  11. .gt-body {
  12. height: 100%;
  13. padding: 0;
  14. }
  15. .gt-header {
  16. font-size: 18px;
  17. text-align: center;
  18. }
  19. .gtmap .gt-body .gtmap-body .gt-position-left {
  20. width: 50%;
  21. float: left;
  22. height: 100%;
  23. padding: 1% 0;
  24. border-right: 1px solid #ccc;
  25. box-sizing: border-box;
  26. }
  27. .gtmap .gt-body .gtmap-body .gt-position-right {
  28. width: 50%;
  29. height: 100%;
  30. padding: 1% 0;
  31. left: 50%;
  32. }
  33. .gtmap .gt-body .gtmap-body .tree-container {
  34. background: #fff;
  35. padding: 0;
  36. right: 0px;
  37. left: 20px;
  38. }
  39. .gtmap .gt-body .gtmap-body .tree-container .person-list{
  40. position: absolute;
  41. left: 0;
  42. right: 0;
  43. top: 30px;
  44. bottom: 0;
  45. overflow: auto;
  46. padding-right: 20px;
  47. }
  48. #treeDemo {
  49. position: absolute;
  50. top: 40px;
  51. bottom: 0;
  52. left: 0;
  53. right: 0;
  54. }
  55. .tip-info {
  56. height: 30px;
  57. line-height: 30px;
  58. }
  59. .person-list li {
  60. height: 30px;
  61. line-height: 30px;
  62. color: #333;
  63. }
  64. .person-list li .layui-icon {
  65. font-size: 20px;
  66. color: #aaa;
  67. float: right;
  68. position: relative;
  69. top: 1px;
  70. cursor: pointer;
  71. }
  72. .person-list li .left-icon {
  73. background-color: transparent;
  74. background-repeat: no-repeat;
  75. background-attachment: scroll;
  76. background-image: url(../lib/zTree_v3/css/metroStyle/img/ztree-cl-qunzu.png);
  77. background-image: url(../lib/zTree_v3/css/metroStyle/img/ztree-cl-qunzu.gif);
  78. font-size: 12px;
  79. background-position: -147px -42px;
  80. vertical-align: middle;
  81. line-height: 0;
  82. margin: 0;
  83. padding: 0;
  84. width: 21px;
  85. height: 20px;
  86. line-height: 20px;
  87. float: left;
  88. position: relative;
  89. top: 3px;
  90. margin-right: 3px;
  91. }
  92. .zTreeBlurrySearch{
  93. margin-right: 20px;
  94. }
  95. </style>
  96. </head>
  97. <body class="gtmap">
  98. <div class="gt-body">
  99. <div class="gt-toolbar gt-header">
  100. 添加人员
  101. </div>
  102. <div class="gtmap-body">
  103. <div class="gt-position-left">
  104. <div class="tree-container">
  105. <!--<div class="tree-cnt">-->
  106. <div class="zTreeBlurrySearch" id="zTreeSearch">
  107. <div class="input-icon-box-right input-icon-box-sm">
  108. <input id="key" name="" placeholder="请输入检索内容" autocomplete="off" class="layui-input">
  109. <i class="layui-icon layui-icon-search"></i>
  110. </div>
  111. </div>
  112. <ul id="treeDemo" class="ztree userTreeColor"></ul>
  113. </div>
  114. </div>
  115. <div class="gtmap-cnt gt-position-right">
  116. <div class="tree-container list-container">
  117. <p class="tip-info">已选择了<span>0</span>个联系人</p>
  118. <ul class="person-list"> 无数据 </ul>
  119. </div>
  120. </div>
  121. </div>
  122. </div>
  123. </body>
  124. <script type="text/html" id="listTpl">
  125. <p class="tip-info">已选择了<span>{{d.length}}</span>个联系人</p>
  126. <ul class="person-list">
  127. {{# layui.each(d, function(index, item){ }}
  128. <li data-id="{{item.id}}">
  129. <i class="left-icon"></i>
  130. <span>{{item.name}}</span>
  131. <i class="layui-icon layui-icon-close-fill delete-icon"></i>
  132. </li>
  133. {{# }); }}
  134. {{# if(d.length === 0){ }}
  135. 无数据
  136. {{# } }}
  137. </ul>
  138. </script>
  139. <script type="text/javascript" src="../lib/layui/layui.js"></script>
  140. <!--加载ztree-->
  141. <script type="text/javascript" src="../lib/zTree_v3/js/jquery-1.4.4.min.js"></script>
  142. <script type="text/javascript" src="../lib/zTree_v3/js/jquery.ztree.core.js"></script>
  143. <script type="text/javascript" src="../lib/zTree_v3/js/jquery.ztree.excheck.js"></script>
  144. <script type="text/javascript" src="../lib/zTree_v3/js/jquery.ztree.exedit.js"></script>
  145. <script type="text/javascript" src="../lib/zTree_v3/js/jquery.ztree.exhide.js"></script>
  146. <script src="../lib/zTree_v3/js/fuzzysearch.js" type="text/javascript" charset="utf-8"></script>
  147. <script>
  148. // 先取到ztree要用的jquery
  149. var ztreeS = $;
  150. layui.use(['jquery', 'form', 'laypage', 'laytpl', 'element'], function () {
  151. var form = layui.form,
  152. laypage = layui.laypage,
  153. laytpl = layui.laytpl,
  154. element = layui.element,
  155. $ = layui.jquery;
  156. // 定义右侧列表渲染的数组
  157. var renderArr = [];
  158. //树形配置
  159. var setting = {
  160. check: {
  161. enable: true//checkbox
  162. },
  163. view: {
  164. nameIsHTML: true, //允许name支持html
  165. selectedMulti: false
  166. },
  167. edit: {
  168. enable: false,
  169. editNameSelectAll: false
  170. },
  171. data: {
  172. simpleData: {
  173. enable: true
  174. }
  175. },
  176. callback: {
  177. onCheck: zTreeOnCheck
  178. }
  179. };
  180. //树形的节点数据
  181. var zNodes = [
  182. {id: 1, pId: 0, name: "父节点1", open: true},
  183. {id: 11, pId: 1, name: "父节点11", open: true},
  184. {id: 111, pId: 11, name: "叶子节点111"},
  185. {id: 112, pId: 11, name: "叶子节点112"},
  186. {id: 113, pId: 11, name: "叶子节点113"},
  187. {id: 114, pId: 11, name: "叶子节点114"},
  188. {id: 12, pId: 1, name: "父节点12"},
  189. {id: 121, pId: 12, name: "叶子节点121"},
  190. {id: 122, pId: 12, name: "叶子节点122"},
  191. {id: 123, pId: 12, name: "叶子节点123"},
  192. {id: 124, pId: 12, name: "叶子节点124"},
  193. {id: 13, pId: 1, name: "父节点13"},
  194. {id: 2, pId: 0, name: "父节点2"},
  195. {id: 21, pId: 2, name: "父节点21", open: true},
  196. {id: 211, pId: 21, name: "叶子节点211"},
  197. {id: 212, pId: 21, name: "叶子节点212"},
  198. {id: 213, pId: 21, name: "叶子节点213"},
  199. {id: 214, pId: 21, name: "叶子节点214"},
  200. {id: 22, pId: 2, name: "父节点22"},
  201. {id: 221, pId: 22, name: "叶子节点221"},
  202. {id: 222, pId: 22, name: "叶子节点222"},
  203. {id: 223, pId: 22, name: "叶子节点223"},
  204. {id: 224, pId: 22, name: "叶子节点224"},
  205. {id: 23, pId: 2, name: "父节点23"},
  206. {id: 231, pId: 23, name: "叶子节点231"},
  207. {id: 232, pId: 23, name: "叶子节点232"},
  208. {id: 233, pId: 23, name: "叶子节点233"},
  209. {id: 234, pId: 23, name: "叶子节点234"},
  210. {id: 3, pId: 0, name: "父节点3"}
  211. ];
  212. // 获取某节点下的所有叶子节点
  213. function getAllChildrenNodes(treeNode, result) {
  214. if (treeNode.isParent) {
  215. var childrenNodes = treeNode.children;
  216. if (childrenNodes) {
  217. for (var i = 0; i < childrenNodes.length; i++) {
  218. if (childrenNodes[i].isParent) {
  219. result = getAllChildrenNodes(childrenNodes[i], result);
  220. } else {
  221. result += ',' + childrenNodes[i].id;
  222. }
  223. }
  224. }
  225. }
  226. return result;
  227. }
  228. // 勾选回调函数
  229. function zTreeOnCheck(event, treeId, treeNode) {
  230. // alert(treeNode.id + ", " + treeNode.name + "," + treeNode.checked);
  231. // 拿到name值,因为搜索之后匹配字段加样式,获取到的name值不正确,所以要根据id进行匹配
  232. var str = "";
  233. var selectedNodesId = [];
  234. if (treeNode.checked) {
  235. if (treeNode.isParent) {
  236. str = getAllChildrenNodes(treeNode, str);
  237. selectedNodesId = str.split(',');
  238. selectedNodesId = selectedNodesId.slice(1);
  239. } else {
  240. selectedNodesId.push(treeNode.id)
  241. }
  242. for (var i = 0; i < selectedNodesId.length; i++) {
  243. var selectedId = selectedNodesId[i];
  244. var selectedName = zNodes.filter(function (v) {
  245. return v.id == selectedId
  246. })[0].name;
  247. renderArr.push({
  248. id: selectedId,
  249. name: selectedName
  250. })
  251. }
  252. } else {
  253. if (treeNode.isParent) {
  254. str = getAllChildrenNodes(treeNode, str);
  255. selectedNodesId = str.split(',');
  256. selectedNodesId = selectedNodesId.slice(1);
  257. } else {
  258. selectedNodesId.push(treeNode.id)
  259. }
  260. for (var i = 0; i < selectedNodesId.length; i++) {
  261. var selectedId = selectedNodesId[i];
  262. var index = renderArr.indexOf(renderArr.filter(function (v) {
  263. return v.id == selectedId
  264. })[0]);
  265. renderArr.splice(index, 1);
  266. }
  267. }
  268. console.log(renderArr)
  269. laytpl($('#listTpl').html()).render(renderArr, function (html) {
  270. $('.list-container').html(html);
  271. })
  272. }
  273. $(document).on('click', '.delete-icon', function () {
  274. // 更新右侧列表
  275. var dataId = $(this).parent().attr('data-id');
  276. var index = renderArr.indexOf(renderArr.filter(function (v) {
  277. return v.id == dataId
  278. })[0]);
  279. renderArr.splice(index, 1);
  280. laytpl($('#listTpl').html()).render(renderArr, function (html) {
  281. $('.list-container').html(html);
  282. })
  283. // 更新左侧树
  284. var treeObj = ztreeS.fn.zTree.getZTreeObj("treeDemo");
  285. function filter(node) {
  286. return (node.id == dataId);
  287. }
  288. var node = treeObj.getNodesByFilter(filter, true); // 仅查找一个节点
  289. treeObj.checkNode(node, false, true, false)
  290. })
  291. //树形加载
  292. $(document).ready(function () {
  293. ztreeS.fn.zTree.init($("#treeDemo"), setting, zNodes);
  294. fuzzySearch('treeDemo', '#key', null, true); //初始化模糊搜索方法
  295. });
  296. });
  297. </script>
  298. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/322384
推荐阅读
  

闽ICP备14008679号