From ffefdea23dd3d501479cccbc3d748e72164ac85a Mon Sep 17 00:00:00 2001 From: sight <26325820+Sight-wcg@users.noreply.github.com> Date: Fri, 5 Sep 2025 21:00:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(table):=20=E4=BF=AE=E5=A4=8D=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=A1=8C=E9=AB=98=E6=97=B6=E5=9B=BA=E5=AE=9A=E5=88=97?= =?UTF-8?q?=E9=AB=98=E5=BA=A6=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/table.js | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/modules/table.js b/src/modules/table.js index d851eae3..44f780d0 100644 --- a/src/modules/table.js +++ b/src/modules/table.js @@ -460,6 +460,7 @@ layui.define(['lay', 'i18n', 'laytpl', 'laypage', 'form', 'util'], function(expo that.pullData(that.page); // 请求数据 that.events(); // 事件 + that.autoResize(); }; // 根据列类型,定制化参数 @@ -1522,6 +1523,11 @@ layui.define(['lay', 'i18n', 'laytpl', 'laypage', 'form', 'util'], function(expo that.haveInit = true; layer.close(that.tipsIndex); + + // reloadData 或 renderData 时,tbody 高度可能不变,需要主动同步 + if(that.needSyncFixedColHeight && (opts.type === 'reloadData' || opts.type === 'renderData')){ + that.syncFixedColHeight(); + } }; table.cache[that.key] = data; //记录数据 @@ -2922,6 +2928,57 @@ layui.define(['lay', 'i18n', 'laytpl', 'laypage', 'form', 'util'], function(expo } }; + Class.prototype.createResizeObserver = function(){ + var that = this; + if(that.resizeObserver){ + that.resizeObserver.disconnect(); + that.resizeObserver = null; + } + that.resizeObserver = lay._createResizeObserver('table-' + that.config.id); + } + + Class.prototype.syncFixedColHeight = function(){ + var that = this; + + var tableElem = that.layMain.children('table'); + var leftTrs = that.layFixLeft.find('.layui-table-body>table>tbody>tr'); + var rightTrs = that.layFixRight.find('.layui-table-body>table>tbody>tr'); + + tableElem.find('>tbody>tr').each(function (i) { + if (leftTrs.length) { + leftTrs.eq(i).height(that.getElementSize(this).height); + } + if (rightTrs.length) { + rightTrs.eq(i).height(that.getElementSize(this).height); + } + }); + } + + Class.prototype.autoResize = function(){ + var that = this; + + that.createResizeObserver(); + + if(!that.resizeObserver) return; + + // 显示或隐藏时重置列宽 + that.resizeObserver.observe(that.elem[0], function(){ + that.resize(); + }); + + // 同步固定列表格和主表格高度 + that.needSyncFixedColHeight = (that.layBody.length > 1) && + that.config.lineStyle && + (/\bheight\s*:\s*auto\b/g.test(that.config.lineStyle) || that.config.lineStyle.indexOf('max-height') !== -1); + + if(that.needSyncFixedColHeight){ + var tableElem = that.layMain.children('table'); + that.resizeObserver.observe(tableElem[0], function () { + that.syncFixedColHeight(); + }); + } + }; + // 全局事件 (function(){ // 自适应尺寸