feat: 字典标签 自定义颜色

pull/2/head
xingyu 2023-01-16 17:54:20 +08:00
parent cccf8d8197
commit 6e49b19952
5 changed files with 39 additions and 25 deletions

View File

@ -17,6 +17,9 @@ const getDictObj = (dictType: string, value: string) => {
const dictOptions = getDictOptions(dictType) const dictOptions = getDictOptions(dictType)
dictOptions.forEach((dict: DictDataType) => { dictOptions.forEach((dict: DictDataType) => {
if (dict.value === value) { if (dict.value === value) {
if (dict.colorType + '' === 'primary' || dict.colorType + '' === 'default') {
dict.colorType = ''
}
dictData.value = dict dictData.value = dict
} }
}) })
@ -31,25 +34,11 @@ onUpdated(() => {
}) })
</script> </script>
<template> <template>
<!-- 默认样式 -->
<span
v-if="
dictData?.colorType === 'default' ||
dictData?.colorType === '' ||
dictData?.colorType === undefined
"
:key="dictData?.value.toString()"
:class="dictData?.cssClass"
>
{{ dictData?.label }}
</span>
<!-- Tag 样式 -->
<ElTag <ElTag
v-else
:disable-transitions="true" :disable-transitions="true"
:key="dictData?.value + ''" :key="dictData?.value + ''"
:type="dictData?.colorType === 'primary' ? 'success' : dictData?.colorType" :type="dictData?.colorType"
:class="dictData?.cssClass" :color="dictData?.cssClass"
> >
{{ dictData?.label }} {{ dictData?.label }}
</ElTag> </ElTag>

View File

@ -65,6 +65,29 @@ export const useDictStore = defineStore('dict', {
this.isSetDict = true this.isSetDict = true
wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期 wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
} }
},
async resetDict() {
wsCache.delete(CACHE_KEY.DICT_CACHE)
const res = await listSimpleDictDataApi()
// 设置数据
const dictDataMap = new Map<string, any>()
res.forEach((dictData: DictDataVO) => {
// 获得 dictType 层级
const enumValueObj = dictDataMap[dictData.dictType]
if (!enumValueObj) {
dictDataMap[dictData.dictType] = []
}
// 处理 dictValue 层级
dictDataMap[dictData.dictType].push({
value: dictData.value,
label: dictData.label,
colorType: dictData.colorType,
cssClass: dictData.cssClass
})
})
this.dictMap = dictDataMap
this.isSetDict = true
wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
} }
} }
}) })

View File

@ -16,7 +16,7 @@ export interface DictDataType {
dictType: string dictType: string
label: string label: string
value: string | number | boolean value: string | number | boolean
colorType: ElementPlusInfoType | '' | 'default' | 'primary' colorType: ElementPlusInfoType | ''
cssClass: string cssClass: string
} }

View File

@ -7,7 +7,7 @@ import { VxeCrudSchema, useVxeCrudSchemas } from '@/hooks/web/useVxeCrudSchemas'
const { t } = useI18n() const { t } = useI18n()
// 表单校验 // 表单校验
export const dictDataRules = reactive({ export const dictDataRules = reactive({
title: [required], label: [required],
value: [required], value: [required],
sort: [required] sort: [required]
}) })
@ -35,7 +35,7 @@ export const crudSchemas = reactive<VxeCrudSchema>({
field: 'value' field: 'value'
}, },
{ {
title: '颜色类型', title: '标签类型',
field: 'colorType', field: 'colorType',
form: { form: {
component: 'Select', component: 'Select',
@ -67,9 +67,12 @@ export const crudSchemas = reactive<VxeCrudSchema>({
isTable: false isTable: false
}, },
{ {
title: 'CSS Class', title: '颜色',
field: 'cssClass', field: 'cssClass',
isTable: false isTable: false,
form: {
component: 'ColorPicker'
}
}, },
{ {
title: '显示排序', title: '显示排序',

View File

@ -7,7 +7,8 @@ const { t } = useI18n() // 国际化
// 表单校验 // 表单校验
export const dictTypeRules = reactive({ export const dictTypeRules = reactive({
name: [required] name: [required],
type: [required]
}) })
// 新增 + 修改 // 新增 + 修改
const crudSchemas = reactive<VxeCrudSchema>({ const crudSchemas = reactive<VxeCrudSchema>({
@ -41,9 +42,7 @@ const crudSchemas = reactive<VxeCrudSchema>({
field: 'createTime', field: 'createTime',
formatter: 'formatDate', formatter: 'formatDate',
isForm: false, isForm: false,
table: { isTable: false,
width: 150
},
search: { search: {
show: true, show: true,
itemRender: { itemRender: {