🍉 feat(sysDict): 增加自定义数据源功能
This commit is contained in:
parent
6557895033
commit
47ebee8f3e
@ -91,6 +91,7 @@ function isMultipleModel(value: any): value is MultipleModel {
|
||||
<script setup lang="ts">
|
||||
import { reactive, watch, computed, PropType } from 'vue';
|
||||
import { useUserInfo } from '/@/stores/userInfo';
|
||||
import {Nullable} from "/@/types/global";
|
||||
|
||||
const userStore = useUserInfo();
|
||||
const emit = defineEmits(['update:modelValue', 'change']);
|
||||
@ -126,7 +127,16 @@ const props = defineProps({
|
||||
*/
|
||||
code: {
|
||||
type: String,
|
||||
required: true,
|
||||
required: false,
|
||||
},
|
||||
/**
|
||||
* 直接传入的字典数据源(优先级高于code)
|
||||
* @type {DictItem[]}
|
||||
* @example [{ label: '选项1', value: '1' }, { label: '选项2', value: '2' }]
|
||||
*/
|
||||
data: {
|
||||
type: Array as PropType<DictItem[]>,
|
||||
default: () => [],
|
||||
},
|
||||
/**
|
||||
* 是否为常量字典(true从常量列表获取,false从字典列表获取)
|
||||
@ -286,8 +296,17 @@ const currentDictItems = computed(() => {
|
||||
*/
|
||||
const getDataList = (): DictItem[] => {
|
||||
try {
|
||||
if (!props.code) {
|
||||
console.error('[g-sys-dict] 字典编码不能为空');
|
||||
// 如果提供了 data 数据源,优先使用
|
||||
if (props.data && props.data.length > 0) {
|
||||
return props.data.map((item: any) => ({
|
||||
...item,
|
||||
label: item[props.propLabel] ?? [item.name, item.desc].filter((x) => x).join('-'),
|
||||
value: item[props.propValue] ?? item.code,
|
||||
}));
|
||||
}
|
||||
|
||||
if (!props.code ) {
|
||||
console.error('[g-sys-dict] code和data不能同时为空');
|
||||
return [];
|
||||
}
|
||||
|
||||
@ -475,6 +494,14 @@ const getDisplayText = (dict?: DictItem): string => {
|
||||
* @function
|
||||
*/
|
||||
const initData = () => {
|
||||
// 验证 code 和 data 不能同时为空
|
||||
if (!props.code && (!props.data || props.data.length === 0)) {
|
||||
console.error('[g-sys-dict] code和data不能同时为空');
|
||||
state.dictData = [];
|
||||
state.value = props.multiple ? [] : null;
|
||||
return;
|
||||
}
|
||||
|
||||
state.dictData = getDataList();
|
||||
processNumericValues(props.modelValue);
|
||||
const initialValue = parseMultipleValue(props.modelValue);
|
||||
@ -522,7 +549,7 @@ watch(
|
||||
validateInitialValue();
|
||||
}
|
||||
);
|
||||
watch(() => [userStore.dictList, userStore.constList, state], initData, { immediate: true });
|
||||
watch(() => [userStore.dictList, userStore.constList, props.data, state], initData, { immediate: true });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user