优化资产分类自定义属性的字典类型匹配逻辑,新增dictType字段用于精确匹配字典数据,避免字典名称修改后导致的匹配失败,同时在编辑时自动同步最新的字典名称
This commit is contained in:
@@ -84,7 +84,7 @@
|
|||||||
:max-collapse-tags="2"
|
:max-collapse-tags="2"
|
||||||
>
|
>
|
||||||
<el-option
|
<el-option
|
||||||
v-for="(item, idx) in getDictValuesByType(attr.name)"
|
v-for="(item, idx) in getDictValuesByType(attr.name, attr.dictType)"
|
||||||
:key="idx"
|
:key="idx"
|
||||||
:label="item.value"
|
:label="item.value"
|
||||||
:value="item.key"
|
:value="item.key"
|
||||||
@@ -145,10 +145,12 @@ interface CustomAttr {
|
|||||||
sort?: number;
|
sort?: number;
|
||||||
dictKey?: string;
|
dictKey?: string;
|
||||||
dictValues?: string[];
|
dictValues?: string[];
|
||||||
|
dictType?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface DictInfo {
|
interface DictInfo {
|
||||||
name: string;
|
name: string;
|
||||||
|
type: string;
|
||||||
remark: string;
|
remark: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,7 +212,7 @@ const fetchAttrTypeOptions = () => {
|
|||||||
// 获取字典类型数据
|
// 获取字典类型数据
|
||||||
const fetchDictTypeOptions = () => {
|
const fetchDictTypeOptions = () => {
|
||||||
dictLoading.value = true;
|
dictLoading.value = true;
|
||||||
getDicts('assets')
|
return getDicts('assets')
|
||||||
.then((res: any) => {
|
.then((res: any) => {
|
||||||
const list = res.data?.list ?? [];
|
const list = res.data?.list ?? [];
|
||||||
// 提取所有字典类型信息
|
// 提取所有字典类型信息
|
||||||
@@ -231,11 +233,18 @@ const fetchDictTypeOptions = () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// 根据字典类型获取对应的字典值
|
// 根据字典类型获取对应的字典值(优先使用 dictType 匹配)
|
||||||
const getDictValuesByType = (dictKey: string) => {
|
const getDictValuesByType = (dictName: string, dictType?: string) => {
|
||||||
if (!dictKey) return [];
|
if (!dictName && !dictType) return [];
|
||||||
// 根据字典类型名称找到对应的字典数据
|
// 优先使用 dictType 匹配,这样即使字典名称修改也能正确匹配
|
||||||
const dictItem = dictValueOptions.value.find((item: any) => item.info?.name === dictKey);
|
let dictItem;
|
||||||
|
if (dictType) {
|
||||||
|
dictItem = dictValueOptions.value.find((item: any) => item.info?.type === dictType);
|
||||||
|
}
|
||||||
|
// 如果 dictType 没匹配到,再用 name 匹配(兼容旧数据)
|
||||||
|
if (!dictItem && dictName) {
|
||||||
|
dictItem = dictValueOptions.value.find((item: any) => item.info?.name === dictName);
|
||||||
|
}
|
||||||
return dictItem?.values ?? [];
|
return dictItem?.values ?? [];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -246,13 +255,19 @@ const isDictType = (type?: string) => type === 'select' || type === 'multi_selec
|
|||||||
const onDictKeyChange = (attr: CustomAttr) => {
|
const onDictKeyChange = (attr: CustomAttr) => {
|
||||||
// 清空已选的字典值
|
// 清空已选的字典值
|
||||||
attr.options = [];
|
attr.options = [];
|
||||||
|
// 根据选择的字典名称,找到对应的 dictType 并保存
|
||||||
|
const selectedDict = dictTypeOptions.value.find((item) => item.name === attr.name);
|
||||||
|
attr.dictType = selectedDict?.type || '';
|
||||||
};
|
};
|
||||||
|
|
||||||
// 判断字典选项是否应被禁用
|
// 判断字典选项是否应被禁用
|
||||||
const isDictOptionDisabled = (dictName: string, currentAttr: CustomAttr) => {
|
const isDictOptionDisabled = (dictName: string, currentAttr: CustomAttr) => {
|
||||||
if (!dictName) return false;
|
if (!dictName) return false;
|
||||||
// 检查该字典名称是否已被其他属性使用
|
// 找到该字典对应的 type
|
||||||
return ruleForm.attrs.some((attr) => attr !== currentAttr && isDictType(attr.type) && attr.name === dictName);
|
const dictInfo = dictTypeOptions.value.find((item) => item.name === dictName);
|
||||||
|
const dictType = dictInfo?.type || '';
|
||||||
|
// 检查该字典是否已被其他属性使用(使用 dictType 判断)
|
||||||
|
return ruleForm.attrs.some((attr) => attr !== currentAttr && isDictType(attr.type) && (attr.dictType === dictType || (!attr.dictType && attr.name === dictName)));
|
||||||
};
|
};
|
||||||
|
|
||||||
// 添加自定义属性
|
// 添加自定义属性
|
||||||
@@ -335,9 +350,19 @@ const openDialog = (row?: CategoryRow | string, edit?: boolean) => {
|
|||||||
}
|
}
|
||||||
return { ...attr, options: options || [] };
|
return { ...attr, options: options || [] };
|
||||||
});
|
});
|
||||||
// 如果有单选/多选属性,预加载字典类型数据
|
// 如果有单选/多选属性,预加载字典类型数据并更新字典名称
|
||||||
if (ruleForm.attrs.some((attr: CustomAttr) => attr.type === 'select' || attr.type === 'multi_select')) {
|
if (ruleForm.attrs.some((attr: CustomAttr) => attr.type === 'select' || attr.type === 'multi_select')) {
|
||||||
fetchDictTypeOptions();
|
fetchDictTypeOptions().then(() => {
|
||||||
|
// 根据 dictType 更新字典名称(字典名称可能已被修改)
|
||||||
|
ruleForm.attrs.forEach((attr: CustomAttr) => {
|
||||||
|
if (isDictType(attr.type) && attr.dictType) {
|
||||||
|
const dictInfo = dictTypeOptions.value.find((item) => item.type === attr.dictType);
|
||||||
|
if (dictInfo) {
|
||||||
|
attr.name = dictInfo.name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if (row && typeof row === 'string') {
|
} else if (row && typeof row === 'string') {
|
||||||
@@ -368,7 +393,7 @@ const formatDictOptions = (attr: CustomAttr) => {
|
|||||||
value: opt.value ?? opt.key ?? '',
|
value: opt.value ?? opt.key ?? '',
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
const dictValues = getDictValuesByType(attr.name || '');
|
const dictValues = getDictValuesByType(attr.name || '', attr.dictType);
|
||||||
return options.map((optValue: string) => {
|
return options.map((optValue: string) => {
|
||||||
const dictItem = dictValues.find((d: any) => d.key === optValue);
|
const dictItem = dictValues.find((d: any) => d.key === optValue);
|
||||||
return {
|
return {
|
||||||
@@ -396,6 +421,7 @@ const onSubmit = () => {
|
|||||||
return {
|
return {
|
||||||
...base,
|
...base,
|
||||||
name: attr.name || '',
|
name: attr.name || '',
|
||||||
|
dictType: attr.dictType || '',
|
||||||
options: formatDictOptions(attr),
|
options: formatDictOptions(attr),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user