Skip to content

Commit 5d49073

Browse files
ArgoZhangCopilot
andauthored
feat(Table): add auto width function (#8126)
* feat: 增加客户端未设置列宽列自适应功能 * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Argo Zhang <argo@live.ca> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Argo Zhang <argo@live.ca> * chore: bump version 10.7.2 --------- Signed-off-by: Argo Zhang <argo@live.ca> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 7e9c00c commit 5d49073

4 files changed

Lines changed: 98 additions & 11 deletions

File tree

‎src/BootstrapBlazor/BootstrapBlazor.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>10.7.2-beta04</Version>
4+
<Version>10.7.2</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

‎src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,12 @@ private async Task QueryAsync(bool shouldRender, int? pageIndex = null, bool tri
516516

517517
if (shouldRender)
518518
{
519+
// 数据渲染后触发脚本测量列内容最小宽度 用于固定表头(table-layout:fixed)窄容器下横向滚动兜底
520+
if (IsFixedHeader)
521+
{
522+
_measureColumnMinWidth = true;
523+
_invoke = true;
524+
}
519525
StateHasChanged();
520526
}
521527
}

‎src/BootstrapBlazor/Components/Table/Table.razor.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ private string GetSortTooltip(ITableColumn col) => SortName != col.GetFieldName(
522522
private bool _resetTable;
523523
private bool _resetColumnListPopover;
524524
private bool _resetColumns;
525+
private bool _measureColumnMinWidth;
525526
private bool _updateSortTooltip;
526527
private bool _shouldScrollTop;
527528
private bool _invoke;
@@ -1151,13 +1152,15 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
11511152
var resetColumnListPopover = _resetColumnListPopover;
11521153
var resetTable = _resetTable;
11531154
var resetColumns = _resetColumns;
1155+
var measureColumnMinWidth = _measureColumnMinWidth;
11541156
var updateSortTooltip = _updateSortTooltip;
11551157
var scrollToTop = _shouldScrollTop;
11561158

11571159
_invoke = false;
11581160
_resetColumnListPopover = false;
11591161
_resetTable = false;
11601162
_resetColumns = false;
1163+
_measureColumnMinWidth = false;
11611164
_updateSortTooltip = false;
11621165
_shouldScrollTop = false;
11631166

@@ -1167,6 +1170,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
11671170
ResetColumnListPopover = resetColumnListPopover,
11681171
ResetTable = resetTable,
11691172
ResetColumns = resetColumns,
1173+
MeasureColumnMinWidth = measureColumnMinWidth,
11701174
ColumnStates = _tableColumnStates,
11711175
AllowDragColumn,
11721176
UpdateSortTooltip = updateSortTooltip,
@@ -1463,6 +1467,7 @@ private async Task OnTableRenderAsync(bool firstRender)
14631467
FitColumnWidthIncludeHeader,
14641468
ResizeColumnCallback = nameof(ResizeColumnCallback),
14651469
ColumnMinWidth = ColumnMinWidth ?? Options.CurrentValue.TableSettings.ColumnMinWidth,
1470+
ColumnStates = _tableColumnStates,
14661471
ScrollWidth = ActualScrollWidth,
14671472
ShowColumnWidthTooltip,
14681473
ColumnWidthTooltipPrefix,

‎src/BootstrapBlazor/Components/Table/Table.razor.js‎

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ export async function reset(id) {
2626
return;
2727
}
2828

29+
if (table.minWidthObserver) {
30+
table.minWidthObserver.disconnect();
31+
table.minWidthObserver = null;
32+
}
33+
if (table.minWidthRaf) {
34+
cancelAnimationFrame(table.minWidthRaf);
35+
table.minWidthRaf = null;
36+
}
37+
table.autoColumns = [];
38+
2939
table.columns = [];
3040
table.tables = [];
3141
table.dragColumns = [];
@@ -51,6 +61,19 @@ export async function reset(id) {
5161
const left = table.body.scrollLeft
5262
table.thead.scrollTo(left, 0)
5363
});
64+
65+
if (window.ResizeObserver) {
66+
table.minWidthObserver = new ResizeObserver(() => {
67+
if (table.minWidthRaf) {
68+
cancelAnimationFrame(table.minWidthRaf);
69+
}
70+
table.minWidthRaf = requestAnimationFrame(() => {
71+
table.minWidthRaf = null;
72+
applyColumnMinWidth(table);
73+
});
74+
});
75+
table.minWidthObserver.observe(table.body);
76+
}
5477
}
5578
else {
5679
table.isExcel = shim.firstElementChild.classList.contains('table-excel')
@@ -91,6 +114,10 @@ export async function reset(id) {
91114
table.loopCheckHeightHandler = requestAnimationFrame(() => check(table));
92115
return;
93116
}
117+
118+
if (table.thead) {
119+
setColSize(table, table.options);
120+
}
94121
}
95122

