ren
This commit is contained in:
@@ -150,31 +150,57 @@ const initCheckedColumns = () => {
|
|||||||
if (props.modelValue && props.modelValue.length > 0) {
|
if (props.modelValue && props.modelValue.length > 0) {
|
||||||
checkedColumns.value = [...props.modelValue]
|
checkedColumns.value = [...props.modelValue]
|
||||||
} else if (props.tableRef && autoVisibleColumns.value.length > 0) {
|
} else if (props.tableRef && autoVisibleColumns.value.length > 0) {
|
||||||
// 使用自动提取的可见列,但需要确保包含所有列(包括固定列)
|
// 使用自动提取的可见列
|
||||||
// 合并已保存的可见列和固定列/alwaysShow列
|
const allSelectableColumns = actualColumns.value
|
||||||
const fixedAndAlwaysShow = actualColumns.value
|
.filter(col => !col.alwaysShow && !col.fixed)
|
||||||
.filter(col => col.alwaysShow || !!col.fixed)
|
|
||||||
.map(col => col.prop || col.label)
|
.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) {
|
} else if (props.storageKey) {
|
||||||
// 从 localStorage 读取
|
// 从 localStorage 读取
|
||||||
const saved = localStorage.getItem(props.storageKey)
|
const saved = localStorage.getItem(props.storageKey)
|
||||||
if (saved) {
|
if (saved) {
|
||||||
try {
|
try {
|
||||||
const savedColumns = JSON.parse(saved)
|
const savedColumns = JSON.parse(saved)
|
||||||
// 确保固定列和 alwaysShow 列始终在选中列表中
|
const allSelectableColumns = actualColumns.value
|
||||||
const fixedAndAlwaysShow = actualColumns.value
|
.filter(col => !col.alwaysShow && !col.fixed)
|
||||||
.filter(col => col.alwaysShow || !!col.fixed)
|
|
||||||
.map(col => col.prop || col.label)
|
.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) {
|
} catch (e) {
|
||||||
// 如果解析失败,使用默认值(所有列)
|
// 如果解析失败,使用默认值(所有列)
|
||||||
checkedColumns.value = getAllColumns()
|
checkedColumns.value = getAllColumns()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// 首次使用,默认全部选中
|
||||||
checkedColumns.value = getAllColumns()
|
checkedColumns.value = getAllColumns()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// 没有 storageKey,默认全部选中
|
||||||
checkedColumns.value = getAllColumns()
|
checkedColumns.value = getAllColumns()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -201,9 +227,22 @@ watch(actualColumns, (newColumns) => {
|
|||||||
watch(visible, (newVal) => {
|
watch(visible, (newVal) => {
|
||||||
console.log('[TableColumnControl] 弹窗状态变化:', newVal)
|
console.log('[TableColumnControl] 弹窗状态变化:', newVal)
|
||||||
if (newVal) {
|
if (newVal) {
|
||||||
// 弹窗打开时,确保选中状态正确初始化
|
// 弹窗打开时,确保选中状态正确初始化(但不重置用户已保存的设置)
|
||||||
if (actualColumns.value.length > 0) {
|
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) {
|
if (newVal && props.tableRef) {
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ export function useTableColumns(
|
|||||||
) {
|
) {
|
||||||
const columns = ref<ColumnConfig[]>([])
|
const columns = ref<ColumnConfig[]>([])
|
||||||
const visibleColumns = ref<string[]>([])
|
const visibleColumns = ref<string[]>([])
|
||||||
|
const isInitialized = ref(false) // 标记是否已经初始化过(用户是否操作过)
|
||||||
|
const hasInitializedVisibleColumns = ref(false) // 标记是否已经初始化过可见列(避免重复初始化)
|
||||||
|
|
||||||
// 获取表格实例的辅助函数
|
// 获取表格实例的辅助函数
|
||||||
const getTableInstance = (): TableInstance | null => {
|
const getTableInstance = (): TableInstance | null => {
|
||||||
@@ -379,25 +381,42 @@ export function useTableColumns(
|
|||||||
columns.value = Array.from(map.values())
|
columns.value = Array.from(map.values())
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化可见列
|
// 初始化可见列(只在第一次初始化时执行,避免后续刷新时重置用户设置)
|
||||||
if (storageKey) {
|
if (!hasInitializedVisibleColumns.value) {
|
||||||
const saved = localStorage.getItem(storageKey)
|
if (storageKey) {
|
||||||
if (saved) {
|
const saved = localStorage.getItem(storageKey)
|
||||||
try {
|
if (saved) {
|
||||||
visibleColumns.value = JSON.parse(saved)
|
try {
|
||||||
// 验证保存的列是否仍然存在
|
const savedColumns = JSON.parse(saved)
|
||||||
const validColumns = columns.value
|
// 验证保存的列是否仍然存在
|
||||||
.filter(col => !col.alwaysShow && col.fixed === undefined)
|
const validColumns = columns.value
|
||||||
.map(col => col.prop || col.label)
|
.filter(col => !col.alwaysShow && col.fixed === undefined)
|
||||||
visibleColumns.value = visibleColumns.value.filter(col => validColumns.includes(col))
|
.map(col => col.prop || col.label)
|
||||||
} catch (e) {
|
const filteredSaved = savedColumns.filter((col: string) => validColumns.includes(col))
|
||||||
|
|
||||||
|
// 如果保存的列数量少于所有列,说明是旧数据,默认显示所有列
|
||||||
|
if (filteredSaved.length < validColumns.length) {
|
||||||
|
visibleColumns.value = validColumns
|
||||||
|
isInitialized.value = false // 旧数据,视为未初始化
|
||||||
|
} else {
|
||||||
|
visibleColumns.value = filteredSaved
|
||||||
|
isInitialized.value = true // 有保存的数据,视为已初始化
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
initDefaultVisibleColumns()
|
||||||
|
isInitialized.value = false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 首次使用,默认显示所有列
|
||||||
initDefaultVisibleColumns()
|
initDefaultVisibleColumns()
|
||||||
|
isInitialized.value = false
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// 没有 storageKey,默认显示所有列
|
||||||
initDefaultVisibleColumns()
|
initDefaultVisibleColumns()
|
||||||
|
isInitialized.value = false
|
||||||
}
|
}
|
||||||
} else {
|
hasInitializedVisibleColumns.value = true
|
||||||
initDefaultVisibleColumns()
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.warn('[useTableColumns] initColumns: 提取失败,未设置 columns.value')
|
console.warn('[useTableColumns] initColumns: 提取失败,未设置 columns.value')
|
||||||
@@ -406,24 +425,12 @@ export function useTableColumns(
|
|||||||
|
|
||||||
// 初始化默认可见列
|
// 初始化默认可见列
|
||||||
const initDefaultVisibleColumns = () => {
|
const initDefaultVisibleColumns = () => {
|
||||||
const defaultHidden = options?.defaultHidden || []
|
// 默认显示所有可选择的列(除了固定列和 alwaysShow 列)
|
||||||
// 默认显示所有列(除了默认隐藏的列和固定列/alwaysShow列)
|
|
||||||
// 注意:固定列和 alwaysShow 列不需要在 visibleColumns 中,因为它们始终显示
|
// 注意:固定列和 alwaysShow 列不需要在 visibleColumns 中,因为它们始终显示
|
||||||
|
// 默认全部选中,展示所有列
|
||||||
visibleColumns.value = columns.value
|
visibleColumns.value = columns.value
|
||||||
.filter(col => {
|
.filter(col => !col.alwaysShow && !col.fixed)
|
||||||
const key = col.prop || col.label
|
|
||||||
return !col.alwaysShow &&
|
|
||||||
!col.fixed &&
|
|
||||||
!defaultHidden.includes(key)
|
|
||||||
})
|
|
||||||
.map(col => col.prop || col.label)
|
.map(col => col.prop || col.label)
|
||||||
|
|
||||||
// 如果所有列都被隐藏了,至少显示所有非固定列
|
|
||||||
if (visibleColumns.value.length === 0 && columns.value.length > 0) {
|
|
||||||
visibleColumns.value = columns.value
|
|
||||||
.filter(col => !col.alwaysShow && !col.fixed)
|
|
||||||
.map(col => col.prop || col.label)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断列是否可见
|
// 判断列是否可见
|
||||||
@@ -447,11 +454,17 @@ export function useTableColumns(
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果可见列列表为空,默认显示所有列(初始状态)
|
// 如果还未初始化(用户未操作过),默认显示所有列
|
||||||
if (visibleColumns.value.length === 0) {
|
if (!isInitialized.value) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果已经初始化,严格按照可见列列表判断
|
||||||
|
// 如果可见列列表为空,说明用户取消了所有列,应该隐藏所有非固定列
|
||||||
|
if (visibleColumns.value.length === 0) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// 检查是否在可见列列表中
|
// 检查是否在可见列列表中
|
||||||
return visibleColumns.value.includes(propOrLabel)
|
return visibleColumns.value.includes(propOrLabel)
|
||||||
}
|
}
|
||||||
@@ -459,6 +472,7 @@ export function useTableColumns(
|
|||||||
// 更新可见列
|
// 更新可见列
|
||||||
const updateVisibleColumns = (newColumns: string[]) => {
|
const updateVisibleColumns = (newColumns: string[]) => {
|
||||||
visibleColumns.value = newColumns
|
visibleColumns.value = newColumns
|
||||||
|
isInitialized.value = true // 标记已经初始化(用户已操作)
|
||||||
if (storageKey) {
|
if (storageKey) {
|
||||||
localStorage.setItem(storageKey, JSON.stringify(newColumns))
|
localStorage.setItem(storageKey, JSON.stringify(newColumns))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2076,37 +2076,37 @@
|
|||||||
command: 'export',
|
command: 'export',
|
||||||
label: '导出',
|
label: '导出',
|
||||||
icon: Download,
|
icon: Download,
|
||||||
visible: permissions.value.professional_teacherbase_export
|
visible: () => permissions.value.professional_teacherbase_export
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
command: 'personnel-transfer',
|
command: 'personnel-transfer',
|
||||||
label: '人员调动',
|
label: '人员调动',
|
||||||
icon: Switch,
|
icon: Switch,
|
||||||
visible: permissions.value.professional_teacherbase_status_lock
|
visible: () => permissions.value.professional_teacherbase_status_lock
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
command: 'party-transfer',
|
command: 'party-transfer',
|
||||||
label: '党员调动',
|
label: '党员调动',
|
||||||
icon: Switch,
|
icon: Switch,
|
||||||
visible: permissions.value.professional_teacherbase_status_lock
|
visible: () => permissions.value.professional_teacherbase_status_lock
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
command: 'allow-inout',
|
command: 'allow-inout',
|
||||||
label: '允许进出',
|
label: '允许进出',
|
||||||
icon: CircleCheck,
|
icon: CircleCheck,
|
||||||
visible: permissions.value.professional_teacherbase_inout && row.inoutFlag == '0'
|
visible: () => permissions.value.professional_teacherbase_inout && row.inoutFlag == '0'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
command: 'forbid-inout',
|
command: 'forbid-inout',
|
||||||
label: '禁止进出',
|
label: '禁止进出',
|
||||||
icon: CircleClose,
|
icon: CircleClose,
|
||||||
visible: permissions.value.professional_teacherbase_inout && row.inoutFlag == '1'
|
visible: () => permissions.value.professional_teacherbase_inout && row.inoutFlag == '1'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
command: 'reset-password',
|
command: 'reset-password',
|
||||||
label: '重置密码',
|
label: '重置密码',
|
||||||
icon: Refresh,
|
icon: Refresh,
|
||||||
visible: permissions.value.professional_teacherbase_resetpw
|
visible: () => permissions.value.professional_teacherbase_resetpw
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user