feat(utils): 优化字典数据递归查找功能并替换select为tree-select

重构format.js中的字典查找逻辑,支持递归查找嵌套结构
This commit is contained in:
piexlMax(奇淼
2025-10-16 19:51:21 +08:00
parent e39f1d8c36
commit 1c749b079e
2 changed files with 47 additions and 18 deletions

View File

@@ -219,14 +219,9 @@ func GenerateSearchFormItem(field systemReq.AutoCodeField) string {
if field.FieldType == "array" {
multipleAttr = "multiple "
}
result += fmt.Sprintf(` <el-select %sv-model="searchInfo.%s" clearable filterable placeholder="请选择" @clear="()=>{searchInfo.%s=undefined}">
result += fmt.Sprintf(` <el-tree-select v-model="formData.%s" placeholder="请选择%s" :data="%sOptions" style="width:100%%" filterable :clearable="%v" check-strictly %s></el-tree-select>
`,
multipleAttr, field.FieldJson, field.FieldJson)
result += fmt.Sprintf(` <el-option v-for="(item,key) in %sOptions" :key="key" :label="item.label" :value="item.value" />
`,
field.DictType)
result += ` </el-select>
`
field.FieldJson, field.FieldDesc, field.DictType, field.Clearable, multipleAttr)
} else if field.CheckDataSource {
multipleAttr := ""
if field.DataSource.Association == 2 {
@@ -488,14 +483,9 @@ func GenerateFormItem(field systemReq.AutoCodeField) string {
case "string":
if field.DictType != "" {
result += fmt.Sprintf(` <el-select v-model="formData.%s" placeholder="请选择%s" style="width:100%%" filterable :clearable="%v">
result += fmt.Sprintf(` <el-tree-select v-model="formData.%s" placeholder="请选择%s" :data="%sOptions" style="width:100%%" filterable :clearable="%v" check-strictly></el-tree-select>
`,
field.FieldJson, field.FieldDesc, field.Clearable)
result += fmt.Sprintf(` <el-option v-for="(item,key) in %sOptions" :key="key" :label="item.label" :value="item.value" />
`,
field.DictType)
result += ` </el-select>
`
field.FieldJson, field.FieldDesc, field.DictType, field.Clearable)
} else {
result += fmt.Sprintf(` <el-input v-model="formData.%s" :clearable="%v" placeholder="请输入%s" />
`,

View File

@@ -19,18 +19,57 @@ export const formatDate = (time) => {
}
export const filterDict = (value, options) => {
const rowLabel = options && options.filter((item) => item.value === value)
return rowLabel && rowLabel[0] && rowLabel[0].label
// 递归查找函数
const findInOptions = (opts, targetValue) => {
if (!opts || !Array.isArray(opts)) return null
for (const item of opts) {
if (item.value === targetValue) {
return item
}
if (item.children && Array.isArray(item.children)) {
const found = findInOptions(item.children, targetValue)
if (found) return found
}
}
return null
}
const rowLabel = findInOptions(options, value)
return rowLabel && rowLabel.label
}
export const filterDataSource = (dataSource, value) => {
// 递归查找函数
const findInDataSource = (data, targetValue) => {
if (!data || !Array.isArray(data)) return null
for (const item of data) {
// 检查当前项是否匹配
if (item.value === targetValue) {
return item
}
// 如果有children属性递归查找
if (item.children && Array.isArray(item.children)) {
const found = findInDataSource(item.children, targetValue)
if (found) return found
}
}
return null
}
if (Array.isArray(value)) {
return value.map((item) => {
const rowLabel = dataSource && dataSource.find((i) => i.value === item)
const rowLabel = findInDataSource(dataSource, item)
return rowLabel?.label
})
}
const rowLabel = dataSource && dataSource.find((item) => item.value === value)
const rowLabel = findInDataSource(dataSource, value)
return rowLabel?.label
}