From 2c784041d8016a8e86891a05bda3cd0a9b5b05c8 Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <285584806@qq.com> Date: Fri, 14 Apr 2023 15:04:59 +0800 Subject: [PATCH] =?UTF-8?q?form=E9=BB=98=E8=AE=A4=E7=9A=AE=E8=82=A4?= =?UTF-8?q?=E7=9A=84=E5=A4=8D=E9=80=89=E6=A1=86=E6=B7=BB=E5=8A=A0=E5=8D=8A?= =?UTF-8?q?=E9=80=89=E5=8A=9F=E8=83=BD=E6=94=AF=E6=8C=81=EF=BC=9B=E4=BF=AE?= =?UTF-8?q?=E5=A4=8DtreeTable=E7=9A=84=E4=B8=80=E4=BA=9B=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/css/layui.css | 40 ++++----- src/modules/form.js | 9 ++- src/modules/treeTable.js | 170 ++++++++++++++++++++++++++------------- 3 files changed, 143 insertions(+), 76 deletions(-) diff --git a/src/css/layui.css b/src/css/layui.css index 8e828ddc..68de0302 100644 --- a/src/css/layui.css +++ b/src/css/layui.css @@ -55,7 +55,7 @@ a cite{font-style: normal; *cursor:pointer;} url('../font/iconfont.ttf?v=280') format('truetype'), url('../font/iconfont.svg?v=280#layui-icon') format('svg'); } - + .layui-icon{ font-family:"layui-icon" !important; font-size: 16px; @@ -367,7 +367,7 @@ a cite{font-style: normal; *cursor:pointer;} .layui-show-md-block{display: block!important;} .layui-show-md-inline{display: inline!important;} .layui-show-md-inline-block{display: inline-block!important;} - + .layui-col-md1, .layui-col-md2, .layui-col-md3, .layui-col-md4, .layui-col-md5, .layui-col-md6, .layui-col-md7, .layui-col-md8, .layui-col-md9, .layui-col-md10, .layui-col-md11, .layui-col-md12{float: left;} .layui-col-md1{width: 8.33333333%;} .layui-col-md2{width: 16.66666667%;} @@ -402,7 +402,7 @@ a cite{font-style: normal; *cursor:pointer;} .layui-show-lg-block{display: block!important;} .layui-show-lg-inline{display: inline!important;} .layui-show-lg-inline-block{display: inline-block!important;} - + .layui-col-lg1, .layui-col-lg2, .layui-col-lg3, .layui-col-lg4, .layui-col-lg5, .layui-col-lg6, .layui-col-lg7, .layui-col-lg8, .layui-col-lg9, .layui-col-lg10, .layui-col-lg11, .layui-col-lg12{float: left;} .layui-col-lg1{width: 8.33333333%;} .layui-col-lg2{width: 16.66666667%;} @@ -437,7 +437,7 @@ a cite{font-style: normal; *cursor:pointer;} .layui-show-xl-block{display: block!important;} .layui-show-xl-inline{display: inline!important;} .layui-show-xl-inline-block{display: inline-block!important;} - + .layui-col-xl1, .layui-col-xl2, .layui-col-xl3, .layui-col-xl4, .layui-col-xl5, .layui-col-xl6, .layui-col-xl7, .layui-col-xl8, .layui-col-xl9, .layui-col-xl10, .layui-col-xl11, .layui-col-xl12{float: left;} .layui-col-xl1{width: 8.33333333%;} .layui-col-xl2{width: 16.66666667%;} @@ -532,7 +532,7 @@ a cite{font-style: normal; *cursor:pointer;} /* 面板 - + */ @@ -566,8 +566,8 @@ a cite{font-style: normal; *cursor:pointer;} .layui-scollbar-hide{overflow: hidden !important;} -/* - * 默认主题 +/* + * 默认主题 */ @@ -582,11 +582,11 @@ a cite{font-style: normal; *cursor:pointer;} /* 边框 */ .layui-border, -.layui-quote-nm, +.layui-quote-nm, .layui-elem-field, -.layui-collapse, +.layui-collapse, .layui-panel, -.layui-colla-item, +.layui-colla-item, .layui-colla-content, .layui-badge-rim, .layui-tab-title, @@ -596,7 +596,7 @@ a cite{font-style: normal; *cursor:pointer;} .layui-input, .layui-textarea, .layui-select, .layui-input-split, -.layui-form-pane .layui-form-label, +.layui-form-pane .layui-form-label, .layui-form-pane .layui-form-item[pane]{border-color: #eee;} .layui-border{border-width: 1px; border-style: solid; color: #5F5F5F!important;} @@ -663,8 +663,8 @@ a cite{font-style: normal; *cursor:pointer;} /* - - 按钮 + + 按钮 */ @@ -716,7 +716,7 @@ a cite{font-style: normal; *cursor:pointer;} .layui-input:focus, .layui-textarea:focus{border-color: #d2d2d2 !important;} .layui-textarea{position: relative; min-height: 100px; height: auto; line-height: 20px; padding: 6px 10px; resize: vertical;} .layui-select{padding: 0 10px;} -.layui-form select, +.layui-form select, .layui-form input[type=checkbox], .layui-form input[type=radio]{display: none;} .layui-form *[lay-ignore]{display: initial;} @@ -831,6 +831,8 @@ a cite{font-style: normal; *cursor:pointer;} .layui-form-checked.layui-checkbox-disabled[lay-skin="primary"] i{background: #eee!important; border-color: #eee!important;} .layui-checkbox-disabled[lay-skin="primary"]:hover i{border-color: #d2d2d2;} .layui-form-item .layui-form-checkbox[lay-skin="primary"]{margin-top: 10px;} +.layui-form-checkbox[lay-skin="primary"] > i.layui-icon-subtraction, +.layui-form-checkbox[lay-skin="primary"]:hover i.layui-icon-subtraction {color: darkgray;} /* 复选框-开关风格 */.layui-form-switch{position: relative; display: inline-block; vertical-align: middle; height: 22px; line-height: 22px; min-width: 35px; padding: 0 5px; margin-top: 8px; border: 1px solid #d2d2d2; border-radius: 20px; cursor: pointer; background-color: #fff; -webkit-transition: .1s linear; transition: .1s linear;} .layui-form-switch i{position: absolute; left: 5px; top: 3px; width: 16px; height: 16px; border-radius: 20px; background-color: #d2d2d2; -webkit-transition: .1s linear; transition: .1s linear;} @@ -865,7 +867,7 @@ a cite{font-style: normal; *cursor:pointer;} .layui-form-pane .layui-form-text .layui-input-block{margin: 0; left: 0; top: -1px;} .layui-form-pane .layui-form-text .layui-textarea{min-height: 100px; border-radius: 0 0 2px 2px;} .layui-form-pane .layui-form-checkbox{margin: 4px 0 4px 10px;} -.layui-form-pane .layui-form-switch, +.layui-form-pane .layui-form-switch, .layui-form-pane .layui-form-radio{margin-top: 6px; margin-left: 10px; } .layui-form-pane .layui-form-item[pane]{position: relative; border-width: 1px; border-style: solid;} .layui-form-pane .layui-form-item[pane] .layui-form-label{position: absolute; left: 0; top: 0; height: 100%; border-width: 0px; border-right-width: 1px;} @@ -891,7 +893,7 @@ a cite{font-style: normal; *cursor:pointer;} .layui-laypage>*:last-child{margin-right: 0!important;} .layui-laypage a, .layui-laypage span, -.layui-laypage input, +.layui-laypage input, .layui-laypage button, .layui-laypage select{border: 1px solid #eee;} .layui-laypage a, @@ -1239,7 +1241,7 @@ body .layui-table-tips .layui-layer-content{background: none; padding: 0; box-sh .layui-nav-tree .layui-nav-child a:hover, .layui-nav-tree .layui-nav-child{background: none; color: #fff;} -.layui-nav-itemed>.layui-nav-child{display: block; background-color: rgba(0,0,0,.3) !important;} +.layui-nav-itemed>.layui-nav-child{display: block; background-color: rgba(0,0,0,.3) !important;} .layui-nav-itemed>.layui-nav-child>.layui-this>.layui-nav-child{display: block;} /* 侧边 */.layui-nav-side{position: fixed; top: 0; bottom: 0; left: 0; overflow-x: hidden; z-index: 999;} @@ -1480,7 +1482,7 @@ body .layui-util-face .layui-layer-content{padding:0; background-color:#fff; co .layui-slider-wrap{width: 36px; height: 36px; position: absolute; top: -16px; -webkit-transform: translateX(-50%); transform: translateX(-50%); z-index: 10; text-align: center;} .layui-slider-wrap-btn{width: 12px; height: 12px; border-radius: 50%; background: #FFF; display: inline-block; vertical-align: middle; cursor: pointer; transition: 0.3s;} .layui-slider-wrap:after{content: ""; height: 100%; display: inline-block; vertical-align: middle;} -.layui-slider-wrap-btn:hover, +.layui-slider-wrap-btn:hover, .layui-slider-wrap-btn.layui-slider-hover{transform: scale(1.2);} .layui-slider-wrap-btn.layui-disabled:hover{transform: scale(1) !important;} .layui-slider-tips{position: absolute; top: -42px; z-index: 77777777; white-space:nowrap; display: none; -webkit-transform: translateX(-50%); transform: translateX(-50%); color: #FFF; background: #000; border-radius: 3px; height: 25px; line-height: 25px; padding: 0 10px;} @@ -1645,7 +1647,7 @@ body .layui-util-face .layui-layer-content{padding:0; background-color:#fff; co .layui-anim-fadein{-webkit-animation-name: layui-fadein; animation-name: layui-fadein;} /* 渐隐 */ -@-webkit-keyframes layui-fadeout { +@-webkit-keyframes layui-fadeout { 0% {opacity: 1;} 100% {opacity: 0;} } diff --git a/src/modules/form.js b/src/modules/form.js index db472973..778dde2e 100644 --- a/src/modules/form.js +++ b/src/modules/form.js @@ -642,7 +642,12 @@ layui.define(['lay', 'layer', 'util'], function(exports){ var title = (check.attr('title')||'').split('|'); if(check[0].disabled) return; - + if (check[0].indeterminate) { + check[0].indeterminate = false; + reElem.find('.layui-icon-subtraction').removeClass('layui-icon-subtraction').addClass('layui-icon-ok') + } + + check[0].checked ? ( check[0].checked = false ,reElem.removeClass(RE_CLASS[1]).find('em').text(title[1]) @@ -686,7 +691,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){ // 复选框 "checkbox": [ (title[0] ? (''+ util.escape(title[0]) +'') : '') - ,'' + ,'' ].join(''), // 开关 diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index ff5c56d9..2c179790 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -85,6 +85,7 @@ layui.define(['table'], function (exports) { 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 Class = function (options) { @@ -149,6 +150,17 @@ layui.define(['table'], function (exports) { that.initData(options.data); } + var updateCache = function (data) { + var tableCache = table.cache[id]; + layui.each(data || tableCache, function (index, item) { + var itemDataIndex = item[LAY_DATA_INDEX]; + if (itemDataIndex.indexOf('-') !== -1) { + tableCache[itemDataIndex] = item + } + item[childrenKey] && updateCache(item[childrenKey]); + }) + } + options.done = function () { var args = arguments; var doneThat = this; @@ -156,7 +168,10 @@ layui.define(['table'], function (exports) { var tableView = this.elem.next(); that.updateStatus(null, { LAY_HAS_EXPANDED: false // 去除已经打开过的状态 - }) + }); + // 更新cache中的内容 将子节点也存到cache中 + updateCache(); + that.renderTreeTable(tableView); if (layui.type(done) === 'function') { @@ -349,7 +364,6 @@ layui.define(['table'], function (exports) { item1[LAY_DATA_INDEX_HISTORY] = item1[LAY_DATA_INDEX]; item1[LAY_PARENT_INDEX] = parentIndex = parentIndex || ''; var dataIndex = item1[LAY_DATA_INDEX] = (parentIndex ? parentIndex + '-' : '') + i1; - dataIndex.indexOf('-') !== -1 && (table.cache[tableId][dataIndex] = item1); that.initData(item1[childrenKey] || [], dataIndex); }); @@ -365,7 +379,7 @@ layui.define(['table'], function (exports) { var options = table.getOptions(tableId); var treeOptions = options.tree || {}; var isParentKey = treeOptions.data.key.isParent; - var trIndex = trElem.attr('data-index'); // 可能出现多层 + var trIndex = trElem.attr('lay-data-index'); // 可能出现多层 var treeTableThat = getThisTable(tableId); var tableData = treeTableThat.getTableData(); @@ -391,8 +405,8 @@ layui.define(['table'], function (exports) { var LAY_HAS_EXPANDED = trData['LAY_HAS_EXPANDED']; // 展开过,包括异步加载 - // 找到表格中的同类节点(需要找到data-index一致的所有行) - var trsElem = tableViewElem.find('tr[data-index="' + trIndex + '"]'); + // 找到表格中的同类节点(需要找到lay-data-index一致的所有行) + var trsElem = tableViewElem.find('tr[lay-data-index="' + trIndex + '"]'); // 处理折叠按钮图标 var flexIconElem = trsElem.find('.layui-table-tree-flexIcon'); flexIconElem.html(LAY_EXPAND ? treeOptions.view.flexIconOpen : treeOptions.view.flexIconClose) @@ -413,15 +427,15 @@ layui.define(['table'], function (exports) { trData['LAY_EXPAND'] = LAY_EXPAND; tableViewElem.find(childNodes.map(function (value, index, array) { - return 'tr[data-index="' + value[LAY_DATA_INDEX] + '"]' + return 'tr[lay-data-index="' + value[LAY_DATA_INDEX] + '"]' }).join(',')).removeClass('layui-hide'); layui.each(childNodes, function (i1, item1) { if (sonSign && !isToggle) { // 非状态切换的情况下 // 级联展开子节点 - expandNode({trElem: tableViewElem.find('tr[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[data-index="' + item1.LAY_DATA_INDEX + '"]').first()}, true); + expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + item1.LAY_DATA_INDEX + '"]').first()}, true); } }) } else { @@ -511,20 +525,26 @@ layui.define(['table'], function (exports) { trs_fixed_r: $(str2.trs_fixed_r.join('')) } layui.each(childNodes, function (childIndex, childItem) { - str2Obj.trs.eq(childIndex).attr({'data-index': childItem[LAY_DATA_INDEX], 'data-level': dataLevelNew}) + str2Obj.trs.eq(childIndex).attr({ + 'data-index': childItem[LAY_DATA_INDEX], + 'lay-data-index': childItem[LAY_DATA_INDEX], + 'data-level': dataLevelNew + }) str2Obj.trs_fixed.eq(childIndex).attr({ 'data-index': childItem[LAY_DATA_INDEX], + 'lay-data-index': childItem[LAY_DATA_INDEX], 'data-level': dataLevelNew }) str2Obj.trs_fixed_r.eq(childIndex).attr({ 'data-index': childItem[LAY_DATA_INDEX], + 'lay-data-index': childItem[LAY_DATA_INDEX], 'data-level': dataLevelNew }) }) - tableViewElem.find(ELEM_MAIN).find('tbody tr[data-index="' + trIndex + '"]').after(str2Obj.trs); - tableViewElem.find(ELEM_FIXL).find('tbody tr[data-index="' + trIndex + '"]').after(str2Obj.trs_fixed); - tableViewElem.find(ELEM_FIXR).find('tbody tr[data-index="' + trIndex + '"]').after(str2Obj.trs_fixed_r); + tableViewElem.find(ELEM_MAIN).find('tbody tr[lay-data-index="' + trIndex + '"]').after(str2Obj.trs); + tableViewElem.find(ELEM_FIXL).find('tbody tr[lay-data-index="' + trIndex + '"]').after(str2Obj.trs_fixed); + tableViewElem.find(ELEM_FIXR).find('tbody tr[lay-data-index="' + trIndex + '"]').after(str2Obj.trs_fixed_r); // 初始化新增的节点中的内容 layui.each(str2Obj, function (key, item) { @@ -534,7 +554,7 @@ layui.define(['table'], function (exports) { if (sonSign && !isToggle) { // 非状态切换的情况下 // 级联展开/关闭子节点 layui.each(childNodes, function (i1, item1) { - expandNode({trElem: tableViewElem.find('tr[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); }) } } @@ -546,15 +566,15 @@ layui.define(['table'], function (exports) { // 折叠 if (sonSign && !isToggle) { // 非状态切换的情况下 layui.each(childNodes, function (i1, item1) { - expandNode({trElem: tableViewElem.find('tr[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[data-index="' + value[LAY_DATA_INDEX] + '"]' + return 'tr[lay-data-index="' + value[LAY_DATA_INDEX] + '"]' }).join(',')).addClass('layui-hide'); } else { var childNodesFlat = treeTableThat.treeToFlat(childNodes, trData[treeOptions.data.simpleData.idKey], trIndex); tableViewElem.find(childNodesFlat.map(function (value, index, array) { - return 'tr[data-index="' + value[LAY_DATA_INDEX] + '"]' + return 'tr[lay-data-index="' + value[LAY_DATA_INDEX] + '"]' }).join(',')).addClass('layui-hide'); } @@ -573,7 +593,7 @@ layui.define(['table'], function (exports) { var that = getThisTable(id); var options = that.getOptions(); var tableViewElem = options.elem.next(); - return expandNode({trElem: tableViewElem.find('tr[data-index="' + index + '"]').first()}, expandFlag, sonSign, null, callbackFlag) + return expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + index + '"]').first()}, expandFlag, sonSign, null, callbackFlag) } // 目前还有性能问题特别是在data模式需要优化暂时不能使用 todo @@ -606,9 +626,9 @@ layui.define(['table'], function (exports) { // 初始化的表格里面没有level信息,可以作为顶层节点的判断 tableViewElem.find('.layui-table-body tr:not([data-level])').attr('data-level', level); layui.each(table.cache[tableId], function (dataIndex, dataItem) { - tableViewElem.find('.layui-table-main tbody tr[data-level="0"]:eq(' + dataIndex + ')').attr('data-index', dataItem[LAY_DATA_INDEX]); - tableViewElem.find('.layui-table-fixed-l tbody tr[data-level="0"]:eq(' + dataIndex + ')').attr('data-index', dataItem[LAY_DATA_INDEX]); - tableViewElem.find('.layui-table-fixed-r tbody tr[data-level="0"]:eq(' + dataIndex + ')').attr('data-index', dataItem[LAY_DATA_INDEX]); + tableViewElem.find('.layui-table-main tbody tr[data-level="0"]:eq(' + dataIndex + ')').attr('lay-data-index', dataItem[LAY_DATA_INDEX]); + tableViewElem.find('.layui-table-fixed-l tbody tr[data-level="0"]:eq(' + dataIndex + ')').attr('lay-data-index', dataItem[LAY_DATA_INDEX]); + tableViewElem.find('.layui-table-fixed-r tbody tr[data-level="0"]:eq(' + dataIndex + ')').attr('lay-data-index', dataItem[LAY_DATA_INDEX]); }) } @@ -624,7 +644,7 @@ layui.define(['table'], function (exports) { return; } itemCell.addClass('layui-table-tree-item'); - var trIndex = trElem.attr('data-index'); + var trIndex = trElem.attr('lay-data-index'); if (!trIndex) { // 排除在统计行中的节点 return; } @@ -662,7 +682,7 @@ layui.define(['table'], function (exports) { // 当前层的数据看看是否需要展开 sonSign !== false && layui.each(dataExpand, function (index, item) { - var trDefaultExpand = tableViewElem.find('tr[data-index="' + index + '"]'); + var trDefaultExpand = tableViewElem.find('tr[lay-data-index="' + index + '"]'); trDefaultExpand.find('.layui-table-tree-flexIcon').html(treeOptions.view.flexIconOpen); expandNode({trElem: trDefaultExpand.first()}, true); }); @@ -689,7 +709,7 @@ layui.define(['table'], function (exports) { } var itemData = that.getNodeDataByIndex(item1.LAY_DATA_INDEX); itemData['LAY_NUM'] = ++num; - tableViewElem.find('tr[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; } @@ -826,7 +846,9 @@ layui.define(['table'], function (exports) { var options = that.getOptions(); var treeOptions = options.tree; var tableView = options.elem.next(); - var trLevel = tableView.find('tr[data-index="' + index + '"]').attr('data-level') + var trElem = tableView.find('tr[lay-data-index="' + index + '"]'); + var trIndex = trElem.attr('data-index'); + var trLevel = trElem.attr('data-level') if (!newNode) { return; @@ -835,11 +857,15 @@ layui.define(['table'], function (exports) { var newNodeTemp = that.getNodeDataByIndex(index, false, newNode); // 获取新的tr替换 var trNew = table.getTrHtml(id, [newNodeTemp]); + debugger; // 重新渲染tr layui.each(['main', 'fixed-l', 'fixed-r'], function (i, item) { - tableView.find('.layui-table-' + item + ' tbody tr[data-index="' + index + '"]').replaceWith($(trNew[['trs', 'trs_fixed', 'trs_fixed_r'][i]].join('')).attr('data-index', index)); + 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, + })); }); - that.renderTreeTable(tableView.find('tr[data-index="' + index + '"]'), trLevel); + that.renderTreeTable(tableView.find('tr[lay-data-index="' + index + '"]'), trLevel); } // 删除数据 @@ -857,20 +883,27 @@ layui.define(['table'], function (exports) { delNode = that.getNodeDataByIndex(indexValue, false, 'delete'); var delNodesFlat = that.treeToFlat([delNode], delNode[treeOptions.data.simpleData.pIdKey], delNode[LAY_PARENT_INDEX]); layui.each(delNodesFlat, function (i2, item2) { - indexArr.push('tr[data-index="' + item2[LAY_DATA_INDEX] + '"]'); + indexArr.push('tr[lay-data-index="' + item2[LAY_DATA_INDEX] + '"]'); }) }); tableView.find(indexArr.join(',')).remove(); // 删除行 // 重新整理数据 var tableData = that.initData(); - // index发生变化需要更新页面tr中对应的data-index 新增和删除都要注意数据结构变动之后的index问题 + // index发生变化需要更新页面tr中对应的lay-data-index 新增和删除都要注意数据结构变动之后的index问题 layui.each(that.treeToFlat(tableData), function (i3, item3) { if (item3[LAY_DATA_INDEX_HISTORY] && item3[LAY_DATA_INDEX_HISTORY] !== item3[LAY_DATA_INDEX]) { - tableView.find('tr[data-index="' + item3[LAY_DATA_INDEX_HISTORY] + '"]').attr('data-index', item3[LAY_DATA_INDEX]); - item3[LAY_DATA_INDEX_HISTORY] = item3[LAY_DATA_INDEX]; + tableView.find('tr[lay-data-index="' + item3[LAY_DATA_INDEX_HISTORY] + '"]').attr({ + 'data-index': item3[LAY_DATA_INDEX], + 'lay-data-index': item3[LAY_DATA_INDEX], + }); + // item3[LAY_DATA_INDEX_HISTORY] = item3[LAY_DATA_INDEX] } }); + // 重新更新顶层节点的data-index; + 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); } @@ -920,13 +953,19 @@ layui.define(['table'], function (exports) { } layui.each(newNodes, function (newNodeIndex, newNodeItem) { - newNodesHtmlObj.trs.eq(newNodeIndex).attr({'data-index': newNodeItem[LAY_DATA_INDEX], 'data-level': '0'}) + newNodesHtmlObj.trs.eq(newNodeIndex).attr({ + 'data-index': newNodeItem[LAY_DATA_INDEX], + 'lay-data-index': newNodeItem[LAY_DATA_INDEX], + 'data-level': '0' + }) newNodesHtmlObj.trs_fixed.eq(newNodeIndex).attr({ 'data-index': newNodeItem[LAY_DATA_INDEX], + 'lay-data-index': newNodeItem[LAY_DATA_INDEX], 'data-level': '0' }) newNodesHtmlObj.trs_fixed_r.eq(newNodeIndex).attr({ 'data-index': newNodeItem[LAY_DATA_INDEX], + 'lay-data-index': newNodeItem[LAY_DATA_INDEX], 'data-level': '0' }) }) @@ -954,14 +993,13 @@ layui.define(['table'], function (exports) { } - // 更新编号 - // layui.each(tableData, function (i1, item1) { - // tableViewElemMAIN.find('tr[data-level="0"]').eq(i1).attr('data-index', item1[LAY_DATA_INDEX]); - // tableViewElemFIXL.find('tr[data-level="0"]').eq(i1).attr('data-index', item1[LAY_DATA_INDEX]); - // tableViewElemFIXR.find('tr[data-level="0"]').eq(i1).attr('data-index', item1[LAY_DATA_INDEX]); - // }) + // 重新更新顶层节点的data-index; + layui.each(table.cache[id], function (i4, item4) { + tableViewElem.find('tr[data-level="0"][lay-data-index="' + item4[LAY_DATA_INDEX] + '"]').attr('data-index', i4); + }) + that.renderTreeTable(tableViewElem.find(newNodes.map(function (value, index, array) { - return 'tr[data-index="' + value[LAY_DATA_INDEX] + '"]' + return 'tr[lay-data-index="' + value[LAY_DATA_INDEX] + '"]' }).join(','))); } else { var isParentKey = treeOptions.data.key.isParent; @@ -983,14 +1021,14 @@ layui.define(['table'], function (exports) { }); var childrenNodesFlat = that.treeToFlat(childrenNodes); tableViewElem.find(childrenNodesFlat.map(function (value) { - return 'tr[data-index="' + value[LAY_DATA_INDEX] + '"]' + return 'tr[lay-data-index="' + value[LAY_DATA_INDEX] + '"]' }).join(',')).remove(); tableData = that.initData(); // 去掉父节点的已经展开过的状态,重新执行一次展开的方法 parentNode['LAY_HAS_EXPANDED'] = false; parentNode['LAY_ASYNC_STATUS'] = 'local'; // 转为本地数据,应该规定异步加载子节点的时候addNodes的规则 - expandNode({trElem: tableViewElem.find('tr[data-index="' + parentIndex + '"]')}, true) + expandNode({trElem: tableViewElem.find('tr[lay-data-index="' + parentIndex + '"]')}, true) } return newNodes; } @@ -1099,24 +1137,36 @@ layui.define(['table'], function (exports) { } }) + // 更新父节点的选中状态 Class.prototype.updateParentCheckStatus = function (dataP, checked) { var that = this; var options = that.getOptions(); var treeOptions = options.tree; var tableId = options.id; + var checkName = table.config.checkName; + var childrenKey = treeOptions.data.key.children; var dataRet = []; + dataP[LAY_CHECKBOX_HALF] = false; // 先设置为非半选,是否为半选又下面逻辑判断 if (checked) { // 为真需要判断子节点的情况 - var childrenKey = treeOptions.data.key.children; layui.each(dataP[childrenKey], function (index, item) { - if (!item[table.config.checkName]) { // 只要有一个子节点为false + if (!item[checkName]) { // 只要有一个子节点为false checked = false; + dataP[LAY_CHECKBOX_HALF] = true; return true; // 跳出循环 } }); + } else { + // 判断是否为半选 + layui.each(dataP[childrenKey], function (index, item) { + if (item[checkName] || item[LAY_CHECKBOX_HALF]) { // 只要有一个子节点为选中或者半选状态 + dataP[LAY_CHECKBOX_HALF] = true; + return true; + } + }); } - dataP[table.config.checkName] = checked; + dataP[checkName] = checked; dataRet.push($.extend({}, dataP)); if (dataP[LAY_PARENT_INDEX]) { dataRet = dataRet.concat(that.updateParentCheckStatus(table.cache[tableId][dataP[LAY_PARENT_INDEX]], checked)); @@ -1138,41 +1188,51 @@ layui.define(['table'], function (exports) { var isParentKey = treeOptions.data.key.isParent; var childrenKey = treeOptions.data.key.children; var trIndex = obj.index; + var checkName = table.config.checkName; // 修改当前节点的信息 - var trData = table.cache[tableId][trIndex]; - // trData[table.config.checkName] = checked; - // tableView.find('tr[data-index="' + trIndex + '"] input[name="layTableCheckbox"]').prop('checked', checked); + var trData = that.getNodeDataByIndex(trIndex); + // trData[checkName] = checked; // 如果是一个父节点,将子节点的状态同步为当前节点的状态,并且注意更深层次的 if (obj.type === 'all' || trData[isParentKey]) { var checkedStatus = {}; - checkedStatus[table.config.checkName] = checked; + checkedStatus[checkName] = checked; + checkedStatus[LAY_CHECKBOX_HALF] = false; var trs = that.updateStatus(trData ? [trData] : table.cache[tableId], checkedStatus); form.render(tableView.find(trs.map(function (value) { - return 'tr[data-index="' + value[LAY_DATA_INDEX] + '"] input[name="layTableCheckbox"]'; - }).join(',')).prop('checked', checked)); + return 'tr[lay-data-index="' + value[LAY_DATA_INDEX] + '"] input[name="layTableCheckbox"]'; + }).join(',')).prop({checked: checked, indeterminate: false})); } // 更新父节点以及更上层节点的状态 if (trData && trData[LAY_PARENT_INDEX]) { // 找到父节点,然后判断父节点的子节点是否全部选中 - var trDataP = table.cache[tableId][trData[LAY_PARENT_INDEX]]; + var trDataP = that.getNodeDataByIndex(trData[LAY_PARENT_INDEX]); var trsP = that.updateParentCheckStatus(trDataP, checked); layui.each(trsP, function (indexP, itemP) { - form.render(tableView.find('tr[data-index="' + itemP[LAY_DATA_INDEX] + '"] input[name="layTableCheckbox"]').prop('checked', itemP[table.config.checkName])) + form.render(tableView.find('tr[lay-data-index="' + itemP[LAY_DATA_INDEX] + '"] input[name="layTableCheckbox"]').prop({ + checked: itemP[checkName], + indeterminate: itemP[LAY_CHECKBOX_HALF] + })) }) } - // 半选复选框的图标处理 todo - // 全选图标的状态更新 obj.isAll = true; + var isIndeterminate = false; layui.each(table.cache[tableId], function (i1, item1) { - if (!item1[table.config.checkName]) { + if (item1[checkName] || item1[LAY_CHECKBOX_HALF]) { + isIndeterminate = true; + } + if (!item1[checkName]) { obj.isAll = false; return true; } }) - form.render(tableView.find('input[name="layTableCheckbox"][lay-filter="layTableAllChoose"]').prop('checked', obj.isAll)); + isIndeterminate = isIndeterminate && !obj.isAll; + form.render(tableView.find('input[name="layTableCheckbox"][lay-filter="layTableAllChoose"]').prop({ + 'checked': obj.isAll, + indeterminate: isIndeterminate + })); } })