fix: 【mall】修复商城系统订单列表中表头未根据表体自适应宽度

pull/831/MERGE
YunaiV 2025-11-22 10:53:29 +08:00
parent 559a13defd
commit 5ebbc756c4
1 changed files with 49 additions and 9 deletions

View File

@ -40,7 +40,7 @@
<el-table-column min-width="300" prop="spuName">
<template #header>
<div
class="mr-[20px] h-[35px] flex items-center pl-[10px] pr-[10px]"
class="h-[35px] flex items-center -mx-[10px] px-[20px]"
style="background-color: var(--app-content-bg-color)"
>
<span class="mr-20px">订单号{{ scope.row.no }} </span>
@ -166,6 +166,7 @@
</el-table-column>
</template>
<script lang="ts" setup>
import { nextTick, onMounted, onUnmounted, watch } from 'vue'
import { DICT_TYPE } from '@/utils/dict'
import { DeliveryTypeEnum } from '@/utils/constants'
import { formatDate } from '@/utils/formatTime'
@ -226,19 +227,26 @@ const spanMethod = ({ row, rowIndex, columnIndex }: SpanMethodProps): spanMethod
}
}
const orderTableHeadWidthList = ref([300, 150, 120, 120, 160, 120, 120, 160]) // col
let isFirstTable = false //
let firstTableInstance: TableInstance | null = null
/** 解决 ref 在 v-for 中的获取问题*/
const setOrderTableRef = (el: TableInstance) => {
const setOrderTableRef = async (el: TableInstance) => {
if (!el) return
//
if (el.tableId !== 'el-table_2') {
return
//
if (!isFirstTable) {
isFirstTable = true
firstTableInstance = el
// 使 nextTick DOM
await nextTick()
tableHeadWidthAuto(el)
}
tableHeadWidthAuto(el)
}
// col
const orderTableHeadWidthList = ref([300, 150, 120, 120, 160, 120, 120, 160])
//
/** 头部宽度自适应 */
const tableHeadWidthAuto = (el: TableInstance) => {
if (!el) return
const columns = el.store.states.columns.value
if (columns.length === 0) {
return
@ -249,6 +257,38 @@ const tableHeadWidthAuto = (el: TableInstance) => {
}
})
}
/** 监听窗口大小变化,重新计算表头宽度 */
const handleResize = async () => {
if (firstTableInstance) {
await nextTick()
tableHeadWidthAuto(firstTableInstance!)
}
}
/** 监听列表数据变化,重新计算表头宽度 */
watch(
() => props.list,
async () => {
// DOM
await nextTick()
if (firstTableInstance) {
//
await new Promise((resolve) => setTimeout(resolve, 100))
tableHeadWidthAuto(firstTableInstance!)
}
},
{ deep: true }
)
onMounted(() => {
window.addEventListener('resize', handleResize)
})
onUnmounted(() => {
window.removeEventListener('resize', handleResize)
})
/** 商品图预览 */
const imagePreview = (imgUrl: string) => {
createImageViewer({