From 6d43db5e229a00a4a16e76b84db7f8668fbb792d Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <285584806@qq.com> Date: Fri, 28 Apr 2023 16:31:11 +0800 Subject: [PATCH 01/16] =?UTF-8?q?=E4=BC=98=E5=8C=96=20layui.sort=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=80=E4=B8=AA=E5=8F=82=E6=95=B0=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E6=98=AF=E5=90=A6=E7=9B=B4=E6=8E=A5=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layui.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/layui.js b/src/layui.js index 03a8dadb..fdb0daaf 100644 --- a/src/layui.js +++ b/src/layui.js @@ -614,12 +614,12 @@ }; // 将数组中的成员对象按照某个 key 的 value 值进行排序 - Layui.prototype.sort = function(arr, key, desc){ + Layui.prototype.sort = function(arr, key, desc, notClone){ var that = this - ,clone = JSON.parse( + ,clone = notClone ? (arr || []) : JSON.parse( JSON.stringify(arr || []) ); - + // 若未传入 key,则直接返回原对象 if(that.type(arr) === 'object' && !key){ return clone; From 90de31719b1cc129fdf2681ac053863547a02f09 Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <285584806@qq.com> Date: Fri, 28 Apr 2023 16:33:57 +0800 Subject: [PATCH 02/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20table=20`autoSort`?= =?UTF-8?q?=20=E4=B8=BA=20true=20=E5=B9=B6=E4=B8=94=E6=98=AF=20=E9=9D=99?= =?UTF-8?q?=E6=80=81=E6=95=B0=E6=8D=AE=20`data`=20=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E4=B8=8B=E6=9B=B4=E6=96=B0cache=E4=BF=A1=E6=81=AF=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=BD=B1=E5=93=8D=20`data`=20=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/table.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/table.js b/src/modules/table.js index 3dbd74e1..f82f6ea1 100644 --- a/src/modules/table.js +++ b/src/modules/table.js @@ -1530,11 +1530,11 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){ // 默认为前端自动排序。如果否,则需自主排序(通常为服务端处理好排序) if(options.autoSort){ if(opts.type === 'asc'){ //升序 - thisData = layui.sort(data, field); + thisData = layui.sort(data, field, null, true); } else if(opts.type === 'desc'){ //降序 - thisData = layui.sort(data, field, true); + thisData = layui.sort(data, field, true, true); } else { // 清除排序 - thisData = layui.sort(data, table.config.indexName); + thisData = layui.sort(data, table.config.indexName, null, true); delete that.sortKey; delete options.initSort; } From 832ce1d3ad40d983fb9e665149bbe93c0468ada6 Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <285584806@qq.com> Date: Fri, 28 Apr 2023 16:48:45 +0800 Subject: [PATCH 03/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20table=20=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E8=A1=A8=E5=A4=B4=E6=9C=89=E5=AD=97=E6=AE=B5=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E4=B8=BA=20hide=20=E6=88=96=E8=80=85=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E7=AD=9B=E9=80=89=E5=92=8C=20`table.hideCol`=20?= =?UTF-8?q?=E9=9A=90=E8=97=8F=E9=83=A8=E5=88=86=E5=AD=97=E6=AE=B5=E4=B9=8B?= =?UTF-8?q?=E5=90=8E=E6=89=A7=E8=A1=8C=20`table.reload`=20=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E8=A1=A8=E5=A4=B4=E9=94=99=E4=BD=8D=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/table.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/modules/table.js b/src/modules/table.js index f82f6ea1..e74bc8b5 100644 --- a/src/modules/table.js +++ b/src/modules/table.js @@ -139,7 +139,7 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){ // thead 区域模板 var TPL_HEADER = function(options){ - var rowCols = '{{#if(item2.colspan){}} colspan="{{=item2.colspan}}"{{#} if(item2.rowspan){}} rowspan="{{=item2.rowspan}}"{{#}}}'; + var rowCols = '{{#var colspan = layui.type(item2.colspan2) === \'number\' ? item2.colspan2 : item2.colspan; if(colspan){}} colspan="{{=colspan}}"{{#} if(item2.rowspan){}} rowspan="{{=item2.rowspan}}"{{#}}}'; options = options || {}; return ['= 1 || !hide ? 'removeClass' : 'addClass'](HIDE); // 如果子列显示,父列必然需要显示 + parentTh[parentColspan ? 'removeClass' : 'addClass'](HIDE); // 如果子列显示,父列必然需要显示 - getThisCol.colspan = parentColspan; //同步 colspan 参数 - getThisCol.hide = parentColspan >= 1 || !hide; //同步 hide 参数 + getThisCol.colspan2 = parentColspan; // 更新实际的 colspan 数 + getThisCol.hide = parentColspan < 1; // 同步 hide 参数 //递归,继续往上查询是否有父列 var nextParentKey = parentTh.data('parentkey'); @@ -2527,7 +2527,7 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){ if (item22.PARENT_COL_INDEX || (childIndex >= 1 && childIndex == (item2.colspan || 1))) return; item22.PARENT_COL_INDEX = index; item2.CHILD_COLS.push(item22); - childIndex = childIndex + (item22.hide ? 0 : parseInt(item22.colspan > 1 ? item22.colspan : 1)); + childIndex = childIndex + (parseInt(item22.colspan > 1 ? item22.colspan : 1)); eachChildCols(index, cols, i2, item22); } }); From ab16f47dd1bd4460b4171a926d2e726bb8e2a6ab Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <285584806@qq.com> Date: Fri, 28 Apr 2023 18:05:32 +0800 Subject: [PATCH 04/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20table=20`reload`=20?= =?UTF-8?q?=E8=A1=A8=E6=A0=BC=E7=9A=84=E4=B8=AD=E5=AF=B9=20`cols`=20?= =?UTF-8?q?=E5=8F=91=E7=94=9F=E5=8F=98=E5=8C=96=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=E8=A1=A8=E5=A4=B4=E5=87=BA=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/table.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/modules/table.js b/src/modules/table.js index e74bc8b5..451cfada 100644 --- a/src/modules/table.js +++ b/src/modules/table.js @@ -872,7 +872,14 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){ // 对参数进行深度或浅扩展 that.config = $.extend(deep, {}, that.config, options); - + if (type !== 'reloadData') { + layui.each(that.config.cols, function (i1, item1) { + layui.each(item1, function (i2, item2) { + delete item2.colspan2; + }) + }) + delete that.config.HAS_SET_COLS_PATCH; + } // 执行渲染 that.render(type); }; From 221009ee51a8c30b722c7d628fa25edea192b1ac Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <285584806@qq.com> Date: Fri, 28 Apr 2023 18:10:44 +0800 Subject: [PATCH 05/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20treeTable=20`reload`?= =?UTF-8?q?=20=E5=92=8C=20`reloadData`=20=E4=BC=A0=E5=8F=82=E4=B8=AD?= =?UTF-8?q?=E5=B8=A6=E4=BA=86=20parseData=20=E5=92=8C=20done=20=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=E7=9A=84=E6=97=B6=E5=80=99=E6=B8=B2=E6=9F=93=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/treeTable.js | 73 +++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 28 deletions(-) diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index 72ed2b7b..cdd12b92 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -111,34 +111,21 @@ layui.define(['table'], function (exports) { }) } - Class.prototype.init = function () { - var that = this; - var options = that.config; - - // 先初始一个空的表格以便拿到对应的表格实例信息 - var tableIns = table.render($.extend({}, options, { - data: [], - url: '', - done: null - })) - var id = tableIns.config.id; - thisTreeTable.that[id] = that; // 记录当前实例对象 - that.tableIns = tableIns; - - var treeOptions = options.tree; - var customName = treeOptions.customName; - var isParentKey = customName.isParent; - var childrenKey = customName.children; - + var updateOptions = function (id, options, reload) { + var that = getThisTable(id); + var thatOptionsTemp = $.extend(true, {} , that.getOptions(), options); + var treeOptions = thatOptionsTemp.tree; + var childrenKey = treeOptions.customName.children; // 处理属性 var parseData = options.parseData; var done = options.done; - if (options.url) { + if (thatOptionsTemp.url) { // 异步加载的时候需要处理parseData进行转换 - options.parseData = function () { - var parseDataThat = this; - var args = arguments; + if (!reload || (reload && parseData && !parseData.mod)) { + options.parseData = function () { + var parseDataThat = this; + var args = arguments; var retData = args[0]; if (layui.type(parseData) === 'function') { retData = parseData.apply(parseDataThat, args) || args[0]; @@ -151,7 +138,9 @@ layui.define(['table'], function (exports) { that.initData(retData[dataName]); - return retData; + return retData; + } + options.parseData.mod = true } } else { options.data = options.data || []; @@ -165,9 +154,10 @@ layui.define(['table'], function (exports) { that.initData(options.data); } - options.done = function () { - var args = arguments; - var doneThat = this; + if (!reload || (reload && done && !done.mod)) { + options.done = function () { + var args = arguments; + var doneThat = this; var tableView = this.elem.next(); that.updateStatus(null, { @@ -188,11 +178,35 @@ layui.define(['table'], function (exports) { that.renderTreeTable(tableView); if (layui.type(done) === 'function') { - return done.apply(doneThat, args); + return done.apply(doneThat, args); + } } + options.done.mod = true; } } + Class.prototype.init = function () { + var that = this; + var options = that.config; + + // 先初始一个空的表格以便拿到对应的表格实例信息 + var tableIns = table.render($.extend({}, options, { + data: [], + url: '', + done: null + })) + var id = tableIns.config.id; + thisTreeTable.that[id] = that; // 记录当前实例对象 + that.tableIns = tableIns; + + // var treeOptions = options.tree; + // var customName = treeOptions.customName; + // var isParentKey = customName.isParent; + // var childrenKey = customName.children; + + updateOptions(id, options); + } + // 初始默认配置 Class.prototype.config = { tree: { @@ -819,6 +833,9 @@ layui.define(['table'], function (exports) { if (layui.type(item) === 'array') delete that.config[key]; }); + // 根据需要处理options中的一些参数 + updateOptions(that.config.id, options, true); + // 对参数进行深度或浅扩展 that.config = $.extend(deep, {}, that.config, options); From c6774f38741f78e481a3b3c9b9903f74dfc6d0bf Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <285584806@qq.com> Date: Fri, 28 Apr 2023 18:16:59 +0800 Subject: [PATCH 06/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20treeTable=20`updateN?= =?UTF-8?q?ode`=20=E6=89=A7=E8=A1=8C=E6=B2=A1=E6=9C=89=E8=B5=B7=E4=BD=9C?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/treeTable.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index cdd12b92..c22bfbf7 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -323,6 +323,7 @@ layui.define(['table'], function (exports) { var dataCache = table.cache[tableId][index]; if (newValue !== 'delete' && dataCache) { + $.extend(dataCache, newValue); return clone ? $.extend({}, dataCache) : dataCache; } @@ -978,6 +979,7 @@ layui.define(['table'], function (exports) { tableView.find('.layui-table-' + item + ' tbody tr[lay-data-index="' + index + '"]').replaceWith($(trNew[['trs', 'trs_fixed', 'trs_fixed_r'][i]].join('')).attr({ 'data-index': trIndex, 'lay-data-index': index, + 'data-level': trLevel })); }); that.renderTreeTable(tableView.find('tr[lay-data-index="' + index + '"]'), trLevel); From 519433cf26ea480d7d404093f3b0293cb5220f94 Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <285584806@qq.com> Date: Fri, 28 Apr 2023 18:21:16 +0800 Subject: [PATCH 07/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20treeTable=20?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E9=9A=90=E8=97=8F=E6=89=80=E6=9C=89=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E6=96=B9=E6=B3=95=E7=9A=84=E6=97=B6=E5=80=99=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E6=A0=8F=E6=B6=88=E5=A4=B1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/treeTable.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index c22bfbf7..56ea0a5a 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -654,16 +654,16 @@ layui.define(['table'], function (exports) { // 将所有已经打开的节点的状态设置为关闭, that.updateStatus(null, {LAY_EXPAND: false}); // 只处理当前页,如果需要处理全部表格,需要用treeTable.updateStatus // 隐藏所有非顶层的节点 - tableView.find('tbody tr[data-level!="0"]').addClass('layui-hide'); + tableView.find('.layui-table-box tbody tr[data-level!="0"]').addClass('layui-hide'); // 处理顶层节点的图标 var trLevel0 = tableView.find('tbody tr[data-level="0"]'); trLevel0.find('.layui-table-tree-flexIcon').html(treeOptions.view.flexIconClose); - trLevel0.find('.layui-table-tree-nodeIcon').html(treeOptions.view.iconClose); + treeOptions.view.showIcon && trLevel0.find('.layui-table-tree-nodeIcon').html(treeOptions.view.iconClose); - treeTable.resize(); + treeTable.resize(id); } else { return hint.error('暂不支持展开全部'); - + // 展开所有 if (treeOptions.async.enable) { // 存在异步加载 From 0b41afe24554f51b2e55e6bed1c4beea8f1dd328 Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <285584806@qq.com> Date: Fri, 28 Apr 2023 18:25:54 +0800 Subject: [PATCH 08/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20treeTable=20?= =?UTF-8?q?=E8=A1=8C=E5=86=85=E4=BA=8B=E4=BB=B6=E7=9A=84=20`update`=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=9B=B4=E6=96=B0=E5=88=B0=E5=85=B3=E9=94=AE?= =?UTF-8?q?=E7=9A=84=E2=80=9C=E5=90=8D=E7=A7=B0=E5=88=97=E2=80=9D=E6=97=B6?= =?UTF-8?q?=E6=A0=91=E8=8A=82=E7=82=B9=E7=89=B9=E5=BE=81=E4=B8=A2=E5=A4=B1?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/treeTable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index 56ea0a5a..0d9d5302 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -937,6 +937,7 @@ layui.define(['table'], function (exports) { var args = arguments; $.extend(tableThat.getNodeDataByIndex(trIndex), args[0]); var ret = updateFn.apply(updateThat, args); // 主要负责更新节点内容 + obj.tr.find('td[data-field="' + obj.config.tree.customName.name + '"]').children('div.layui-table-cell').removeClass('layui-table-tree-item'); tableThat.renderTreeTable(obj.tr, obj.tr.attr('data-level'), false); return ret; } @@ -1248,7 +1249,6 @@ layui.define(['table'], function (exports) { if (tableView.hasClass('layui-table-tree')) { updateObjParams(obj); if (obj.field === options.tree.customName.name) { - obj.tr.find('td[data-field="' + obj.field + '"]').children('div.layui-table-cell').removeClass('layui-table-tree-item') obj.update({}); // 通过update调用执行tr节点的更新 } } From 0fdaf22f8203b789094e4422b72baa9fe9c5aa68 Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <285584806@qq.com> Date: Fri, 28 Apr 2023 18:35:52 +0800 Subject: [PATCH 09/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20table=20=E7=82=B9?= =?UTF-8?q?=E5=87=BB=20`radio`=20=E5=88=97=E7=9A=84=E9=80=89=E9=A1=B9?= =?UTF-8?q?=E6=97=B6=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/treeTable.js | 64 ++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index 0d9d5302..6ac59942 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -126,17 +126,17 @@ layui.define(['table'], function (exports) { options.parseData = function () { var parseDataThat = this; var args = arguments; - var retData = args[0]; - if (layui.type(parseData) === 'function') { - retData = parseData.apply(parseDataThat, args) || args[0]; - } - var dataName = parseDataThat.response.dataName; - // 处理 isSimpleData - if (treeOptions.data.isSimpleData && !treeOptions.async.enable) { // 异步加载和 isSimpleData 不应该一起使用 - retData[dataName] = that.flatToTree(retData[dataName]); - } + var retData = args[0]; + if (layui.type(parseData) === 'function') { + retData = parseData.apply(parseDataThat, args) || args[0]; + } + var dataName = parseDataThat.response.dataName; + // 处理 isSimpleData + if (treeOptions.data.isSimpleData && !treeOptions.async.enable) { // 异步加载和 isSimpleData 不应该一起使用 + retData[dataName] = that.flatToTree(retData[dataName]); + } - that.initData(retData[dataName]); + that.initData(retData[dataName]); return retData; } @@ -159,25 +159,25 @@ layui.define(['table'], function (exports) { var args = arguments; var doneThat = this; - var tableView = this.elem.next(); - that.updateStatus(null, { - LAY_HAS_EXPANDED: false // 去除已经打开过的状态 - }); - // 更新cache中的内容 将子节点也存到cache中 - updateCache(id, childrenKey); - // 更新全选框的状态 - var layTableAllChooseElem = tableView.find('[name="layTableCheckbox"][lay-filter="layTableAllChoose"]'); - if (layTableAllChooseElem.length) { - var checkStatus = treeTable.checkStatus(id); - layTableAllChooseElem.prop({ - checked: checkStatus.isAll && checkStatus.data.length, - indeterminate: !checkStatus.isAll && checkStatus.data.length - }) - } + var tableView = this.elem.next(); + that.updateStatus(null, { + LAY_HAS_EXPANDED: false // 去除已经打开过的状态 + }); + // 更新cache中的内容 将子节点也存到cache中 + updateCache(id, childrenKey); + // 更新全选框的状态 + var layTableAllChooseElem = tableView.find('[name="layTableCheckbox"][lay-filter="layTableAllChoose"]'); + if (layTableAllChooseElem.length) { + var checkStatus = treeTable.checkStatus(id); + layTableAllChooseElem.prop({ + checked: checkStatus.isAll && checkStatus.data.length, + indeterminate: !checkStatus.isAll && checkStatus.data.length + }) + } - that.renderTreeTable(tableView); + that.renderTreeTable(tableView); - if (layui.type(done) === 'function') { + if (layui.type(done) === 'function') { return done.apply(doneThat, args); } } @@ -641,7 +641,7 @@ layui.define(['table'], function (exports) { if (layui.type(expandFlag) !== 'boolean') { return hint.error('expandAll的展开状态参数只接收true/false') } - + var that = getThisTable(id); if(!that) return; @@ -950,7 +950,7 @@ layui.define(['table'], function (exports) { // 处理setRowChecked obj.setRowChecked = function (checked) { treeTable.setRowChecked(tableId, { - index: trData, + index: trData, checked: checked }); } @@ -1042,7 +1042,7 @@ layui.define(['table'], function (exports) { var tableViewElem = options.elem.next(); opts = opts || {}; - + var parentIndex = opts.parentIndex; var index = opts.index; var newNodes = opts.data; @@ -1259,9 +1259,9 @@ layui.define(['table'], function (exports) { var options = obj.config; var tableView = options.elem.next(); var tableId = options.id; - var that = getThisTable(tableId); if (tableView.hasClass('layui-table-tree')) { + var that = getThisTable(tableId); updateObjParams(obj); checkNode.call(that, obj.tr, obj.checked) } @@ -1482,7 +1482,7 @@ layui.define(['table'], function (exports) { if (!trElem.length) { // 如果还没有展开没有渲染的要先渲染出来 treeTable.expandNode(id, { - index: nodeData[LAY_PARENT_INDEX], + index: nodeData[LAY_PARENT_INDEX], expandFlag: true }); trElem = tableView.find('tr[lay-data-index="' + dataIndex + '"]'); From 378fb2cf285ed0ebf58a27b48574e51491056bcc Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <470459819@qq.com> Date: Wed, 3 May 2023 18:32:50 +0800 Subject: [PATCH 10/16] =?UTF-8?q?=E4=BC=98=E5=8C=96=20treeTable=20?= =?UTF-8?q?=E8=AE=A9=E5=A4=8D=E9=80=89=E7=9A=84=E5=8D=8A=E9=80=89=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E5=9B=BE=E6=A0=87=E6=9B=B4=E5=8A=A0=E6=98=8E=E6=98=BE?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9E=E5=B1=82=E7=BA=A7=E9=97=B4=E9=80=89?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E8=83=8C=E6=99=AF=E9=A2=9C=E8=89=B2=E5=8C=BA?= =?UTF-8?q?=E5=88=AB=E8=BE=85=E5=8A=A9=E5=8C=BA=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/css/layui.css | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/css/layui.css b/src/css/layui.css index 2f3df254..ed7bab24 100644 --- a/src/css/layui.css +++ b/src/css/layui.css @@ -1119,6 +1119,13 @@ body .layui-table-tips .layui-layer-content{background: none; padding: 0; box-sh .layui-table-tree-nodeIcon > * {width: 100%;} .layui-table-tree-flexIcon,.layui-table-tree-nodeIcon {margin-right: 2px;} .layui-table-tree-flexIcon {cursor: pointer;} +.layui-table-tree [name="layTableCheckbox"] + div.layui-form-checkbox[lay-skin="primary"] .layui-icon-indeterminate:before {height: 2px;} +.layui-table-tree tr[data-level] [name="layTableCheckbox"] + div.layui-form-checked[lay-skin="primary"] i {background-color: #c8f6d4;border-color: #c8f6d4 !important;} +.layui-table-tree tr[data-level="0"] [name="layTableCheckbox"] + div.layui-form-checked[lay-skin="primary"] i {background-color: #16b777;border-color: #16b777 !important;} +.layui-table-tree tr[data-level="1"] [name="layTableCheckbox"] + div.layui-form-checked[lay-skin="primary"] i {background-color: #23ca81;border-color: #23ca81 !important;} +.layui-table-tree tr[data-level="2"] [name="layTableCheckbox"] + div.layui-form-checked[lay-skin="primary"] i {background-color: #34e089;border-color: #34e089 !important;} +.layui-table-tree tr[data-level="3"] [name="layTableCheckbox"] + div.layui-form-checked[lay-skin="primary"] i {background-color: #48fa98;border-color: #48fa98 !important;} +.layui-table-tree tr[data-level="4"] [name="layTableCheckbox"] + div.layui-form-checked[lay-skin="primary"] i {background-color: #87e2bd;border-color: #87e2bd !important;} /** 文件上传 **/ .layui-upload-file{display: none!important; opacity: .01; filter: Alpha(opacity=1);} From 6f25d2f2e5993a58f1509b10d8e4d0413c6ea727 Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <470459819@qq.com> Date: Wed, 3 May 2023 18:38:03 +0800 Subject: [PATCH 11/16] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20treeTable=20?= =?UTF-8?q?=E8=A1=A5=E5=85=85=20`getOptions`=20=E5=92=8C=20`hideCol`=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/treeTable.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index 6ac59942..30ddecb5 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -24,14 +24,16 @@ layui.define(['table'], function (exports) { that.config = $.extend({}, that.config, options); return that; }, - resize: table.resize + resize: table.resize, + getOptions: table.getOptions, + hideCol: table.hideCol }; // 操作当前实例 var thisTreeTable = function () { - var that = this - , options = that.config - , id = options.id || options.index; + var that = this; + var options = that.config + var id = options.id || options.index; return { config: options, From 3709b74bfe290883cf5beaa9f1af3ebfe3b871ee Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <470459819@qq.com> Date: Wed, 3 May 2023 18:41:42 +0800 Subject: [PATCH 12/16] =?UTF-8?q?=E4=BC=98=E5=8C=96=20treeTable=20?= =?UTF-8?q?=E5=B0=86=20`rootId`=20=E7=A7=BB=E5=88=B0=20tree.data=20?= =?UTF-8?q?=E4=B8=AD=EF=BC=8C=E5=B9=B6=E4=B8=94=E6=94=B9=E5=90=8D=E4=B8=BA?= =?UTF-8?q?=E6=9B=B4=E5=A5=BD=E7=90=86=E8=A7=A3=E7=9A=84=20`rootPId`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/treeTable.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index 30ddecb5..fa5b3e07 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -216,9 +216,8 @@ layui.define(['table'], function (exports) { children: "children", // 节点数据中保存子节点数据的属性名称 isParent: "isParent", // 节点数据保存节点是否为父节点的属性名称 name: "name", // 节点数据保存节点名称的属性名称 - id: "id", // 唯一标识的属性名称 - pid: "parentId", - rootId: null + id: "id", // 唯一标识的属性名称 + pid: "parentId", // 父节点唯一标识的属性名称 }, view: { indent: 14, // 层级缩进量 @@ -233,7 +232,8 @@ layui.define(['table'], function (exports) { dblClickExpand: true, // 双击节点时,是否自动展开父节点的标识 }, data: { - isSimpleData: false // 是否简单数据模式 + isSimpleData: false, // 是否简单数据模式 + rootPId: null // 根节点的父 ID 值 }, async: { enable: false, // 是否开启异步加载模式,只有开启的时候其他参数才起作用 From 585f1c430118e33c79115145b62b9da9b33d8ef4 Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <470459819@qq.com> Date: Wed, 3 May 2023 18:56:02 +0800 Subject: [PATCH 13/16] =?UTF-8?q?=E4=BC=98=E5=8C=96=20treeTable=20?= =?UTF-8?q?=E5=86=85=E9=83=A8=20`formatNumber`=20=E7=9A=84=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E7=BB=86=E8=8A=82=E5=87=8F=E5=B0=91=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/treeTable.js | 53 +++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index fa5b3e07..7e924057 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -10,6 +10,7 @@ layui.define(['table'], function (exports) { var form = layui.form; var table = layui.table; var hint = layui.hint(); + var timer = {}; // 记录定时器 index // api var treeTable = { @@ -119,6 +120,19 @@ layui.define(['table'], function (exports) { var treeOptions = thatOptionsTemp.tree; var childrenKey = treeOptions.customName.children; // 处理属性 + delete options.hasNumberCol; + delete options.hasChecboxCol; + delete options.hasRadioCol; + table.eachCols(null, function (i1, item1) { + if (item1.type === 'numbers') { + options.hasNumberCol = true; + } else if (item1.type === 'checkbox') { + options.hasChecboxCol = true; + } else if (item1.type === 'radio') { + options.hasRadioCol = true; + } + }, thatOptionsTemp.cols) + var parseData = options.parseData; var done = options.done; @@ -788,33 +802,31 @@ layui.define(['table'], function (exports) { expandNode({trElem: trDefaultExpand.first()}, true); }); - treeTable.formatNumber(tableId); + options.hasNumberCol && that.formatNumber(tableId); form.render(null, tableFilterId); } - - /** - * 重新编号 - * @param {String} id 表格id - * @return {Object} layui.treeTable - * */ - treeTable.formatNumber = function (id) { - var that = getThisTable(id); - if(!that) return; - + var formatNumber = function (that) { var options = that.getOptions(); var tableViewElem = options.elem.next(); var num = 0; - layui.each(that.treeToFlat(table.cache[id]), function (i1, item1) { + layui.each(that.treeToFlat(table.cache[options.id]), function (i1, item1) { if (layui.isArray(item1)) { return; } - var itemData = that.getNodeDataByIndex(item1.LAY_DATA_INDEX); + var itemData = that.getNodeDataByIndex(item1[LAY_DATA_INDEX]); itemData['LAY_NUM'] = ++num; - tableViewElem.find('tr[lay-data-index="' + item1.LAY_DATA_INDEX + '"] .laytable-cell-numbers').html(itemData['LAY_NUM']); + tableViewElem.find('tr[lay-data-index="' + item1[LAY_DATA_INDEX] + '"] .laytable-cell-numbers').html(itemData['LAY_NUM']); }) - return treeTable; + } + + Class.prototype.formatNumber = function (id) { + var that = this; + clearTimeout(id); + timer[id] = setTimeout(function () { + formatNumber(that); + }, 10) } // 树表渲染 @@ -1023,16 +1035,17 @@ layui.define(['table'], function (exports) { layui.each(table.cache[id], function (i4, item4) { tableView.find('tr[data-level="0"][lay-data-index="' + item4[LAY_DATA_INDEX] + '"]').attr('data-index', i4); }) - treeTable.formatNumber(id); + options.hasNumberCol && that.formatNumber(id); } /** * 新增数据节点 * @param {String} id 树表id - * @param {String|Number} parentIndex 指定的父节点,如果增加根节点,请设置 parentIndex 为 null 即可 - * @param {Number} index:Number 新节点插入的位置(从 0 开始)index = -1(默认) 时,插入到最后 - * @param {Object|Array} data 新增的节点,单个或者多个 - * @param {Boolean} focus 新增的节点,单个或者多个 + * @param {Object} opts + * @param {String|Number} opts.parentIndex 指定的父节点,如果增加根节点,请设置 parentIndex 为 null 即可 + * @param {Number} opts.index 新节点插入的位置(从 0 开始)index = -1(默认) 时,插入到最后 + * @param {Object|Array} opts.data 新增的节点,单个或者多个 + * @param {Boolean} opts.focus 新增的节点,单个或者多个 * @return {Array} 新增的节点 * */ treeTable.addNodes = function (id, opts) { From 075cca611b186bdcc5604c4ec3fd79937424ef31 Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <470459819@qq.com> Date: Wed, 3 May 2023 19:00:18 +0800 Subject: [PATCH 14/16] =?UTF-8?q?=E5=AE=8C=E5=96=84=20treeTable=20`expandA?= =?UTF-8?q?ll`=20=E5=AE=9E=E7=8E=B0=E5=B1=95=E5=BC=80=E6=89=80=E6=9C=89?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/treeTable.js | 117 ++++++++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 38 deletions(-) diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index 7e924057..31cc1cc1 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -646,16 +646,19 @@ layui.define(['table'], function (exports) { var sonSign = opts.inherit; var callbackFlag = opts.callbackFlag; - var options = that.getOptions(); var tableViewElem = options.elem.next(); return expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + index + '"]').first()}, expandFlag, sonSign, null, callbackFlag) }; - // 目前还有性能问题特别是在data模式需要优化暂时不能使用 todo + /** + * 展开或关闭全部节点 + * @param {String} id 树表id + * @param {Boolean} expandFlag 展开或关闭 + * */ treeTable.expandAll = function (id, expandFlag) { if (layui.type(expandFlag) !== 'boolean') { - return hint.error('expandAll的展开状态参数只接收true/false') + return hint.error('expandAll 的展开状态参数只接收true/false') } var that = getThisTable(id); @@ -670,62 +673,100 @@ layui.define(['table'], function (exports) { // 将所有已经打开的节点的状态设置为关闭, that.updateStatus(null, {LAY_EXPAND: false}); // 只处理当前页,如果需要处理全部表格,需要用treeTable.updateStatus // 隐藏所有非顶层的节点 - tableView.find('.layui-table-box tbody tr[data-level!="0"]').addClass('layui-hide'); + tableView.find('.layui-table-box tbody tr[data-level!="0"]').addClass(HIDE); // 处理顶层节点的图标 var trLevel0 = tableView.find('tbody tr[data-level="0"]'); trLevel0.find('.layui-table-tree-flexIcon').html(treeOptions.view.flexIconClose); treeOptions.view.showIcon && trLevel0.find('.layui-table-tree-nodeIcon').html(treeOptions.view.iconClose); - treeTable.resize(id); } else { - return hint.error('暂不支持展开全部'); - + var tableDataFlat = treeTable.getData(id, true); // 展开所有 + // 存在异步加载 if (treeOptions.async.enable) { - // 存在异步加载 - - } else { - // 先判断是否全部打开过了 - var tableDataFlat = treeTable.getData(id, true); - var isAllExpanded = true; + // 判断是否有未加载过的节点 + var isAllAsyncDone = true; + var isParentKey = treeOptions.customName.isParent; layui.each(tableDataFlat, function (i1, item1) { - if (!item1[LAY_HAS_EXPANDED]) { - isAllExpanded = false; + if (item1[isParentKey] && !item1[LAY_ASYNC_STATUS]) { + isAllAsyncDone = false; return true; } }) - // 如果全部节点已经都打开过,就可以简单处理跟隐藏所有节点反操作 - if (isAllExpanded) { - that.updateStatus(null, {LAY_EXPAND: true}); // 只处理当前页,如果需要处理全部表格,需要用treeTable.updateStatus - // 隐藏所有非顶层的节点 - tableView.find('tbody tr[data-level!="0"]').removeClass('layui-hide'); - // 处理顶层节点的图标 - // var trLevel0 = tableView.find('tbody tr[data-level="0"]'); - tableView.find('.layui-table-tree-flexIcon').html(treeOptions.view.flexIconOpen); - tableView.find('.layui-table-tree-nodeIcon').html(treeOptions.view.iconOpen); - - treeTable.resize(); - } else { - // 如果有未打开过的父节点,将内容全部生成 - that.updateStatus(null, {LAY_EXPAND: true, LAY_HAS_EXPANDED: true}); - var trsAll = table.getTrHtml(id, tableDataFlat); - + // 有未加载过的节点 + if (!isAllAsyncDone) { + // 逐个展开 + layui.each(treeTable.getData(id), function (i1, item1) { + treeTable.expandNode(id, { + index: item1[LAY_DATA_INDEX], + expandFlag: true, + inherit: true + }) + }) + return; } - } + // 先判断是否全部打开过了 + var isAllExpanded = true; + layui.each(tableDataFlat, function (i1, item1) { + if (!item1[LAY_HAS_EXPANDED]) { + isAllExpanded = false; + return true; + } + }) + // 如果全部节点已经都打开过,就可以简单处理跟隐藏所有节点反操作 + if (isAllExpanded) { + that.updateStatus(null, {LAY_EXPAND: true}); + // 隐藏所有非顶层的节点 + tableView.find('tbody tr[data-level!="0"]').removeClass(HIDE); + // 处理顶层节点的图标 + // var trLevel0 = tableView.find('tbody tr[data-level="0"]'); + tableView.find('.layui-table-tree-flexIcon').html(treeOptions.view.flexIconOpen); + tableView.find('.layui-table-tree-nodeIcon').html(treeOptions.view.iconOpen); + } else { + // 如果有未打开过的父节点,将内容全部生成 + that.updateStatus(null, {LAY_EXPAND: true, LAY_HAS_EXPANDED: true}); + var trAll = table.getTrHtml(id, tableDataFlat); - // 如果是异步加载子节点模式 如何处理 - // 如果有部分节点未打开过,也需要重新 + var trAllObj = { + trs: $(trAll.trs.join('')), + trs_fixed: $(trAll.trs_fixed.join('')), + trs_fixed_r: $(trAll.trs_fixed_r.join('')) + } + layui.each(tableDataFlat, function (dataIndex, dataItem) { + // debugger; + var dataLevel = dataItem[LAY_DATA_INDEX].split('-').length - 1; + trAllObj.trs.eq(dataIndex).attr({ + 'data-index': dataItem[LAY_DATA_INDEX], + 'lay-data-index': dataItem[LAY_DATA_INDEX], + 'data-level': dataLevel + }) + trAllObj.trs_fixed.eq(dataIndex).attr({ + 'data-index': dataItem[LAY_DATA_INDEX], + 'lay-data-index': dataItem[LAY_DATA_INDEX], + 'data-level': dataLevel + }) + trAllObj.trs_fixed_r.eq(dataIndex).attr({ + 'data-index': dataItem[LAY_DATA_INDEX], + 'lay-data-index': dataItem[LAY_DATA_INDEX], + 'data-level': dataLevel + }) + }) + layui.each(['main', 'fixed-l', 'fixed-r'], function (i, item) { + tableView.find('.layui-table-' + item + ' tbody').html(trAllObj[['trs', 'trs_fixed', 'trs_fixed_r'][i]]); + }); + that.renderTreeTable(tableView, 0, false); + } } - + treeTable.resize(id); } Class.prototype.renderTreeTable = function (tableView, level, sonSign) { var that = this; var options = that.getOptions(); var tableViewElem = options.elem.next(); - tableViewElem.addClass('layui-table-tree'); + tableViewElem.addClass(TABLE_TREE); var tableId = options.id; var treeOptions = options.tree || {}; var treeOptionsData = treeOptions.data || {}; @@ -775,10 +816,10 @@ layui.define(['table'], function (exports) { var flexIconElem = item.find('div.layui-table-cell') .html(['
', - treeOptions.view.flexIconClose, // 折叠图标 + trData[LAY_EXPAND] ? treeOptions.view.flexIconOpen : treeOptions.view.flexIconClose, // 折叠图标 '
', '
', treeOptions.view.showIcon ? (trData.icon || treeOptions.view.icon || (trData[isParentKey] ? treeOptions.view.iconClose : treeOptions.view.iconLeaf) || '') : '', From b7760e305529be52f2c89dd24481f8977e5d01ba Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <470459819@qq.com> Date: Wed, 3 May 2023 19:01:25 +0800 Subject: [PATCH 15/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20treeTable=20?= =?UTF-8?q?=E8=A1=8C=E5=86=85=E8=8F=9C=E5=8D=95=E4=BA=8B=E4=BB=B6=E4=B8=AD?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=B2=A1=E6=9C=89=E5=8A=A0=E5=B7=A5=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/treeTable.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index 31cc1cc1..0681827f 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -1284,13 +1284,24 @@ layui.define(['table'], function (exports) { } }) + // 菜单 + treeTable.on('rowContextmenu', function (obj) { + var options = obj.config; + var tableView = options.elem.next(); + var tableId = options.id; + + if (tableView.hasClass(TABLE_TREE)) { + updateObjParams(obj); + } + }) + // tr中带lay-event节点点击 treeTable.on('tool', function (obj) { var options = obj.config; var tableView = options.elem.next(); var tableId = options.id; - if (tableView.hasClass('layui-table-tree')) { + if (tableView.hasClass(TABLE_TREE)) { updateObjParams(obj); } }) From f1a060671b1563a917a80a16ec21185c53b92cbd Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <470459819@qq.com> Date: Wed, 3 May 2023 19:03:37 +0800 Subject: [PATCH 16/16] =?UTF-8?q?=E5=AE=8C=E5=96=84=20treeTable=20?= =?UTF-8?q?=E8=A7=84=E6=95=B4=E4=BA=86=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=8E=BB=E6=8E=89=E4=B8=80=E4=BA=9B=E6=B2=A1=E6=9C=89=E7=94=A8?= =?UTF-8?q?=E5=88=B0=E7=9A=84=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/treeTable.js | 121 +++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 63 deletions(-) diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index 0681827f..fadf3d31 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -60,38 +60,28 @@ layui.define(['table'], function (exports) { } // 字符 - var MOD_NAME = 'treeTable' - , ELEMTREE = '.layui-table-tree' - , THIS = 'layui-this' - , SHOW = 'layui-show' - , HIDE = 'layui-hide' - , HIDE_V = 'layui-hide-v' - , DISABLED = 'layui-disabled' - , NONE = 'layui-none' + var MOD_NAME = 'treeTable'; + var HIDE = 'layui-hide'; - , ELEM_VIEW = '.layui-table-view' - , ELEM_TOOL = '.layui-table-tool' - , ELEM_BOX = '.layui-table-box' - , ELEM_INIT = '.layui-table-init' - , ELEM_HEADER = '.layui-table-header' - , ELEM_BODY = '.layui-table-body' - , ELEM_MAIN = '.layui-table-main' - , ELEM_FIXED = '.layui-table-fixed' - , ELEM_FIXL = '.layui-table-fixed-l' - , ELEM_FIXR = '.layui-table-fixed-r' - , ELEM_TOTAL = '.layui-table-total' - , ELEM_PAGE = '.layui-table-page' - , ELEM_SORT = '.layui-table-sort' - , ELEM_EDIT = 'layui-table-edit' - , ELEM_HOVER = 'layui-table-hover' - , ELEM_GROUP = 'laytable-cell-group' + var ELEM_VIEW = '.layui-table-view'; + var ELEM_TREE = '.layui-table-tree'; + var ELEM_TOOL = '.layui-table-tool'; + var ELEM_BOX = '.layui-table-box'; + var ELEM_HEADER = '.layui-table-header'; + var ELEM_BODY = '.layui-table-body'; + var ELEM_MAIN = '.layui-table-main'; + var ELEM_FIXED = '.layui-table-fixed'; + var ELEM_FIXL = '.layui-table-fixed-l'; + var ELEM_FIXR = '.layui-table-fixed-r'; + var TABLE_TREE = 'layui-table-tree'; var LAY_DATA_INDEX = 'LAY_DATA_INDEX'; var LAY_DATA_INDEX_HISTORY = 'LAY_DATA_INDEX_HISTORY'; var LAY_PARENT_INDEX = 'LAY_PARENT_INDEX'; var LAY_CHECKBOX_HALF = 'LAY_CHECKBOX_HALF'; var LAY_EXPAND = 'LAY_EXPAND'; var LAY_HAS_EXPANDED = 'LAY_HAS_EXPANDED'; + var LAY_ASYNC_STATUS = 'LAY_ASYNC_STATUS'; // 构造器 var Class = function (options) { @@ -215,11 +205,6 @@ layui.define(['table'], function (exports) { thisTreeTable.that[id] = that; // 记录当前实例对象 that.tableIns = tableIns; - // var treeOptions = options.tree; - // var customName = treeOptions.customName; - // var isParentKey = customName.isParent; - // var childrenKey = customName.children; - updateOptions(id, options); } @@ -306,7 +291,7 @@ layui.define(['table'], function (exports) { tableData = tableData || table.cache[tableId]; - return flatToTree(tableData, customName.id, customName.pid, customName.children, customName.rootId) + return flatToTree(tableData, customName.id, customName.pid, customName.children, treeOptions.data.rootPId) } Class.prototype.treeToFlat = function (tableData, parentId, parentIndex) { @@ -445,7 +430,7 @@ layui.define(['table'], function (exports) { var trExpand = isToggle ? !trData[LAY_EXPAND] : expandFlag; var retValue = trData[isParentKey] ? trExpand : null; - if (callbackFlag && trExpand != trData[LAY_EXPAND] && (!trData['LAY_ASYNC_STATUS'] || trData['LAY_ASYNC_STATUS'] === 'local')) { + if (callbackFlag && trExpand != trData[LAY_EXPAND] && (!trData[LAY_ASYNC_STATUS] || trData[LAY_ASYNC_STATUS] === 'local')) { var beforeExpand = treeOptions.callback.beforeExpand; if (layui.type(beforeExpand) === 'function') { if (beforeExpand(tableId, trData, expandFlag) === false) { @@ -476,22 +461,22 @@ layui.define(['table'], function (exports) { trData[LAY_EXPAND] = trExpand; tableViewElem.find(childNodes.map(function (value, index, array) { return 'tr[lay-data-index="' + value[LAY_DATA_INDEX] + '"]' - }).join(',')).removeClass('layui-hide'); + }).join(',')).removeClass(HIDE); layui.each(childNodes, function (i1, item1) { if (sonSign && !isToggle) { // 非状态切换的情况下 // 级联展开子节点 - expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + item1.LAY_DATA_INDEX + '"]').first()}, expandFlag, sonSign, focus, callbackFlag); + expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + item1[LAY_DATA_INDEX] + '"]').first()}, expandFlag, sonSign, focus, callbackFlag); } else if (item1[LAY_EXPAND]) { // 级联展开 - expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + item1.LAY_DATA_INDEX + '"]').first()}, true); + expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + item1[LAY_DATA_INDEX] + '"]').first()}, true); } }) } else { var asyncSetting = treeOptions.async || {}; var asyncUrl = asyncSetting.url || options.url; // 提供一个能支持用户在获取子数据转换调用的回调,这样让子节点数据获取更加灵活 todo - if (asyncSetting.enable && asyncUrl && !trData['LAY_ASYNC_STATUS']) { - trData['LAY_ASYNC_STATUS'] = 'loading'; + if (asyncSetting.enable && trData[isParentKey] && asyncUrl && !trData[LAY_ASYNC_STATUS]) { + trData[LAY_ASYNC_STATUS] = 'loading'; var params = {}; // 参数 var data = $.extend(params, asyncSetting.where || options.where); @@ -516,22 +501,22 @@ layui.define(['table'], function (exports) { // that.loading(); flexIconElem.html('') $.ajax({ - type: asyncType || 'get' - , url: asyncUrl - , contentType: asyncContentType - , data: data - , dataType: asyncDataType || 'json' - , jsonpCallback: asyncJsonpCallback - , headers: asyncHeaders || {} - , success: function (res) { - trData['LAY_ASYNC_STATUS'] = 'success'; + type: asyncType || 'get', + url: asyncUrl, + contentType: asyncContentType, + data: data, + dataType: asyncDataType || 'json', + jsonpCallback: asyncJsonpCallback, + headers: asyncHeaders || {}, + success: function (res) { + trData[LAY_ASYNC_STATUS] = 'success'; // 若有数据解析的回调,则获得其返回的数据 if (typeof asyncParseData === 'function') { res = asyncParseData.call(options, res) || res; } // 检查数据格式是否符合规范 if (res[asyncResponse.statusName] != asyncResponse.statusCode) { - trData['LAY_ASYNC_STATUS'] = 'error'; + trData[LAY_ASYNC_STATUS] = 'error'; // 异常处理 todo flexIconElem.html(''); // 事件 @@ -541,9 +526,9 @@ layui.define(['table'], function (exports) { // 正常返回 expandNode(treeNode, true, isToggle ? false : sonSign, focus, callbackFlag); } - } - , error: function (e, msg) { - trData['LAY_ASYNC_STATUS'] = 'error'; + }, + error: function (e, msg) { + trData[LAY_ASYNC_STATUS] = 'error'; // 异常处理 todo typeof options.error === 'function' && options.error(e, msg); } @@ -602,7 +587,7 @@ layui.define(['table'], function (exports) { if (sonSign && !isToggle) { // 非状态切换的情况下 // 级联展开/关闭子节点 layui.each(childNodes, function (i1, item1) { - expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + item1.LAY_DATA_INDEX + '"]').first()}, expandFlag, sonSign, focus, callbackFlag); + expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + item1[LAY_DATA_INDEX] + '"]').first()}, expandFlag, sonSign, focus, callbackFlag); }) } } @@ -612,22 +597,22 @@ layui.define(['table'], function (exports) { // 折叠 if (sonSign && !isToggle) { // 非状态切换的情况下 layui.each(childNodes, function (i1, item1) { - expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + item1.LAY_DATA_INDEX + '"]').first()}, expandFlag, sonSign, focus, callbackFlag); + expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + item1[LAY_DATA_INDEX] + '"]').first()}, expandFlag, sonSign, focus, callbackFlag); }); tableViewElem.find(childNodes.map(function (value, index, array) { // 只隐藏直接子节点,其他由递归的处理 return 'tr[lay-data-index="' + value[LAY_DATA_INDEX] + '"]' - }).join(',')).addClass('layui-hide'); + }).join(',')).addClass(HIDE); } else { var childNodesFlat = treeTableThat.treeToFlat(childNodes, trData[customName.id], trIndex); tableViewElem.find(childNodesFlat.map(function (value, index, array) { return 'tr[lay-data-index="' + value[LAY_DATA_INDEX] + '"]' - }).join(',')).addClass('layui-hide'); + }).join(',')).addClass(HIDE); } } table.resize(tableId); - if (callbackFlag && trData['LAY_ASYNC_STATUS'] !== 'loading') { + if (callbackFlag && trData[LAY_ASYNC_STATUS] !== 'loading') { var onExpand = treeOptions.callback.onExpand; layui.type(onExpand) === 'function' && onExpand(tableId, trData, expandFlag); } @@ -635,9 +620,19 @@ layui.define(['table'], function (exports) { return retValue; } + /** + * 展开或关闭一个节点 + * @param {String} id 树表id + * @param {Object} opts + * @param {Number|String} opts.index 展开行的数据下标 + * @param {Boolean} [opts.expandFlag] 展开、关闭、切换 + * @param {Boolean} [opts.inherit] 是否级联子节点 + * @param {Boolean} [opts.callbackFlag] 是否触发事件 + * @return [{Boolean}] 状态结果 + * */ treeTable.expandNode = function (id, opts) { var that = getThisTable(id); - if(!that) return; + if (!that) return; opts = opts || {}; @@ -1210,7 +1205,7 @@ layui.define(['table'], function (exports) { tableData = that.initData(); // 去掉父节点的已经展开过的状态,重新执行一次展开的方法 parentNode[LAY_HAS_EXPANDED] = false; - parentNode['LAY_ASYNC_STATUS'] = 'local'; // 转为本地数据,应该规定异步加载子节点的时候addNodes的规则 + parentNode[LAY_ASYNC_STATUS] = 'local'; // 转为本地数据,应该规定异步加载子节点的时候addNodes的规则 expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + parentIndex + '"]')}, true) } that.updateCheckStatus(parentNode, true); @@ -1253,7 +1248,7 @@ layui.define(['table'], function (exports) { var tableView = options.elem.next(); var tableId = options.id; - if (tableView.hasClass('layui-table-tree')) { + if (tableView.hasClass(TABLE_TREE)) { treeTable.sort(tableId); } }); @@ -1263,7 +1258,7 @@ layui.define(['table'], function (exports) { var options = obj.config; var tableView = options.elem.next(); - if (tableView.hasClass('layui-table-tree')) { + if (tableView.hasClass(TABLE_TREE)) { updateObjParams(obj); } }) @@ -1274,7 +1269,7 @@ layui.define(['table'], function (exports) { var tableView = options.elem.next(); var tableId = options.id; - if (tableView.hasClass('layui-table-tree')) { + if (tableView.hasClass(TABLE_TREE)) { updateObjParams(obj); var treeOptions = options.tree || {}; @@ -1313,7 +1308,7 @@ layui.define(['table'], function (exports) { var tableView = options.elem.next(); var tableId = options.id; - if (tableView.hasClass('layui-table-tree')) { + if (tableView.hasClass(TABLE_TREE)) { updateObjParams(obj); if (obj.field === options.tree.customName.name) { obj.update({}); // 通过update调用执行tr节点的更新 @@ -1327,7 +1322,7 @@ layui.define(['table'], function (exports) { var tableView = options.elem.next(); var tableId = options.id; - if (tableView.hasClass('layui-table-tree')) { + if (tableView.hasClass(TABLE_TREE)) { var that = getThisTable(tableId); updateObjParams(obj); checkNode.call(that, obj.tr, obj.checked) @@ -1508,7 +1503,7 @@ layui.define(['table'], function (exports) { var tableView = options.elem.next(); var tableId = options.id; - if (tableView.hasClass('layui-table-tree')) { + if (tableView.hasClass(TABLE_TREE)) { var that = getThisTable(tableId); var checked = obj.checked; updateObjParams(obj) @@ -1570,7 +1565,7 @@ layui.define(['table'], function (exports) { /** * 获得数据 * @param {String} id 表格id - * @param {Boolean} isSimpleData 是否返回平铺结构的数据 + * @param {Boolean} [isSimpleData] 是否返回平铺结构的数据 * @return {Array} 表格数据 * */ treeTable.getData = function (id, isSimpleData) {