diff --git a/apps/web-antd/src/adapter/vxe-table.ts b/apps/web-antd/src/adapter/vxe-table.ts index 0a346d3a..f725fa0d 100644 --- a/apps/web-antd/src/adapter/vxe-table.ts +++ b/apps/web-antd/src/adapter/vxe-table.ts @@ -1,8 +1,9 @@ import { h } from 'vue'; import { setupVbenVxeTable, useVbenVxeGrid } from '@vben/plugins/vxe-table'; +import { useDictStore } from '@vben/stores'; -import { Button, Image } from 'ant-design-vue'; +import { Button, Image, Tag } from 'ant-design-vue'; import { useVbenForm } from './form'; @@ -72,6 +73,32 @@ setupVbenVxeTable({ }, }); + // 表格配置项可以用 cellRender: { name: 'CellDict',props:{dictType: ''} }, + vxeUI.renderer.add('CellDict', { + renderTableDefault(renderOpts, params) { + const dictStore = useDictStore(); + const { props } = renderOpts; + const { column, row } = params; + if (!props) { + return ''; + } + const dict = dictStore.getDictData(props.type, row[column.field]); + // 转义 + if (dict) { + if (`${dict.colorType}` === 'primary') dict.colorType = 'processing'; + else if (`${dict.colorType}` === 'danger') dict.colorType = 'error'; + else if (`${dict.colorType}` === 'info') dict.colorType = 'default'; + else if (!dict.colorType) dict.colorType = 'default'; + return h( + Tag, + { color: dict.colorType }, + { default: () => dict.label }, + ); + } + return ''; + }, + }); + // 这里可以自行扩展 vxe-table 的全局配置,比如自定义格式化 // vxeUI.formats.add }, diff --git a/apps/web-antd/src/views/system/post/post.data.ts b/apps/web-antd/src/views/system/post/post.data.ts index f8851cc0..e42382ec 100644 --- a/apps/web-antd/src/views/system/post/post.data.ts +++ b/apps/web-antd/src/views/system/post/post.data.ts @@ -44,7 +44,11 @@ export const columns: VxeGridProps['columns'] = [ { field: 'code', title: '岗位编码' }, { field: 'sort', title: '岗位顺序' }, { field: 'remark', title: '岗位备注' }, - { field: 'status', title: '状态' }, + { + field: 'status', + title: '状态', + cellRender: { name: 'CellDict', props: { type: 'common_status' } }, + }, { field: 'createTime', formatter: 'formatDateTime', title: '创建时间' }, { field: 'action', diff --git a/packages/stores/src/modules/dict.ts b/packages/stores/src/modules/dict.ts index f95ffd75..800b752e 100644 --- a/packages/stores/src/modules/dict.ts +++ b/packages/stores/src/modules/dict.ts @@ -15,12 +15,22 @@ interface DictState { export const useDictStore = defineStore('dict', { actions: { - getDictData(dictType: string, value?: string) { + getDictData(dictType: string, value: any) { const dict = this.dictCache[dictType]; if (!dict) { return undefined; } - return value ? dict.find((d) => d.value === value) : dict; + return ( + dict.find((d) => d.value === value || d.value === value.toString()) ?? + undefined + ); + }, + getDictOptions(dictType: string) { + const dictOptions = this.dictCache[dictType]; + if (!dictOptions) { + return []; + } + return dictOptions; }, setDictCache(dicts: Dict) { this.dictCache = dicts;