Skip to content

Commit 1c749b0

Browse files
author
piexlMax(奇淼
committed
feat(utils): 优化字典数据递归查找功能并替换select为tree-select
重构format.js中的字典查找逻辑,支持递归查找嵌套结构
1 parent e39f1d8 commit 1c749b0

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

server/utils/autocode/template_funcs.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,9 @@ func GenerateSearchFormItem(field systemReq.AutoCodeField) string {
219219
if field.FieldType == "array" {
220220
multipleAttr = "multiple "
221221
}
222-
result += fmt.Sprintf(` <el-select %sv-model="searchInfo.%s" clearable filterable placeholder="请选择" @clear="()=>{searchInfo.%s=undefined}">
222+
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>
223223
`,
224-
multipleAttr, field.FieldJson, field.FieldJson)
225-
result += fmt.Sprintf(` <el-option v-for="(item,key) in %sOptions" :key="key" :label="item.label" :value="item.value" />
226-
`,
227-
field.DictType)
228-
result += ` </el-select>
229-
`
224+
field.FieldJson, field.FieldDesc, field.DictType, field.Clearable, multipleAttr)
230225
} else if field.CheckDataSource {
231226
multipleAttr := ""
232227
if field.DataSource.Association == 2 {
@@ -488,14 +483,9 @@ func GenerateFormItem(field systemReq.AutoCodeField) string {
488483

489484
case "string":
490485
if field.DictType != "" {
491-
result += fmt.Sprintf(` <el-select v-model="formData.%s" placeholder="请选择%s" style="width:100%%" filterable :clearable="%v">
486+
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>
492487
`,
493-
field.FieldJson, field.FieldDesc, field.Clearable)
494-
result += fmt.Sprintf(` <el-option v-for="(item,key) in %sOptions" :key="key" :label="item.label" :value="item.value" />
495-
`,
496-
field.DictType)
497-
result += ` </el-select>
498-
`
488+
field.FieldJson, field.FieldDesc, field.DictType, field.Clearable)
499489
} else {
500490
result += fmt.Sprintf(` <el-input v-model="formData.%s" :clearable="%v" placeholder="请输入%s" />
501491
`,

web/src/utils/format.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,57 @@ export const formatDate = (time) => {
1919
}
2020

2121
export const filterDict = (value, options) => {
22-
const rowLabel = options && options.filter((item) => item.value === value)
23-
return rowLabel && rowLabel[0] && rowLabel[0].label
22+
// 递归查找函数
23+
const findInOptions = (opts, targetValue) => {
24+
if (!opts || !Array.isArray(opts)) return null
25+
26+
for (const item of opts) {
27+
if (item.value === targetValue) {
28+
return item
29+
}
30+
31+
if (item.children && Array.isArray(item.children)) {
32+
const found = findInOptions(item.children, targetValue)
33+
if (found) return found
34+
}
35+
}
36+
37+
return null
38+
}
39+
40+
const rowLabel = findInOptions(options, value)
41+
return rowLabel && rowLabel.label
2442
}
2543

2644
export const filterDataSource = (dataSource, value) => {
45+
// 递归查找函数
46+
const findInDataSource = (data, targetValue) => {
47+
if (!data || !Array.isArray(data)) return null
48+
49+
for (const item of data) {
50+
// 检查当前项是否匹配
51+
if (item.value === targetValue) {
52+
return item
53+
}
54+
55+
// 如果有children属性,递归查找
56+
if (item.children && Array.isArray(item.children)) {
57+
const found = findInDataSource(item.children, targetValue)
58+
if (found) return found
59+
}
60+
}
61+
62+
return null
63+
}
64+
2765
if (Array.isArray(value)) {
2866
return value.map((item) => {
29-
const rowLabel = dataSource && dataSource.find((i) => i.value === item)
67+
const rowLabel = findInDataSource(dataSource, item)
3068
return rowLabel?.label
3169
})
3270
}
33-
const rowLabel = dataSource && dataSource.find((item) => item.value === value)
71+
72+
const rowLabel = findInDataSource(dataSource, value)
3473
return rowLabel?.label
3574
}
3675

0 commit comments

Comments
 (0)