使用layui模块化的方式封装ztree使用

604 604
Layui
Jordan
Jordan 2024-10-10 10:08:25

概述

将ztree扩展标准的layui控件调用方式

定义方式

采用原型的方式定义类
使用类的实例化来输出最终对象

步骤

  • 定义树的json数组
  • 定义默认参数,并根据外部参数进行参数合并
  • 使用render来渲染树
  • 对树json数组来进行操作,增加节点,删除节点,更新节点
  • 对外输出树数据json

代码

  1. layui.define(["jquery", "laytpl", "layer"], function (exports) {
  2. //全局定义
  3. var $ = layui.jquery;
  4. //定义一个类
  5. function adfZtree(config) {
  6. this.config = config;
  7. var self = this;
  8. this.treeId = config.elemId.replace("#", "");
  9. //内部标准方法
  10. function beforeDrag(treeId, treeNodes) {
  11. for (var i = 0, l = treeNodes.length; i < l; i++) {
  12. if (treeNodes[i].drag === false) {
  13. return false;
  14. }
  15. }
  16. return true;
  17. }
  18. function beforeDrop(treeId, treeNodes, targetNode, moveType) {
  19. return targetNode ? targetNode.drop !== false : true;
  20. }
  21. function beforeRemove(treeId, treeNode) {
  22. if ( typeof(treeNode.children) !== "undefined" && treeNode.children.length !== 0) {
  23. layer.msg("存在子节点不能删除");
  24. return false;
  25. }
  26. //从数据中删除以idKey为核心的值
  27. var idKey = self.config.idKey;
  28. var curData = self.config.data.find(x => x[idKey] === treeNode[idKey]);
  29. if (typeof(curData) !== "undefined") {
  30. self.config.data.splice(curData, 1);
  31. }
  32. return true;
  33. }
  34. //基础定义
  35. this.treeSetting = {
  36. view: {
  37. expandSpeed: "",
  38. selectedMulti: false
  39. },
  40. edit: {
  41. enable: true,
  42. showRemoveBtn: true,
  43. showRenameBtn: true
  44. },
  45. data: {
  46. key: {
  47. name:"name"
  48. },
  49. simpleData: {
  50. enable: true
  51. }
  52. },
  53. callback: {
  54. beforeDrag: beforeDrag,
  55. beforeRemove: beforeRemove,
  56. beforeDrop: beforeDrop
  57. }
  58. };
  59. //定义相关的属性
  60. if (config.hasOwnProperty("idKey")) {
  61. $.extend(true, this.treeSetting,{data: { simpleData: { idKey: config.idKey } } });
  62. }
  63. if (config.hasOwnProperty("pIdKey")) {
  64. $.extend(true, this.treeSetting,{data: { simpleData: { pIdKey: config.pIdKey } } });
  65. }
  66. if (config.hasOwnProperty("rootPId")) {
  67. $.extend(true, this.treeSetting,{data: { simpleData: { rootPId: config.rootPId } } });
  68. }
  69. if (config.hasOwnProperty("keyName")) {
  70. $.extend(true, this.treeSetting,{data: { key: { name: config.keyName } } });
  71. }
  72. //判断相关的属性
  73. if (config.hasOwnProperty("addHoverDom")) {
  74. $.extend(true, this.treeSetting,{view:{addHoverDom:config.addHoverDom}});
  75. }
  76. if (config.hasOwnProperty("removeHoverDom")) {
  77. $.extend(true, this.treeSetting,{view:{removeHoverDom:config.removeHoverDom}});
  78. }
  79. if (config.hasOwnProperty("beforeRemove")) {
  80. $.extend(true, this.treeSetting,{callback:{beforeRemove:config.beforeRemove}});
  81. }
  82. if (config.hasOwnProperty("beforeRename")) {
  83. $.extend(true, this.treeSetting,{callback:{beforeRename:config.beforeRename}});
  84. }
  85. if (config.hasOwnProperty("onClick")) {
  86. $.extend(true, this.treeSetting,{callback:{onClick:config.onClick}});
  87. }
  88. //额外原始的配置
  89. this.treeSetting = $.extend(true, this.treeSetting,config.SelfConfig);
  90. //初始化
  91. this.Init();
  92. };
  93. adfZtree.prototype.Init = function () {
  94. var data = this.config.data;
  95. //注册变化事件
  96. this.RegisterEvent();
  97. this.LoadData(data);
  98. }
  99. //增加节点
  100. // 将节点推送至 json数组中
  101. adfZtree.prototype.AddNode = function (treeNode, data) {
  102. var self = this;
  103. self.config.data.push(data);
  104. var zTree = $.fn.zTree.getZTreeObj(self.treeId);
  105. zTree.addNodes(treeNode, data);
  106. //表格单击的时候得到所在行
  107. }
  108. adfZtree.prototype.UpdateNode = function (treeNode) {
  109. var self = this;
  110. //根据treeNode 得到当前的
  111. var zTree = $.fn.zTree.getZTreeObj(self.treeId);
  112. zTree.updateNode(treeNode);
  113. //表格单击的时候得到所在行
  114. }
  115. //删除节点
  116. adfZtree.prototype.DeleteNode = function(treeNode) {
  117. //表格单击的时候得到所在行
  118. }
  119. //加载树数据
  120. adfZtree.prototype.LoadData = function (rowData) {
  121. var setting = this.treeSetting;
  122. var treeId = this.config.elemId;
  123. $.fn.zTree.init($(treeId), setting, rowData);
  124. }
  125. adfZtree.prototype.GetNodeData = function (idKeyValue) {
  126. var idKeyName = this.config.idKey;
  127. return this.config.data.find(x=>x[idKeyName] === idKeyValue);
  128. }
  129. adfZtree.prototype.GetData = function () {
  130. var self = this;
  131. var zTree = $.fn.zTree.getZTreeObj(self.treeId);
  132. return zTree.transformToArray(zTree.getNodes());
  133. }
  134. /**对外方法 */
  135. var o = {
  136. render: function (options) {
  137. options = options || {};
  138. options.selfConfig = options.selfConfig || {};
  139. options.data = options.data || [];
  140. options.idKey = options.idKey || "id";
  141. options.pIdKey = options.pIdKey || "pIdKey";
  142. options.rootPId = options.rootPId || "rootPId";
  143. options.keyName =options.keyName || "name";
  144. //创建实例
  145. var interObj = new adfZtree(options);
  146. return interObj;
  147. }
  148. };
  149. //导出方法
  150. var MOD_NAME = "adfZtree";
  151. exports(MOD_NAME, o);
  152. });

关键技术

  • prototype 类进行类的定义各个方法
  • 使用 new adfZtree 来输出具体对象
  • 定义默认的 树状参数,使用extend 来覆盖外部参数值
  • 将数的结构定义 json ,树的定义数据全部围绕json数组来操作,结果全部存储在本地,对外可以输出整体json 用于保存至数据库等相关的操作
回帖
  • 消灭零回复
作者信息
相关文章