96123
export async function switchCardView(id) {
@@ -113,6 +140,13 @@ const destroyTable = table => {
113140
if (table.loopCheckHeightHandler) {
114141
cancelAnimationFrame(table.loopCheckHeightHandler);
115142
}
143+
if (table.minWidthObserver) {
144+
table.minWidthObserver.disconnect();
145+
table.minWidthObserver = null;
146+
}
147+
if (table.minWidthRaf) {
148+
cancelAnimationFrame(table.minWidthRaf);
149+
}
116150
if (table.thead) {
117151
EventHandler.off(table.body, 'scroll')
118152
}
@@ -1062,6 +1096,9 @@ export async function updateTableState(id, options) {
10621096
if (options.resetColumns) {
10631097
resetColumns(table, options);
10641098
}
1099+
else if (options.measureColumnMinWidth) {
1100+
setColSize(table, options);
1101+
}
10651102

10661103
if (options.resetColumnListPopover) {
10671104
resetColumnListPopover(table);
@@ -1121,18 +1158,57 @@ const resetColumns = (table, options) => {
11211158
}
11221159

11231160
const setColSize = (table, options) => {
1124-
var zeroWidthColumns = options.columnStates.filter(i => i.width === null && i.visible === true);
1125-
zeroWidthColumns.forEach(col => {
1126-
const headerCollection = [...table.tables[0].querySelectorAll('thead > tr > th')];
1161+
if (!table.tables || table.tables.length === 0) {
1162+
return;
1163+
}
1164+
const headerCollection = [...table.tables[0].querySelectorAll('thead > tr > th')];
1165+
const columnMinWidth = table.options.columnMinWidth || 0;
1166+
const autoColumns = [];
1167+
options.columnStates.forEach(col => {
1168+
if (col.width != null || col.visible === false) {
1169+
return;
1170+
}
11271171
const th = headerCollection.find(i => i.getAttribute('data-bb-field') === col.name);
1128-
if (th.offsetWidth === 0) {
1129-
const width = getCellWidth(th);
1130-
const colIndex = headerCollection.indexOf(th);
1131-
col.width = Math.max(width, getColumnMaxCellWidth(table, colIndex));
1132-
table.tables.forEach(table => {
1133-
table.querySelectorAll('colgroup col')[colIndex].style.width = `${col.width}px`;
1134-
});
1172+
if (th === void 0) {
1173+
return;
11351174
}
1175+
const colIndex = headerCollection.indexOf(th);
1176+
const minWidth = Math.max(getCellWidth(th) + getHeaderIconsWidth(th), getColumnMaxCellWidth(table, colIndex), columnMinWidth) | 0;
1177+
autoColumns.push({ colIndex, minWidth });
1178+
});
1179+
table.autoColumns = autoColumns;
1180+
applyColumnMinWidth(table);
1181+
}
1182+
1183+
const getHeaderIconsWidth = th => {
1184+
let width = 0;
1185+
th.querySelectorAll('.sort-icon, .filter-icon, .toolbox-icon, .col-copy').forEach(icon => {
1186+
width += getWidth(icon);
1187+
});
1188+
return width;
1189+
}
1190+
1191+
const applyColumnMinWidth = table => {
1192+
if (!table.thead || !table.body || !table.autoColumns || table.autoColumns.length === 0) {
1193+
return;
1194+
}
1195+
setAutoColWidths(table, true);
1196+
const compact = table.body.scrollWidth > table.body.clientWidth + 1;
1197+
if (!compact) {
1198+
setAutoColWidths(table, false);
1199+
}
1200+
}
1201+
1202+
const setAutoColWidths = (table, apply) => {
1203+
const colgroups = table.tables.map(t => t.querySelectorAll('colgroup col'));
1204+
table.autoColumns.forEach(({ colIndex, minWidth }) => {
1205+
const width = apply ? `${minWidth}px` : '';
1206+
colgroups.forEach(cols => {
1207+
const colEl = cols[colIndex];
1208+
if (colEl) {
1209+
colEl.style.width = width;
1210+
}
1211+
});
11361212
});
11371213
}
11381214

0 commit comments

Comments
 (0)