feat: vxe table dict tag

pull/49/head
xingyu4j 2024-11-26 12:40:37 +08:00
parent b7465498dc
commit 6c40712df4
3 changed files with 45 additions and 4 deletions

View File

@ -1,8 +1,9 @@
import { h } from 'vue'; import { h } from 'vue';
import { setupVbenVxeTable, useVbenVxeGrid } from '@vben/plugins/vxe-table'; 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'; 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 的全局配置,比如自定义格式化 // 这里可以自行扩展 vxe-table 的全局配置,比如自定义格式化
// vxeUI.formats.add // vxeUI.formats.add
}, },

View File

@ -44,7 +44,11 @@ export const columns: VxeGridProps['columns'] = [
{ field: 'code', title: '岗位编码' }, { field: 'code', title: '岗位编码' },
{ field: 'sort', title: '岗位顺序' }, { field: 'sort', title: '岗位顺序' },
{ field: 'remark', title: '岗位备注' }, { field: 'remark', title: '岗位备注' },
{ field: 'status', title: '状态' }, {
field: 'status',
title: '状态',
cellRender: { name: 'CellDict', props: { type: 'common_status' } },
},
{ field: 'createTime', formatter: 'formatDateTime', title: '创建时间' }, { field: 'createTime', formatter: 'formatDateTime', title: '创建时间' },
{ {
field: 'action', field: 'action',

View File

@ -15,12 +15,22 @@ interface DictState {
export const useDictStore = defineStore('dict', { export const useDictStore = defineStore('dict', {
actions: { actions: {
getDictData(dictType: string, value?: string) { getDictData(dictType: string, value: any) {
const dict = this.dictCache[dictType]; const dict = this.dictCache[dictType];
if (!dict) { if (!dict) {
return undefined; 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) { setDictCache(dicts: Dict) {
this.dictCache = dicts; this.dictCache = dicts;