treetable.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. layui.define(['layer', 'table'], function (exports) {
  2. var $ = layui.jquery;
  3. var layer = layui.layer;
  4. var table = layui.table;
  5. var treetable = {
  6. // 渲染树形表格
  7. render: function (param) {
  8. // 检查参数
  9. if (!treetable.checkParam(param)) {
  10. return;
  11. }
  12. // 获取数据
  13. if (param.data) {
  14. treetable.init(param, param.data);
  15. } else {
  16. $.getJSON(param.url, param.where, function (res) {
  17. treetable.init(param, res.data);
  18. });
  19. }
  20. },
  21. // 渲染表格
  22. init: function (param, data) {
  23. var mData = [];
  24. var doneCallback = param.done;
  25. var tNodes = data;
  26. // 补上id和pid字段
  27. for (var i = 0; i < tNodes.length; i++) {
  28. var tt = tNodes[i];
  29. if (!tt.id) {
  30. if (!param.treeIdName) {
  31. layer.msg('参数treeIdName不能为空', {icon: 5});
  32. return;
  33. }
  34. tt.id = tt[param.treeIdName];
  35. }
  36. if (!tt.pid) {
  37. if (!param.treePidName) {
  38. layer.msg('参数treePidName不能为空', {icon: 5});
  39. return;
  40. }
  41. tt.pid = tt[param.treePidName];
  42. }
  43. }
  44. // 对数据进行排序
  45. var sort = function (s_pid, data) {
  46. for (var i = 0; i < data.length; i++) {
  47. if (data[i].pid == s_pid) {
  48. var len = mData.length;
  49. if (len > 0 && mData[len - 1].id == s_pid) {
  50. mData[len - 1].isParent = true;
  51. }
  52. mData.push(data[i]);
  53. sort(data[i].id, data);
  54. }
  55. }
  56. };
  57. sort(param.treeSpid, tNodes);
  58. // 重写参数
  59. param.url = undefined;
  60. param.data = mData;
  61. param.page = {
  62. count: param.data.length,
  63. limit: param.data.length
  64. };
  65. param.cols[0][param.treeColIndex].templet = function (d) {
  66. var mId = d.id;
  67. var mPid = d.pid;
  68. var isDir = d.isParent;
  69. var emptyNum = treetable.getEmptyNum(mPid, mData);
  70. var iconHtml = '';
  71. for (var i = 0; i < emptyNum; i++) {
  72. iconHtml += '<span class="treeTable-empty"></span>';
  73. }
  74. if (isDir) {
  75. iconHtml += '<i class="layui-icon layui-icon-triangle-d"></i> <i class="layui-icon layui-icon-layer"></i>';
  76. } else {
  77. iconHtml += '<i class="layui-icon layui-icon-file"></i>';
  78. }
  79. iconHtml += '&nbsp;&nbsp;';
  80. var ttype = isDir ? 'dir' : 'file';
  81. var vg = '<span class="treeTable-icon open" lay-tid="' + mId + '" lay-tpid="' + mPid + '" lay-ttype="' + ttype + '">';
  82. return vg + iconHtml + d[param.cols[0][param.treeColIndex].field] + '</span>'
  83. };
  84. param.done = function (res, curr, count) {
  85. $(param.elem).next().addClass('treeTable');
  86. $('.treeTable .layui-table-page').css('display', 'none');
  87. $(param.elem).next().attr('treeLinkage', param.treeLinkage);
  88. // 绑定事件换成对body绑定
  89. /*$('.treeTable .treeTable-icon').click(function () {
  90. treetable.toggleRows($(this), param.treeLinkage);
  91. });*/
  92. if (param.treeDefaultClose) {
  93. treetable.foldAll(param.elem);
  94. }
  95. if (doneCallback) {
  96. doneCallback(res, curr, count);
  97. }
  98. };
  99. // 渲染表格
  100. table.render(param);
  101. },
  102. // 计算缩进的数量
  103. getEmptyNum: function (pid, data) {
  104. var num = 0;
  105. if (!pid) {
  106. return num;
  107. }
  108. var tPid;
  109. for (var i = 0; i < data.length; i++) {
  110. if (pid == data[i].id) {
  111. num += 1;
  112. tPid = data[i].pid;
  113. break;
  114. }
  115. }
  116. return num + treetable.getEmptyNum(tPid, data);
  117. },
  118. // 展开/折叠行
  119. toggleRows: function ($dom, linkage) {
  120. var type = $dom.attr('lay-ttype');
  121. if ('file' == type) {
  122. return;
  123. }
  124. var mId = $dom.attr('lay-tid');
  125. var isOpen = $dom.hasClass('open');
  126. if (isOpen) {
  127. $dom.removeClass('open');
  128. } else {
  129. $dom.addClass('open');
  130. }
  131. $dom.closest('tbody').find('tr').each(function () {
  132. var $ti = $(this).find('.treeTable-icon');
  133. var pid = $ti.attr('lay-tpid');
  134. var ttype = $ti.attr('lay-ttype');
  135. var tOpen = $ti.hasClass('open');
  136. if (mId == pid) {
  137. if (isOpen) {
  138. $(this).hide();
  139. if ('dir' == ttype && tOpen == isOpen) {
  140. $ti.trigger('click');
  141. }
  142. } else {
  143. $(this).show();
  144. if (linkage && 'dir' == ttype && tOpen == isOpen) {
  145. $ti.trigger('click');
  146. }
  147. }
  148. }
  149. });
  150. },
  151. // 检查参数
  152. checkParam: function (param) {
  153. if (!param.treeSpid && param.treeSpid != 0) {
  154. layer.msg('参数treeSpid不能为空', {icon: 5});
  155. return false;
  156. }
  157. if (!param.treeColIndex && param.treeColIndex != 0) {
  158. layer.msg('参数treeColIndex不能为空', {icon: 5});
  159. return false;
  160. }
  161. return true;
  162. },
  163. // 展开所有
  164. expandAll: function (dom) {
  165. $(dom).next('.treeTable').find('.layui-table-body tbody tr').each(function () {
  166. var $ti = $(this).find('.treeTable-icon');
  167. var ttype = $ti.attr('lay-ttype');
  168. var tOpen = $ti.hasClass('open');
  169. if ('dir' == ttype && !tOpen) {
  170. $ti.trigger('click');
  171. }
  172. });
  173. },
  174. // 折叠所有
  175. foldAll: function (dom) {
  176. $(dom).next('.treeTable').find('.layui-table-body tbody tr').each(function () {
  177. var $ti = $(this).find('.treeTable-icon');
  178. var ttype = $ti.attr('lay-ttype');
  179. var tOpen = $ti.hasClass('open');
  180. if ('dir' == ttype && tOpen) {
  181. $ti.trigger('click');
  182. }
  183. });
  184. }
  185. };
  186. // layui.link(layui.cache.base + 'treetable-lay/treetable.css');
  187. // 给图标列绑定事件
  188. $('body').on('click', '.treeTable .treeTable-icon', function () {
  189. var treeLinkage = $(this).parents('.treeTable').attr('treeLinkage');
  190. if ('true' == treeLinkage) {
  191. treetable.toggleRows($(this), true);
  192. } else {
  193. treetable.toggleRows($(this), false);
  194. }
  195. });
  196. exports('treetable', treetable);
  197. });