fix(components): [table] table-layout:auto fixed column misalignment (#18736)

* fix(components): [table] `table-layout:auto` fixed column misalignment

* fix: update

* fix: update

* fix: update

* Update packages/components/table/src/table-header/index.ts

Co-authored-by: qiang <qw13131wang@gmail.com>

* Update packages/components/table/src/table-header/index.ts

Co-authored-by: qiang <qw13131wang@gmail.com>

* fix: lint

---------

Co-authored-by: qiang <qw13131wang@gmail.com>
This commit is contained in:
btea 2024-11-10 14:39:45 +08:00 committed by GitHub
parent 03380de918
commit c0f071e464
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 9 deletions

View File

@ -77,9 +77,21 @@ function useWatcher<T>() {
const sortOrder = ref(null)
const hoverRow = ref(null)
watch(data, () => instance.state && scheduleLayout(false), {
deep: true,
})
watch(
data,
() => {
if (instance.state) {
scheduleLayout(false)
const needUpdateFixed = instance.props.tableLayout === 'auto'
if (needUpdateFixed) {
instance.refs.tableHeaderRef?.updateFixedColumnStyle()
}
}
},
{
deep: true,
}
)
// 检查 rowKey 是否存在
const assertRowKey = () => {

View File

@ -67,13 +67,38 @@ export default defineComponent({
const ns = useNamespace('table')
const filterPanels = ref({})
const { onColumnsChange, onScrollableChange } = useLayoutObserver(parent!)
const isTableLayoutAuto = parent?.props.tableLayout === 'auto'
const saveIndexSelection = new Map()
const theadRef = ref()
const updateFixedColumnStyle = () => {
setTimeout(() => {
if (saveIndexSelection.size > 0) {
saveIndexSelection.forEach((column, key) => {
const el = theadRef.value.querySelector(
`.${key.replace(/\s/g, '.')}`
)
if (el) {
const width = el.getBoundingClientRect().width
column.width = width
}
})
saveIndexSelection.clear()
}
})
}
onMounted(async () => {
// Need double await, because updateColumns is executed after nextTick for now
await nextTick()
await nextTick()
const { prop, order } = props.defaultSort
parent?.store.commit('sort', { prop, order, init: true })
updateFixedColumnStyle()
})
const {
handleHeaderClick,
handleHeaderContextMenu,
@ -118,6 +143,10 @@ export default defineComponent({
handleFilterClick,
isGroup,
toggleAllSelection,
saveIndexSelection,
isTableLayoutAuto,
theadRef,
updateFixedColumnStyle,
}
},
render() {
@ -137,11 +166,14 @@ export default defineComponent({
handleMouseOut,
store,
$parent,
saveIndexSelection,
isTableLayoutAuto,
} = this
let rowSpan = 1
return h(
'thead',
{
ref: 'theadRef',
class: { [ns.is('group')]: isGroup },
},
columnRows.map((subColumns, rowIndex) =>
@ -156,15 +188,19 @@ export default defineComponent({
if (column.rowSpan > rowSpan) {
rowSpan = column.rowSpan
}
const _class = getHeaderCellClass(
rowIndex,
cellIndex,
subColumns,
column
)
if (isTableLayoutAuto && column.fixed) {
saveIndexSelection.set(_class, column)
}
return h(
'th',
{
class: getHeaderCellClass(
rowIndex,
cellIndex,
subColumns,
column
),
class: _class,
colspan: column.colSpan,
key: `${column.id}-thead`,
rowspan: column.rowSpan,