Files
school-developer/src/components/TableColumn/index.vue
guochunsi 98fcd368f9 ren
2026-01-08 19:00:25 +08:00

45 lines
1.1 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<el-table-column
v-if="shouldShow"
:prop="prop"
:label="label"
v-bind="$attrs"
>
<template v-for="(_, name) in $slots" #[name]="slotProps">
<slot :name="name" v-bind="slotProps" />
</template>
</el-table-column>
</template>
<script setup lang="ts">
import { computed, inject } from 'vue'
interface Props {
prop?: string
label?: string
// 其他 el-table-column 的所有属性都通过 $attrs 传递
}
const props = withDefaults(defineProps<Props>(), {
prop: '',
label: ''
})
// 从父组件注入 isColumnVisible 函数
const isColumnVisible = inject<(propOrLabel: string) => boolean>('isColumnVisible', () => true)
// 计算是否应该显示该列
const shouldShow = computed(() => {
// 优先使用 prop如果没有 prop 则使用 label
const key = props.prop || props.label || ''
if (!key) {
// 如果没有 prop 和 label默认显示可能是序号列等特殊列
return true
}
// isColumnVisible 函数会同时匹配 prop 和 label所以直接传递即可
return isColumnVisible(key)
})
</script>