ren
This commit is contained in:
@@ -76,23 +76,50 @@ export function useTableColumns(
|
||||
|
||||
if (store.states && store.states.columns) {
|
||||
tableColumns = store.states.columns.value || []
|
||||
console.log('[useTableColumns] 从 store.states.columns 获取到列数:', tableColumns.length)
|
||||
console.log('[useTableColumns] store.states.columns 内容:', tableColumns)
|
||||
} else if (store.columns) {
|
||||
tableColumns = Array.isArray(store.columns) ? store.columns : (store.columns.value || [])
|
||||
console.log('[useTableColumns] 从 store.columns 获取到列数:', tableColumns.length)
|
||||
} else if ((table as any).columns) {
|
||||
tableColumns = Array.isArray((table as any).columns) ? (table as any).columns : ((table as any).columns.value || [])
|
||||
console.log('[useTableColumns] 从 table.columns 获取到列数:', tableColumns.length)
|
||||
}
|
||||
|
||||
if (tableColumns.length > 0) {
|
||||
tableColumns.forEach((col: any) => {
|
||||
// 跳过序号列和没有 label 的列
|
||||
if (!col.label || col.type === 'index') return
|
||||
tableColumns.forEach((col: any, idx: number) => {
|
||||
console.log(`[useTableColumns] store 列 ${idx}:`, {
|
||||
type: col.type,
|
||||
label: col.label,
|
||||
property: col.property,
|
||||
prop: col.prop,
|
||||
fixed: col.fixed,
|
||||
fullCol: col
|
||||
})
|
||||
|
||||
// 跳过序号列
|
||||
if (col.type === 'index' || col.type === 'selection') {
|
||||
console.log(`[useTableColumns] 列 ${idx} 被跳过(类型: ${col.type})`)
|
||||
return
|
||||
}
|
||||
|
||||
// 尝试多种方式获取 label
|
||||
const label = col.label || col.columnKey || col.property || col.prop || ''
|
||||
|
||||
// 如果没有 label,尝试从其他属性推断
|
||||
if (!label) {
|
||||
console.log(`[useTableColumns] 列 ${idx} 没有 label,尝试从其他属性推断`)
|
||||
// 如果还是没有,跳过这一列
|
||||
return
|
||||
}
|
||||
|
||||
const config: ColumnConfig = {
|
||||
prop: col.property || col.prop || '',
|
||||
label: col.label || '',
|
||||
label: label,
|
||||
width: col.width,
|
||||
minWidth: col.minWidth,
|
||||
fixed: col.fixed,
|
||||
// Element Plus 中非固定列的 fixed 通常是 false,这里统一将 false 归一为 undefined
|
||||
fixed: col.fixed ? col.fixed : undefined,
|
||||
}
|
||||
|
||||
// 应用自定义映射
|
||||
@@ -111,12 +138,16 @@ export function useTableColumns(
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[useTableColumns] 从 store 提取到列配置:`, config)
|
||||
extracted.push(config)
|
||||
})
|
||||
|
||||
console.log('[useTableColumns] 从 store 总共提取到列数:', extracted.length)
|
||||
if (extracted.length > 0) {
|
||||
return extracted
|
||||
}
|
||||
} else {
|
||||
console.warn('[useTableColumns] store 中没有找到列数据')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,8 +205,45 @@ export function useTableColumns(
|
||||
}
|
||||
|
||||
columnHeaders.forEach((th: HTMLElement, index: number) => {
|
||||
const label = th.textContent?.trim() || ''
|
||||
console.log(`[useTableColumns] 列 ${index}: label="${label}"`)
|
||||
// 尝试多种方式获取 label 文本
|
||||
// 1. 从 .cell 元素
|
||||
const cell = th.querySelector('.cell') as HTMLElement | null
|
||||
// 2. 从所有可能的文本节点
|
||||
let rawLabel = ''
|
||||
|
||||
if (cell) {
|
||||
rawLabel = cell.innerText || cell.textContent || ''
|
||||
} else {
|
||||
// 尝试从 th 的所有子元素中查找文本
|
||||
const textNodes: string[] = []
|
||||
const walker = document.createTreeWalker(
|
||||
th,
|
||||
NodeFilter.SHOW_TEXT,
|
||||
null
|
||||
)
|
||||
let node: Node | null
|
||||
while ((node = walker.nextNode())) {
|
||||
const text = node.textContent?.trim()
|
||||
if (text) {
|
||||
textNodes.push(text)
|
||||
}
|
||||
}
|
||||
rawLabel = textNodes.join(' ') || th.innerText || th.textContent || ''
|
||||
}
|
||||
|
||||
const label = rawLabel.trim()
|
||||
|
||||
// 调试:打印 th 的完整结构
|
||||
console.log(`[useTableColumns] DOM 列 ${index}:`, {
|
||||
label,
|
||||
rawLabel,
|
||||
thHTML: th.innerHTML.substring(0, 100),
|
||||
hasCell: !!cell,
|
||||
cellText: cell?.innerText || cell?.textContent || '',
|
||||
thInnerText: th.innerText,
|
||||
thTextContent: th.textContent
|
||||
})
|
||||
|
||||
// 排除序号列和空列
|
||||
if (!label || label === '序号') {
|
||||
console.log(`[useTableColumns] 列 ${index} 被跳过(序号列或空列)`)
|
||||
@@ -234,7 +302,8 @@ export function useTableColumns(
|
||||
prop: prop || `column_${index}`,
|
||||
label,
|
||||
width,
|
||||
fixed,
|
||||
// DOM 提取的 fixed 只有 left/right,这里也归一为 undefined 或 'left'/'right'
|
||||
fixed: fixed ? fixed : undefined,
|
||||
}
|
||||
console.log(`[useTableColumns] 提取到列配置:`, columnConfig)
|
||||
extracted.push(columnConfig)
|
||||
@@ -304,12 +373,13 @@ export function useTableColumns(
|
||||
// 初始化默认可见列
|
||||
const initDefaultVisibleColumns = () => {
|
||||
const defaultHidden = options?.defaultHidden || []
|
||||
// 默认显示所有列(除了默认隐藏的列)
|
||||
// 默认显示所有列(除了默认隐藏的列和固定列/alwaysShow列)
|
||||
// 注意:固定列和 alwaysShow 列不需要在 visibleColumns 中,因为它们始终显示
|
||||
visibleColumns.value = columns.value
|
||||
.filter(col => {
|
||||
const key = col.prop || col.label
|
||||
return !col.alwaysShow &&
|
||||
col.fixed === undefined &&
|
||||
!col.fixed &&
|
||||
!defaultHidden.includes(key)
|
||||
})
|
||||
.map(col => col.prop || col.label)
|
||||
@@ -317,7 +387,7 @@ export function useTableColumns(
|
||||
// 如果所有列都被隐藏了,至少显示所有非固定列
|
||||
if (visibleColumns.value.length === 0 && columns.value.length > 0) {
|
||||
visibleColumns.value = columns.value
|
||||
.filter(col => !col.alwaysShow && col.fixed === undefined)
|
||||
.filter(col => !col.alwaysShow && !col.fixed)
|
||||
.map(col => col.prop || col.label)
|
||||
}
|
||||
}
|
||||
@@ -339,7 +409,7 @@ export function useTableColumns(
|
||||
}
|
||||
|
||||
// 固定列和始终显示的列始终显示
|
||||
if (column.fixed !== undefined || column.alwaysShow) {
|
||||
if (column.fixed || column.alwaysShow) {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user