From cba735435ad89dda15963f3838c601736a2d220d Mon Sep 17 00:00:00 2001 From: Tony-ST0754 <6914529@qq.com> Date: Wed, 16 Sep 2026 16:23:03 +0800 Subject: [PATCH 1/9] =?UTF-8?q?test:=E5=9B=BA=E5=AE=9A=E5=88=97=E6=8B=96?= =?UTF-8?q?=E5=8A=A8=E5=88=97=E5=AE=BD=E6=B5=8B=E8=AF=95=EF=BC=8C=E8=BF=98?= =?UTF-8?q?=E6=9C=89=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit test:固定列拖动列宽测试,还有问题,拖动列宽时其他列宽也会跟着变,固定列宽调大后,再由大拖动变小,拖动时有点困难 --- src/BootstrapBlazor/Components/Table/Table.razor.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.cs b/src/BootstrapBlazor/Components/Table/Table.razor.cs index 6ce02dd3986..15c5d27c57a 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.cs @@ -215,10 +215,12 @@ private string GetSortTooltip(ITableColumn col) => SortName != col.GetFieldName( private string? GetHeaderStyleString(ITableColumn col, int margin = 0) { var fixedStyle = GetFixedCellStyleString(col, margin); - var widthStyle = col.Width is > 0 + var columnState = _tableColumnStateCache.Columns.FirstOrDefault(x => x.Name == col.GetFieldName()); + var width = columnState?.Width ?? col.Width; + var widthStyle = width is > 0 ? AllowResizing - ? $"width: {col.Width}px;" - : $"width: {col.Width}px; min-width: {col.Width}px;" + ? $"width: {width}px;" + : $"width: {width}px; min-width: {width}px;" : null; if (fixedStyle == null) { @@ -1609,7 +1611,7 @@ private async Task OnTableRenderAsync(bool firstRender) } // 计算实际宽度 - var width = _tableColumnStateCache.TableWidth; + var width = _tableColumnStateCache.Columns.Sum(x => x.Width); var tableWidth = hasHeader ? width : width - ActualScrollWidth; return $"width: {tableWidth}px;"; From d69e43628383519f635099048d6bfd3eb2ba0b83 Mon Sep 17 00:00:00 2001 From: Tony-ST0754 <6914529@qq.com> Date: Thu, 17 Sep 2026 10:00:30 +0800 Subject: [PATCH 2/9] =?UTF-8?q?refactor:=E8=A1=A5=E5=85=85=E6=8B=96?= =?UTF-8?q?=E5=8A=A8=E5=88=97=E5=AE=BD=E5=AD=98=E7=9A=84=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E5=B7=A6=E5=8F=B3=E5=9B=BA=E5=AE=9A=E5=88=97=E5=B7=B2=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refactor:补充拖动列宽存的题,左右固定列已测试 --- .../Components/Table/Table.razor.js | 117 ++++++++++++------ 1 file changed, 78 insertions(+), 39 deletions(-) diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.js b/src/BootstrapBlazor/Components/Table/Table.razor.js index a257c1fd4d4..554e393e3cd 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.js +++ b/src/BootstrapBlazor/Components/Table/Table.razor.js @@ -467,7 +467,9 @@ const setResizeListener = table => { } let colWidth = 0 - let tableWidth = 0 + let colMinWidth = 5 + let columnWidths = [] + let tableWidths = [] let colIndex = 0 let originalX = 0 @@ -489,53 +491,76 @@ const setResizeListener = table => { const tableEl = col.closest('table') const currentCol = tableEl.querySelectorAll('colgroup col')[colIndex] const width = currentCol.style.width - if (width) { - colWidth = parseInt(width) + const th = getColumnHeader(col); + const isFixedLayout = getComputedStyle(tableEl).tableLayout === 'fixed'; + if (isFixedLayout) { + colWidth = width ? parseInt(width) : getResizableColumnWidth(col); + colMinWidth = Math.max(table.options.columnMinWidth || 0, 5); } else { colWidth = getResizableColumnWidth(col); + colMinWidth = Math.max( + getCellWidth(th) + getHeaderIconsWidth(th), + getColumnMaxCellWidth(table, colIndex), + table.options.columnMinWidth || 0 + ) | 0; } - tableWidth = getWidth(tableEl); + const headerCells = [...th.parentElement.children]; + columnWidths = [...table.tables[0].querySelectorAll('colgroup col')].map((col, index) => { + const width = parseFloat(col.style.width); + return !isNaN(width) ? width : getWidth(headerCells[index]); + }); + tableWidths = table.tables.map(t => getWidth(t)); originalX = e.clientX ?? e.touches[0].clientX }, e => { const eventX = e.clientX ?? e.changedTouches[0].clientX const marginX = eventX - originalX - table.tables.forEach(t => { + const calcColWidth = Math.max(colWidth + marginX, colMinWidth); + const actualDelta = calcColWidth - colWidth; + columnWidths[colIndex] = calcColWidth; + table.tables.forEach((t, index) => { const group = [...t.children].find(i => i.nodeName === 'COLGROUP') - let calcColWidth = colWidth + marginX; - if (calcColWidth < 5) { - calcColWidth = 5; - } if (group) { const curCol = group.children.item(colIndex) - curCol.style.setProperty('width', `${calcColWidth}px`); - const tableEl = curCol.closest('table') - let width = tableWidth + marginX - if (t.closest('.table-fixed-body')) { - width = width - table.scrollWidth; + if (curCol) { + curCol.style.setProperty('width', `${calcColWidth}px`); + const tableEl = curCol.closest('table') + const width = tableWidths[index] + actualDelta; + tableEl.style.setProperty('width', `${width}px`); + } + } + }); + + table.tables.forEach(t => { + t.querySelectorAll('thead > tr').forEach(row => { + const th = row.children.item(colIndex); + if (th) { + th.style.setProperty('width', `${calcColWidth}px`); } - tableEl.setAttribute('style', `width: ${width}px;`) + }); + }); - resetColumnWidthTips(table, col); + resetColumnWidthTips(table, col); - const header = getColumnHeader(col); - if (header.classList.contains('fixed')) { - resizeNextFixedColumnWidth(header, getWidth(header)); + const header = getColumnHeader(col); + if (header.classList.contains('fixed')) { + resizeNextFixedColumnWidth(header, calcColWidth, columnWidths, colIndex); + table.tables.forEach(t => { + const tbody = [...t.children].find(i => i.nodeName === 'TBODY'); + if (!tbody) { + return; } - } - const tbody = [...t.children].find(i => i.nodeName === 'TBODY'); - if (tbody) { const rows = [...tbody.children].filter(i => i.nodeName === 'TR'); rows.forEach(row => { - const header = row.children.item(colIndex); - if (header !== null && header.classList.contains('fixed')) { - resizeNextFixedColumnWidth(header, getWidth(header)); + const cell = row.children.item(colIndex); + if (cell?.classList.contains('fixed')) { + resizeNextFixedColumnWidth(cell, calcColWidth, columnWidths, colIndex); } }); - } - }) + }); + } }, () => { eff(col, false) @@ -550,21 +575,29 @@ const setResizeListener = table => { }) } -const resizeNextFixedColumnWidth = (col, width) => { +const resizeNextFixedColumnWidth = (col, width, columnWidths, colIndex) => { if (col.classList.contains('fixed-right')) { const nextColumn = col.previousElementSibling; - if (nextColumn.classList.contains('fixed')) { - const right = parseFloat(col.style.getPropertyValue('right')); + if (nextColumn?.classList.contains('fixed')) { + const nextIndex = colIndex - 1; + const right = parseFloat(col.style.getPropertyValue('right')) || 0; nextColumn.style.setProperty('right', `${right + width}px`); - resizeNextFixedColumnWidth(nextColumn, getWidth(nextColumn)); + const nextWidth = columnWidths[nextIndex]; + if (nextWidth !== void 0) { + resizeNextFixedColumnWidth(nextColumn, nextWidth, columnWidths, nextIndex); + } } } else if (col.classList.contains('fixed')) { const nextColumn = col.nextElementSibling; - if (nextColumn.classList.contains('fixed')) { - const left = parseFloat(col.style.getPropertyValue('left')); + if (nextColumn?.classList.contains('fixed')) { + const nextIndex = colIndex + 1; + const left = parseFloat(col.style.getPropertyValue('left')) || 0; nextColumn.style.setProperty('left', `${left + width}px`); - resizeNextFixedColumnWidth(nextColumn, getWidth(nextColumn)); + const nextWidth = columnWidths[nextIndex]; + if (nextWidth !== void 0) { + resizeNextFixedColumnWidth(nextColumn, nextWidth, columnWidths, nextIndex); + } } } } @@ -1018,7 +1051,7 @@ const getColumnStateObject = table => { cols: cols.map(col => { return { name: col.name, - width: getColumnWidth(col, table.columns), + width: getColumnWidth(table, col, table.columns), visible: col.visible } }), @@ -1030,7 +1063,7 @@ const getColumnStateObject = table => { cols: table.columns.map(col => { return { name: getColumnName(col), - width: getResizableColumnWidth(col), + width: getColumnStyleWidth(table, col), visible: true } }), @@ -1038,11 +1071,10 @@ const getColumnStateObject = table => { }; } -const getColumnWidth = (col, columns) => { +const getColumnWidth = (table, col, columns) => { const column = columns.find(i => getColumnName(i) === col.name); if (column) { - const width = getResizableColumnWidth(column); - return width > 0 ? width : null; + return getColumnStyleWidth(table, column); } else if (col.width) { return col.width; @@ -1052,6 +1084,13 @@ const getColumnWidth = (col, columns) => { } } +const getColumnStyleWidth = (table, col) => { + const index = indexOfCol(col); + const colEl = table.tables[0].querySelectorAll('colgroup col')[index]; + const width = parseFloat(colEl?.style.width); + return !isNaN(width) && width > 0 ? width : getResizableColumnWidth(col); +} + const getTableWidth = table => { let tableWidth = 0; const colgroup = [...table.children].find(i => i.nodeName === 'COLGROUP'); From 57081a06cbe0b79afec678d288ea8001490a7bb9 Mon Sep 17 00:00:00 2001 From: Tony-ST0754 <6914529@qq.com> Date: Thu, 17 Sep 2026 11:14:26 +0800 Subject: [PATCH 3/9] =?UTF-8?q?refactor:=E5=B0=86=E6=89=80=E6=9C=89?= =?UTF-8?q?=E9=9D=9E=E7=94=A8=E6=88=B7=E6=95=B0=E6=8D=AE=E5=88=97=E7=9A=84?= =?UTF-8?q?th=E4=B8=8Astyle=E9=87=8C=E9=9D=A2=E5=8A=A0=E4=B8=8Awidth?= =?UTF-8?q?=E5=80=BC=EF=BC=8C=E4=BB=A5=E4=BF=9D=E8=AF=81=E8=A1=A8=E6=A0=BC?= =?UTF-8?q?=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refactor:将所有非用户数据列的th上style里面加上width值,以保证表格显示 refactor:将表格宽度的值计算全部由表格包含的全部列的值的总合,以保持表格宽度与总列宽一致,免避免出现各种奇怪问题 --- .../Components/Table/Table.razor | 18 +++---- .../Components/Table/Table.razor.Sort.cs | 12 +++-- .../Components/Table/Table.razor.cs | 54 +++++++++++++++++-- 3 files changed, 69 insertions(+), 15 deletions(-) diff --git a/src/BootstrapBlazor/Components/Table/Table.razor b/src/BootstrapBlazor/Components/Table/Table.razor index 5b6ef0c9ba9..8da63bd4d0e 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor +++ b/src/BootstrapBlazor/Components/Table/Table.razor @@ -380,7 +380,7 @@ } @if (hasScroll) { -