Skip to content

Commit be6a36a

Browse files
authored
fix(Table): update resetTableWidth method (#7836)
* fix: 修复列宽计算错误 * chore: bump version 10.5.1-beta02 * doc: 格式化文档 * refactor: 列和大于表格宽度时设置 * fix: 修复脚本错误
1 parent dd73301 commit be6a36a

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

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.5.1-beta01</Version>
4+
<Version>10.5.1-beta02</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Table/Table.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,9 +1146,9 @@
11461146
RenderFragment<ColumnVisibleItem> RenderColumnListItem => item =>
11471147
@<div class="dropdown-item">
11481148
<Checkbox ShowAfterLabel="true" DisplayText="@item.DisplayName"
1149-
IsDisabled="@GetColumnsListState(item)"
1150-
@bind-Value="@item.Visible"
1151-
OnValueChanged="visible => OnToggleColumnVisible(item.Name, visible)">
1149+
IsDisabled="@GetColumnsListState(item)"
1150+
@bind-Value="@item.Visible"
1151+
OnValueChanged="visible => OnToggleColumnVisible(item.Name, visible)">
11521152
</Checkbox>
11531153
</div>;
11541154

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

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -536,24 +536,41 @@ const setExcelKeyboardListener = table => {
536536
}
537537

538538
const resetTableWidth = table => {
539+
const { options: { scrollWidth } } = table;
539540
table.tables.forEach(t => {
540541
const group = [...t.children].find(i => i.nodeName === 'COLGROUP')
541542
if (group) {
542-
let width = 0;
543-
[...group.children].forEach(col => {
544-
let colWidth = parseInt(col.style.width);
545-
if (isNaN(colWidth)) {
546-
colWidth = 100;
547-
}
548-
width += colWidth;
549-
})
550-
t.style.width = `${width}px`;
543+
const colgroup = getLastColgroup(t, group);
544+
if (colgroup) {
545+
colgroup.style = null;
546+
}
547+
const width = getTableWidthByColumnGroup(t, 100);
548+
if (width >= t.parentElement.offsetWidth + scrollWidth) {
549+
t.style.width = `${width}px`;
550+
}
551+
else {
552+
t.style.width = null;
553+
}
551554

552555
saveColumnWidth(table);
553556
}
554557
})
555558
}
556559

560+
const getLastColgroup = (table, group) => {
561+
const p = table.parentElement;
562+
if (p) {
563+
const length = group.children.length;
564+
if (p.classList.contains("table-fixed-header")) {
565+
return group.children[length - 2];
566+
}
567+
else {
568+
return group.children[length - 1];
569+
}
570+
}
571+
return null;
572+
}
573+
557574
const setResizeListener = table => {
558575
if (table.options.showColumnWidthTooltip) {
559576
table.handlers.setResizeHandler = e => {
@@ -1047,15 +1064,33 @@ const saveColumnWidth = table => {
10471064
}));
10481065
}
10491066

1067+
const getTableWidthByColumnGroup = (table, defaultWidth) => {
1068+
return [...table.querySelectorAll('colgroup col')]
1069+
.map((col, index) => getColumnRenderWidth(table, col, index, defaultWidth))
1070+
.reduce((accumulator, val) => accumulator + val, 0);
1071+
}
1072+
1073+
const getColumnRenderWidth = (table, col, index, defaultWidth) => {
1074+
let width = parseFloat(col.style.width);
1075+
if (!isNaN(width) && width > 0) {
1076+
return width;
1077+
}
1078+
1079+
const header = table.querySelectorAll('thead > tr:last-child > th').item(index);
1080+
width = header?.offsetWidth ?? 0;
1081+
if (width > 0) {
1082+
return width;
1083+
}
1084+
1085+
const row = [...table.querySelectorAll('tbody > tr')].find(i => !i.classList.contains('is-detail'));
1086+
width = row?.children.item(index)?.offsetWidth ?? 0;
1087+
return width > 0 ? width : defaultWidth;
1088+
}
1089+
10501090
const setTableDefaultWidth = table => {
10511091
if (table.tables.length > 0 && isVisible(table.tables[0])) {
10521092
const { scrollWidth, columnMinWidth } = table.options;
1053-
const tableWidth = [...table.tables[0].querySelectorAll('col')]
1054-
.map(i => {
1055-
const colWidth = parseFloat(i.style.width);
1056-
return isNaN(colWidth) ? columnMinWidth : colWidth;
1057-
})
1058-
.reduce((accumulator, val) => accumulator + val, 0);
1093+
const tableWidth = getTableWidthByColumnGroup(table.tables[0], columnMinWidth);
10591094

10601095
if (tableWidth > table.el.offsetWidth) {
10611096
table.tables[0].style.setProperty('width', `${tableWidth}px`);

0 commit comments

Comments
 (0)