Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions src/BootstrapBlazor/Components/Table/Table.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export async function reset(id) {
table.minWidthRaf = null;
}
table.autoColumns = [];
table.resizing = false;

table.columns = [];
table.tables = [];
Expand Down Expand Up @@ -470,6 +471,7 @@ const setResizeListener = table => {
let tableWidth = 0
let colIndex = 0
let originalX = 0
let resized = false

const columns = [...table.tables[0].querySelectorAll('.col-resizer')]
columns.forEach(col => {
Expand All @@ -484,20 +486,24 @@ const setResizeListener = table => {
setColumnResizingListen(table, col);
drag(col,
e => {
// 拖动期间暂停列宽测试,防止 ResizeObserver 恢复拖动前宽度
table.resizing = true;
resized = false;
colIndex = eff(col, true)
const table = col.closest('table')
const currentCol = table.querySelectorAll('colgroup col')[colIndex]
const tableEl = col.closest('table')
const currentCol = tableEl.querySelectorAll('colgroup col')[colIndex]
const width = currentCol.style.width
if (width) {
colWidth = parseInt(width)
}
else {
colWidth = getResizableColumnWidth(col);
}
tableWidth = getWidth(col.closest('table'));
tableWidth = getWidth(tableEl);
originalX = e.clientX ?? e.touches[0].clientX
},
e => {
resized = true;
const eventX = e.clientX ?? e.changedTouches[0].clientX
const marginX = eventX - originalX
table.tables.forEach(t => {
Comment on lines +506 to 509

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): resized becomes true for every mousemove/touchmove event, even when eventX - originalX is zero, so moving the pointer without changing the column width still persists column state and invokes resizeColumnCallback, freezing a column that was not actually resized.

Triggers: When a pointer move event is delivered over the resizer without horizontal displacement, such as vertical pointer movement or duplicate events with the same clientX.

Suggested fix: Set resized only when the computed horizontal margin is nonzero and the resulting width differs from the starting width.

Suggested change
resized = true;
const eventX = e.clientX ?? e.changedTouches[0].clientX
const marginX = eventX - originalX
table.tables.forEach(t => {
const eventX = e.clientX ?? e.changedTouches[0].clientX
const marginX = eventX - originalX
let calcColWidth = colWidth + marginX;
if (calcColWidth < 5) {
calcColWidth = 5;
}
resized = marginX !== 0 && calcColWidth !== colWidth;
table.tables.forEach(t => {
const group = [...t.children].find(i => i.nodeName === 'COLGROUP')

Expand Down Expand Up @@ -538,12 +544,23 @@ const setResizeListener = table => {
},
() => {
eff(col, false)
table.resizing = false;

const state = getColumnStateObject(table);
saveColumnStateToLocalstorage(table, state);
// 仅在发生实际拖动且表格未被重置/销毁时生效
const th = getColumnHeader(col);
if (resized && th !== null && th.parentNode !== null) {
// 拖动的列由用户显式设定宽度,移出自动测量集合,防止列宽测试恢复拖动前宽度
removeAutoColumn(table, col, colIndex);

const field = getColumnName(col);
table.invoke.invokeMethodAsync(table.options.resizeColumnCallback, field, state);
// 拖动结束后列宽测试只执行一次
applyColumnMinWidth(table);
Comment on lines +552 to +556

const state = getColumnStateObject(table);
saveColumnStateToLocalstorage(table, state);

const field = getColumnName(col);
table.invoke.invokeMethodAsync(table.options.resizeColumnCallback, field, state);
}
}
)
})
Expand Down Expand Up @@ -643,6 +660,9 @@ const autoFitColumnWidth = async (table, col) => {
table.style.setProperty('width', `${tableWidth}px`);
});

// 自适应的列由用户显式设定宽度,移出自动测量集合,防止列宽测试恢复自适应前宽度
removeAutoColumn(table, col, index);

resetColumnWidthTips(table, col);

const state = getColumnStateObject(table);
Expand Down Expand Up @@ -1196,7 +1216,8 @@ const getHeaderIconsWidth = th => {
}

const applyColumnMinWidth = table => {
if (!table.thead || !table.body || !table.autoColumns || table.autoColumns.length === 0) {
// 拖动调整列宽期间暂停列宽测试,防止恢复拖动前宽度
if (table.resizing === true || !table.thead || !table.body || !table.autoColumns || table.autoColumns.length === 0) {
return;
}
setAutoColWidths(table, true);
Expand All @@ -1219,6 +1240,19 @@ const setAutoColWidths = (table, apply) => {
});
}

const removeAutoColumn = (table, col, colIndex) => {
if (table.autoColumns) {
table.autoColumns = table.autoColumns.filter(i => i.colIndex !== colIndex);
}

// 同步列状态宽度,防止 reset 时 setColSize 将该列重新加入自动测量集合
const field = getColumnName(col);
const state = (table.options.columnStates ?? []).find(i => i.name === field);
if (state) {
state.width = getResizableColumnWidth(col);
}
}

const updateSortTooltip = table => {
const el = table.el
const span = el.querySelector('.sortable .table-text[aria-describedby]')
Expand Down
Loading