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 sortOrder = ref(null)
const hoverRow = ref(null) const hoverRow = ref(null)
watch(data, () => instance.state && scheduleLayout(false), { watch(
deep: true, data,
}) () => {
if (instance.state) {
scheduleLayout(false)
const needUpdateFixed = instance.props.tableLayout === 'auto'
if (needUpdateFixed) {
instance.refs.tableHeaderRef?.updateFixedColumnStyle()
}
}
},
{
deep: true,
}
)
// 检查 rowKey 是否存在 // 检查 rowKey 是否存在
const assertRowKey = () => { const assertRowKey = () => {

View File

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