-
-
Notifications
You must be signed in to change notification settings - Fork 382
fix(Table): update resetTableWidth method #7836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f45eabd
6a3fc53
ac58137
99a6d07
69cd81b
b759996
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -536,24 +536,41 @@ const setExcelKeyboardListener = table => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const resetTableWidth = table => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { options: { scrollWidth } } = table; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| table.tables.forEach(t => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const group = [...t.children].find(i => i.nodeName === 'COLGROUP') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (group) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let width = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [...group.children].forEach(col => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let colWidth = parseInt(col.style.width); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isNaN(colWidth)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| colWidth = 100; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width += colWidth; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.style.width = `${width}px`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const colgroup = getLastColgroup(t, group); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (colgroup) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| colgroup.style = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const width = getTableWidthByColumnGroup(t, 100); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (width >= t.parentElement.offsetWidth + scrollWidth) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.style.width = `${width}px`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.style.width = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.style.width = null; | |
| t.style.removeProperty('width'); |
Copilot
AI
Apr 3, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getLastColgroup returns a col element (a child of the COLGROUP), not a COLGROUP. The current name/variable (colgroup) is misleading and makes the logic harder to follow. Consider renaming to something like getLastColElement / getLastDataCol and renaming the local variable accordingly.
Copilot
AI
Apr 3, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getTableWidthByColumnGroup/getColumnRenderWidth calls querySelectorAll for the header and searches tbody rows once per column, which scales poorly with many columns (O(n^2) DOM queries). Consider caching the header NodeList and the first non-detail row once per table-width calculation and passing them into getColumnRenderWidth to reduce repeated DOM work.
| return [...table.querySelectorAll('colgroup col')] | |
| .map((col, index) => getColumnRenderWidth(table, col, index, defaultWidth)) | |
| .reduce((accumulator, val) => accumulator + val, 0); | |
| } | |
| const getColumnRenderWidth = (table, col, index, defaultWidth) => { | |
| let width = parseFloat(col.style.width); | |
| if (!isNaN(width) && width > 0) { | |
| return width; | |
| } | |
| const header = table.querySelectorAll('thead > tr:last-child > th').item(index); | |
| width = header?.offsetWidth ?? 0; | |
| if (width > 0) { | |
| return width; | |
| } | |
| const row = [...table.querySelectorAll('tbody > tr')].find(i => !i.classList.contains('is-detail')); | |
| const headers = table.querySelectorAll('thead > tr:last-child > th'); | |
| const rows = table.querySelectorAll('tbody > tr'); | |
| const row = [...rows].find(i => !i.classList.contains('is-detail')); | |
| return [...table.querySelectorAll('colgroup col')] | |
| .map((col, index) => getColumnRenderWidth(col, index, defaultWidth, headers, row)) | |
| .reduce((accumulator, val) => accumulator + val, 0); | |
| } | |
| const getColumnRenderWidth = (col, index, defaultWidth, headers, row) => { | |
| let width = parseFloat(col.style.width); | |
| if (!isNaN(width) && width > 0) { | |
| return width; | |
| } | |
| const header = headers.item(index); | |
| width = header?.offsetWidth ?? 0; | |
| if (width > 0) { | |
| return width; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (performance): Avoid repeated DOM queries inside width calculations for better performance.
getTableWidthByColumnGroup / getColumnRenderWidth re-run querySelectorAll on colgroup col, thead, and tbody for every column, which can be costly on large tables or during frequent resizes. Consider querying these once (e.g. const cols = ..., const headers = ..., a cached non-detail body row) and passing them into getColumnRenderWidth, or at least moving the header/body queries outside the per-column loop.
| const getTableWidthByColumnGroup = (table, defaultWidth) => { | |
| return [...table.querySelectorAll('colgroup col')] | |
| .map((col, index) => getColumnRenderWidth(table, col, index, defaultWidth)) | |
| .reduce((accumulator, val) => accumulator + val, 0); | |
| } | |
| const getColumnRenderWidth = (table, col, index, defaultWidth) => { | |
| let width = parseFloat(col.style.width); | |
| if (!isNaN(width) && width > 0) { | |
| return width; | |
| } | |
| const header = table.querySelectorAll('thead > tr:last-child > th').item(index); | |
| width = header?.offsetWidth ?? 0; | |
| if (width > 0) { | |
| return width; | |
| } | |
| const row = [...table.querySelectorAll('tbody > tr')].find(i => !i.classList.contains('is-detail')); | |
| width = row?.children.item(index)?.offsetWidth ?? 0; | |
| return width > 0 ? width : defaultWidth; | |
| } | |
| const getTableWidthByColumnGroup = (table, defaultWidth) => { | |
| const cols = [...table.querySelectorAll('colgroup col')]; | |
| const headers = table.querySelectorAll('thead > tr:last-child > th'); | |
| const bodyRows = table.querySelectorAll('tbody > tr'); | |
| const dataRow = [...bodyRows].find(row => !row.classList.contains('is-detail')); | |
| return cols | |
| .map((col, index) => getColumnRenderWidth(col, index, defaultWidth, headers, dataRow)) | |
| .reduce((accumulator, val) => accumulator + val, 0); | |
| } | |
| const getColumnRenderWidth = (col, index, defaultWidth, headers, dataRow) => { | |
| let width = parseFloat(col.style.width); | |
| if (!isNaN(width) && width > 0) { | |
| return width; | |
| } | |
| const header = headers.item(index); | |
| width = header?.offsetWidth ?? 0; | |
| if (width > 0) { | |
| return width; | |
| } | |
| const cell = dataRow?.children.item(index); | |
| width = cell?.offsetWidth ?? 0; | |
| return width > 0 ? width : defaultWidth; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
colgroup.style = nullattempts to assign to the DOMstyleproperty (a read-onlyCSSStyleDeclarationin ES modules/strict mode) and can throw a TypeError. If the intent is to clear the last column's inline width, usecolgroup.style.removeProperty('width'), setcolgroup.style.width = '', orcolgroup.removeAttribute('style')(depending on what needs to be cleared).