ren
This commit is contained in:
@@ -150,31 +150,57 @@ const initCheckedColumns = () => {
|
||||
if (props.modelValue && props.modelValue.length > 0) {
|
||||
checkedColumns.value = [...props.modelValue]
|
||||
} else if (props.tableRef && autoVisibleColumns.value.length > 0) {
|
||||
// 使用自动提取的可见列,但需要确保包含所有列(包括固定列)
|
||||
// 合并已保存的可见列和固定列/alwaysShow列
|
||||
const fixedAndAlwaysShow = actualColumns.value
|
||||
.filter(col => col.alwaysShow || !!col.fixed)
|
||||
// 使用自动提取的可见列
|
||||
const allSelectableColumns = actualColumns.value
|
||||
.filter(col => !col.alwaysShow && !col.fixed)
|
||||
.map(col => col.prop || col.label)
|
||||
checkedColumns.value = [...new Set([...autoVisibleColumns.value, ...fixedAndAlwaysShow])]
|
||||
|
||||
// 如果保存的列数量少于所有列,默认全部选中
|
||||
if (autoVisibleColumns.value.length < allSelectableColumns.length) {
|
||||
const fixedAndAlwaysShow = actualColumns.value
|
||||
.filter(col => col.alwaysShow || !!col.fixed)
|
||||
.map(col => col.prop || col.label)
|
||||
checkedColumns.value = [...new Set([...allSelectableColumns, ...fixedAndAlwaysShow])]
|
||||
} else {
|
||||
// 使用保存的列,但确保包含固定列和 alwaysShow 列
|
||||
const fixedAndAlwaysShow = actualColumns.value
|
||||
.filter(col => col.alwaysShow || !!col.fixed)
|
||||
.map(col => col.prop || col.label)
|
||||
checkedColumns.value = [...new Set([...autoVisibleColumns.value, ...fixedAndAlwaysShow])]
|
||||
}
|
||||
} else if (props.storageKey) {
|
||||
// 从 localStorage 读取
|
||||
const saved = localStorage.getItem(props.storageKey)
|
||||
if (saved) {
|
||||
try {
|
||||
const savedColumns = JSON.parse(saved)
|
||||
// 确保固定列和 alwaysShow 列始终在选中列表中
|
||||
const fixedAndAlwaysShow = actualColumns.value
|
||||
.filter(col => col.alwaysShow || !!col.fixed)
|
||||
const allSelectableColumns = actualColumns.value
|
||||
.filter(col => !col.alwaysShow && !col.fixed)
|
||||
.map(col => col.prop || col.label)
|
||||
checkedColumns.value = [...new Set([...savedColumns, ...fixedAndAlwaysShow])]
|
||||
|
||||
// 如果保存的列数量少于所有列,默认全部选中
|
||||
if (savedColumns.length < allSelectableColumns.length) {
|
||||
const fixedAndAlwaysShow = actualColumns.value
|
||||
.filter(col => col.alwaysShow || !!col.fixed)
|
||||
.map(col => col.prop || col.label)
|
||||
checkedColumns.value = [...new Set([...allSelectableColumns, ...fixedAndAlwaysShow])]
|
||||
} else {
|
||||
// 使用保存的列,但确保包含固定列和 alwaysShow 列
|
||||
const fixedAndAlwaysShow = actualColumns.value
|
||||
.filter(col => col.alwaysShow || !!col.fixed)
|
||||
.map(col => col.prop || col.label)
|
||||
checkedColumns.value = [...new Set([...savedColumns, ...fixedAndAlwaysShow])]
|
||||
}
|
||||
} catch (e) {
|
||||
// 如果解析失败,使用默认值(所有列)
|
||||
checkedColumns.value = getAllColumns()
|
||||
}
|
||||
} else {
|
||||
// 首次使用,默认全部选中
|
||||
checkedColumns.value = getAllColumns()
|
||||
}
|
||||
} else {
|
||||
// 没有 storageKey,默认全部选中
|
||||
checkedColumns.value = getAllColumns()
|
||||
}
|
||||
}
|
||||
@@ -201,9 +227,22 @@ watch(actualColumns, (newColumns) => {
|
||||
watch(visible, (newVal) => {
|
||||
console.log('[TableColumnControl] 弹窗状态变化:', newVal)
|
||||
if (newVal) {
|
||||
// 弹窗打开时,确保选中状态正确初始化
|
||||
// 弹窗打开时,确保选中状态正确初始化(但不重置用户已保存的设置)
|
||||
if (actualColumns.value.length > 0) {
|
||||
initCheckedColumns()
|
||||
// 只有在 checkedColumns 为空时才初始化,避免覆盖用户已保存的设置
|
||||
if (checkedColumns.value.length === 0) {
|
||||
initCheckedColumns()
|
||||
} else {
|
||||
// 如果已有选中状态,只确保固定列和 alwaysShow 列在选中列表中
|
||||
const fixedAndAlwaysShow = actualColumns.value
|
||||
.filter(col => col.alwaysShow || !!col.fixed)
|
||||
.map(col => col.prop || col.label)
|
||||
const currentChecked = checkedColumns.value
|
||||
const missingFixed = fixedAndAlwaysShow.filter(col => !currentChecked.includes(col))
|
||||
if (missingFixed.length > 0) {
|
||||
checkedColumns.value = [...currentChecked, ...missingFixed]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (newVal && props.tableRef) {
|
||||
|
||||
Reference in New Issue
Block a user