2026-08-29 18:16:18 +08:00

66 lines
2.0 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.

<!--
ColumnCellRenderer — 通用表格列渲染器
根据传入的 ColumnSlotConfig 决定如何渲染单元格:
- type='tag' → el-tag(支持 tagResolver / tagType 颜色映射)
- type='text' → 纯文本(支持 datetime 格式化)
- type='default' → 默认插槽渲染
- type='operation' → 操作列(由 OperationCellRenderer 处理)
-->
<template>
<template v-if="!config || config.type === 'default'">
<slot />
</template>
<template v-else-if="config.type === 'tag'">
<el-tag size="small" :type="resolvedTagType">
{{ displayValue }}
</el-tag>
</template>
<template v-else>
<span>{{ displayValue }}</span>
</template>
</template>
<script setup lang="ts">
import { computed } from 'vue'
import type { ColumnSlotConfig } from '../config/template-config'
import { TAG_RESOLVERS, translate, formatTime } from '../config/template-config'
defineOptions({ name: 'ColumnCellRenderer' })
const props = defineProps<{
/** 列插槽渲染配置 */
config: ColumnSlotConfig | undefined
/** 单元格值 */
value: any
}>()
/** 显示文本:translateField 翻译 > format 格式化 > 原始值 */
const displayValue = computed(() => {
const v = props.value
if (props.config?.translateField) {
return translate(props.config.translateField, v)
}
if (props.config?.format === 'datetime') {
return formatTime(v)
}
return v ?? '—'
})
const resolvedTagType = computed(() => {
if (!props.config || props.config.type !== 'tag') return ''
const { tagType, tagResolver } = props.config
// 优先使用 tagResolver(函数名映射)
if (tagResolver && TAG_RESOLVERS[tagResolver]) {
return TAG_RESOLVERS[tagResolver](props.value)
}
// 其次使用 tagType 映射表(用原始值查找)
if (tagType && typeof tagType === 'object') {
return tagType[String(props.value)] ?? 'info'
}
// 最后使用固定值
return (typeof tagType === 'string' ? tagType : 'info')
})
</script>