wui-components-v2 1.2.50 → 1.2.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/form-control/form-control.vue +85 -6
- package/components/search/search.vue +5 -1
- package/components/wui-select-popup/wui-select-popup.vue +36 -32
- package/components/wui-tree-cascader-popup/tree-check-node.vue +188 -0
- package/components/wui-tree-cascader-popup/tree-check-panel.vue +252 -0
- package/components/wui-tree-cascader-popup/wui-tree-cascader-popup.vue +407 -0
- package/components/wui-tree-page/components/tree-item.vue +10 -6
- package/package.json +1 -1
|
@@ -14,6 +14,7 @@ import userChoose from '../user-choose/user-choose.vue'
|
|
|
14
14
|
import { generateHighResolutionID } from '../../utils/index'
|
|
15
15
|
import scanInput from '../scan-input/scan-input.vue'
|
|
16
16
|
import { dtmplData, topData } from '../../api/page'
|
|
17
|
+
import WuiTreeCascaderPopup from '../wui-tree-cascader-popup/wui-tree-cascader-popup.vue'
|
|
17
18
|
defineOptions({
|
|
18
19
|
name: 'FormControl',
|
|
19
20
|
})
|
|
@@ -79,8 +80,7 @@ function initFormData() {
|
|
|
79
80
|
}
|
|
80
81
|
|
|
81
82
|
if (ControlTypeSupportor.getControlType(item, props.entity && props.entity[item.sourceId]) === 'relselect') {
|
|
82
|
-
const rawRel =
|
|
83
|
-
(props.entity && props.entity[item.sourceId]) || item.transDefaultValue || item.defaultValue || ''
|
|
83
|
+
const rawRel = (props.entity && props.entity[item.sourceId]) || item.transDefaultValue || item.defaultValue || ''
|
|
84
84
|
// 解析 $$user. 占位符:取当前用户对应扩展字段的值(实体字段为 code@R@name,需保留完整值供回显)
|
|
85
85
|
let resolvedRel: any = rawRel
|
|
86
86
|
if (typeof rawRel === 'string' && rawRel.includes('$$user.')) {
|
|
@@ -133,9 +133,7 @@ function initFormData() {
|
|
|
133
133
|
: defaultVal
|
|
134
134
|
: ''
|
|
135
135
|
return (models[item.sourceId] =
|
|
136
|
-
(entityVal && dayjs(entityVal).valueOf()) ||
|
|
137
|
-
(resolvedDefault && dayjs(resolvedDefault).valueOf()) ||
|
|
138
|
-
null)
|
|
136
|
+
(entityVal && dayjs(entityVal).valueOf()) || (resolvedDefault && dayjs(resolvedDefault).valueOf()) || null)
|
|
139
137
|
}
|
|
140
138
|
|
|
141
139
|
// 评分
|
|
@@ -154,7 +152,8 @@ function initFormData() {
|
|
|
154
152
|
return (models[item.sourceId] = isYes ? '是' : '否')
|
|
155
153
|
}
|
|
156
154
|
|
|
157
|
-
return (models[item.sourceId] =
|
|
155
|
+
return (models[item.sourceId] =
|
|
156
|
+
(props.entity && props.entity[item.sourceId]) || item.transDefaultValue || item.defaultValue || '')
|
|
158
157
|
})
|
|
159
158
|
|
|
160
159
|
// 遍历 models,处理 $$user. 前缀
|
|
@@ -260,9 +259,23 @@ watch(
|
|
|
260
259
|
)
|
|
261
260
|
|
|
262
261
|
// 输入裁剪:对设置了 min/max 的字段,输入值超出范围时自动纠正到边界
|
|
262
|
+
// 仅对「数值型」字段生效,textarea / 文本类字段不参与裁剪,避免破坏文本输入与字数统计
|
|
263
|
+
const NUMERIC_CONTROL_TYPES = new Set([
|
|
264
|
+
'int',
|
|
265
|
+
'float',
|
|
266
|
+
'double',
|
|
267
|
+
'decimal',
|
|
268
|
+
'timestamp',
|
|
269
|
+
'progress',
|
|
270
|
+
'range',
|
|
271
|
+
'intrange',
|
|
272
|
+
'decimalrange',
|
|
273
|
+
])
|
|
263
274
|
const fieldConstraints = computed(() => {
|
|
264
275
|
const map: Record<string, { min?: number; max?: number }> = {}
|
|
265
276
|
for (const item of fields.value) {
|
|
277
|
+
const ctrlType = ControlTypeSupportor.getControlType(item)
|
|
278
|
+
if (!NUMERIC_CONTROL_TYPES.has(ctrlType)) continue
|
|
266
279
|
const rawMin = item.min
|
|
267
280
|
const rawMax = item.max
|
|
268
281
|
const min = rawMin !== undefined && rawMin !== null && rawMin !== '' ? Number(rawMin) : undefined
|
|
@@ -517,6 +530,17 @@ function formatSelectColumns(columns: any) {
|
|
|
517
530
|
})
|
|
518
531
|
}
|
|
519
532
|
|
|
533
|
+
// 文本类字段最大字数:读取字段配置 item.max(数字字符串),
|
|
534
|
+
// 配置了则作为 textarea 的 maxlength;未配置时回退到默认上限 120,
|
|
535
|
+
// 保证 show-word-limit 默认仍显示 "当前/120"
|
|
536
|
+
const DEFAULT_TEXTAREA_MAXLENGTH = 120
|
|
537
|
+
function getFieldMaxlength(item: Fields): number {
|
|
538
|
+
const raw = item.max
|
|
539
|
+
if (raw === undefined || raw === null || raw === '') return DEFAULT_TEXTAREA_MAXLENGTH
|
|
540
|
+
const num = Number(raw)
|
|
541
|
+
return !isNaN(num) && num > 0 ? num : DEFAULT_TEXTAREA_MAXLENGTH
|
|
542
|
+
}
|
|
543
|
+
|
|
520
544
|
const schema = computed((): FormSchema => {
|
|
521
545
|
return {
|
|
522
546
|
validate(_model: Record<string, any>): FormSchemaIssue[] {
|
|
@@ -531,6 +555,35 @@ const schema = computed((): FormSchema => {
|
|
|
531
555
|
issues.push({ path: [item.sourceId], message: `请填写${item.title}` })
|
|
532
556
|
}
|
|
533
557
|
}
|
|
558
|
+
// 文本类 input 字数校验(text / password / textarea):
|
|
559
|
+
// item.min / item.max 作为最小/最大字数限制;数值类字段(int/decimal)走数值范围校验,不在此处理
|
|
560
|
+
const ctrlType = ControlTypeSupportor.getControlType(item, props.entity && props.entity[item.sourceId])
|
|
561
|
+
const isTextInput = ['text', 'password', 'textarea'].includes(ctrlType)
|
|
562
|
+
if (isTextInput && value !== undefined && value !== null && value !== '') {
|
|
563
|
+
const charCount = Array.from(String(value)).length
|
|
564
|
+
// 最小字数
|
|
565
|
+
const minRaw = item.min
|
|
566
|
+
if (minRaw !== undefined && minRaw !== null && minRaw !== '') {
|
|
567
|
+
const minLen = Number(minRaw)
|
|
568
|
+
if (!isNaN(minLen) && minLen > 0 && charCount < minLen) {
|
|
569
|
+
issues.push({
|
|
570
|
+
path: [item.sourceId],
|
|
571
|
+
message: `${item.title}至少输入${minLen}个字`,
|
|
572
|
+
})
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
// 最大字数
|
|
576
|
+
const maxRaw = item.max
|
|
577
|
+
if (maxRaw !== undefined && maxRaw !== null && maxRaw !== '') {
|
|
578
|
+
const maxLen = Number(maxRaw)
|
|
579
|
+
if (!isNaN(maxLen) && maxLen > 0 && charCount > maxLen) {
|
|
580
|
+
issues.push({
|
|
581
|
+
path: [item.sourceId],
|
|
582
|
+
message: `${item.title}最多输入${maxLen}个字`,
|
|
583
|
+
})
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
534
587
|
// 自定义规则校验(item.rules: { message, pattern })
|
|
535
588
|
if (item.rules) {
|
|
536
589
|
const ruleList = Array.isArray(item.rules) ? item.rules : [item.rules]
|
|
@@ -712,6 +765,8 @@ console.log('clearValueMap', clearValueMap)
|
|
|
712
765
|
:clearable="!item.disabled"
|
|
713
766
|
:placeholder="`请输入${item.title}`"
|
|
714
767
|
auto-height
|
|
768
|
+
:maxlength="getFieldMaxlength(item)"
|
|
769
|
+
show-word-limit
|
|
715
770
|
/>
|
|
716
771
|
</wd-form-item>
|
|
717
772
|
<wd-form-item
|
|
@@ -945,6 +1000,7 @@ console.log('clearValueMap', clearValueMap)
|
|
|
945
1000
|
fold-card-model="complex"
|
|
946
1001
|
/>
|
|
947
1002
|
</wd-form-item>
|
|
1003
|
+
|
|
948
1004
|
<!-- done -->
|
|
949
1005
|
<wd-form-item
|
|
950
1006
|
v-else-if="ControlTypeSupportor.getControlType(item, props.entity && props.entity[item.sourceId]) === 'int'"
|
|
@@ -1007,6 +1063,29 @@ console.log('clearValueMap', clearValueMap)
|
|
|
1007
1063
|
/>
|
|
1008
1064
|
</view>
|
|
1009
1065
|
</wd-form-item>
|
|
1066
|
+
<wd-form-item
|
|
1067
|
+
v-else-if="
|
|
1068
|
+
['tree-entity-select', 'ltree-entity-select', 'lab-tree-select'].includes(
|
|
1069
|
+
ControlTypeSupportor.getControlType(item, props.entity && props.entity[item.sourceId])
|
|
1070
|
+
)
|
|
1071
|
+
"
|
|
1072
|
+
:class="{ 'no-border-top': fields.indexOf(item) === 0 }"
|
|
1073
|
+
:prop="item.sourceId"
|
|
1074
|
+
:title="item.title"
|
|
1075
|
+
>
|
|
1076
|
+
<WuiTreeCascaderPopup
|
|
1077
|
+
v-model="model[item.sourceId]"
|
|
1078
|
+
:source-id="item.sourceId"
|
|
1079
|
+
:title="item.title"
|
|
1080
|
+
:select-mode="Number(item.max) === 1 ? 'single' : 'multiple'"
|
|
1081
|
+
:ext-control-type="Number(item.max) === 1 ? 'relselect' : 'ss'"
|
|
1082
|
+
:readonly="item.disabled || item.rowEditType === 'readonly'"
|
|
1083
|
+
:clearable="!item.disabled"
|
|
1084
|
+
:default-select-first="!!item.defaultSelectFirst"
|
|
1085
|
+
:default-value="item.transDefaultValue || item.defaultValue"
|
|
1086
|
+
fold-card-model="complex"
|
|
1087
|
+
/>
|
|
1088
|
+
</wd-form-item>
|
|
1010
1089
|
</view>
|
|
1011
1090
|
</view>
|
|
1012
1091
|
</wd-form>
|
|
@@ -132,7 +132,11 @@ function emitSubmit(fdata: { [key: string]: any }) {
|
|
|
132
132
|
function handleSubmit() {
|
|
133
133
|
if (!formControlRef.value) {
|
|
134
134
|
if (!showSearch.value || !searchCriterion.value?.sourceId) return
|
|
135
|
-
|
|
135
|
+
// 弹窗未挂载(从未打开或已关闭)时,entity 中仍保留着已选中的筛选条件,
|
|
136
|
+
// 不能只提交搜索框关键字,需把搜索框值与 entity 中的筛选值合并后一并提交
|
|
137
|
+
const merged = normalizeForEntity({ ...entity.value })
|
|
138
|
+
merged[searchCriterion.value.sourceId] = search.value
|
|
139
|
+
emitSubmit(merged)
|
|
136
140
|
return
|
|
137
141
|
}
|
|
138
142
|
formControlRef.value.submit().then((fdata: any) => {
|
|
@@ -92,30 +92,30 @@ const config = ref<Config>({
|
|
|
92
92
|
const datas = ref<Entities[]>([])
|
|
93
93
|
const searchData = ref('')
|
|
94
94
|
const enumColumn = ref<Enums>({})
|
|
95
|
-
|
|
95
|
+
// ★ 选择数据:使用「有序数组」保存(值格式:code@R@label)。
|
|
96
|
+
// 若用「对象」以 code 为 key,当 code 为纯数字时会被 JS 按数值重新排序,
|
|
97
|
+
// 导致多选回显/显示顺序错乱(如“第二个选中项显示异常”)。
|
|
98
|
+
const selectData = ref<string[]>([])
|
|
96
99
|
|
|
97
100
|
// ★ 关键修改:使用独立的 ref 管理显示状态(参考 action-popup)
|
|
98
101
|
const show = ref(false)
|
|
99
102
|
|
|
100
103
|
// 格式化显示的选中值
|
|
101
104
|
const displayValue = computed(() => {
|
|
102
|
-
|
|
103
|
-
if (!values.length) return ''
|
|
105
|
+
if (!selectData.value.length) return ''
|
|
104
106
|
|
|
105
107
|
if (props.extControlType === 'relselect' || props.extControlType === 'relselect-extdis') {
|
|
106
|
-
return
|
|
108
|
+
return selectData.value[0]?.split('@R@')[1] || ''
|
|
107
109
|
}
|
|
108
|
-
return
|
|
110
|
+
return selectData.value.map(v => v.split('@R@')[1] ?? '').join(', ')
|
|
109
111
|
})
|
|
110
112
|
|
|
111
113
|
// 格式化选中的数据用于checkbox显示
|
|
112
114
|
const selectDataFormat = computed(() => {
|
|
113
115
|
const newdata: Record<string, string> = {}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
})
|
|
118
|
-
}
|
|
116
|
+
selectData.value.forEach(item => {
|
|
117
|
+
newdata[item.split('@R@')[0]] = item.split('@R@')[0]
|
|
118
|
+
})
|
|
119
119
|
return newdata
|
|
120
120
|
})
|
|
121
121
|
|
|
@@ -124,23 +124,27 @@ watch(
|
|
|
124
124
|
() => props.modelValue,
|
|
125
125
|
val => {
|
|
126
126
|
if (!val || (Array.isArray(val) && !val.length)) {
|
|
127
|
-
selectData.value =
|
|
127
|
+
selectData.value = []
|
|
128
128
|
return
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
const
|
|
131
|
+
const newList: string[] = []
|
|
132
|
+
const seen = new Set<string>()
|
|
133
|
+
const pushItem = (item: string) => {
|
|
134
|
+
const code = item.split('@R@')[0]
|
|
135
|
+
if (code && !seen.has(code)) {
|
|
136
|
+
seen.add(code)
|
|
137
|
+
newList.push(item)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
132
140
|
if (Array.isArray(val)) {
|
|
133
141
|
val.forEach(item => {
|
|
134
|
-
if (item && typeof item === 'string')
|
|
135
|
-
const code = item.split('@R@')[0]
|
|
136
|
-
if (code) newData[code] = item
|
|
137
|
-
}
|
|
142
|
+
if (item && typeof item === 'string') pushItem(item)
|
|
138
143
|
})
|
|
139
144
|
} else if (typeof val === 'string' && val) {
|
|
140
|
-
|
|
141
|
-
if (code) newData[code] = val
|
|
145
|
+
pushItem(val)
|
|
142
146
|
}
|
|
143
|
-
selectData.value =
|
|
147
|
+
selectData.value = newList
|
|
144
148
|
},
|
|
145
149
|
{ immediate: true }
|
|
146
150
|
)
|
|
@@ -191,12 +195,12 @@ const getDefaultValue = (): string | string[] => {
|
|
|
191
195
|
|
|
192
196
|
// ★ 默认选择:数据加载完成后按「默认值」或「第一项」预选并回显
|
|
193
197
|
watch(datas, val => {
|
|
194
|
-
if (!val?.length || hasDefaultSelected.value ||
|
|
198
|
+
if (!val?.length || hasDefaultSelected.value || selectData.value.length > 0) return
|
|
195
199
|
|
|
196
200
|
const buildResult = () =>
|
|
197
201
|
props.extControlType === 'relselect' || props.extControlType === 'relselect-extdis'
|
|
198
|
-
?
|
|
199
|
-
:
|
|
202
|
+
? selectData.value[0]
|
|
203
|
+
: [...selectData.value]
|
|
200
204
|
|
|
201
205
|
// 1) 字段/后端配置的默认值:按 code 在已加载数据中匹配预选
|
|
202
206
|
const def = getDefaultValue()
|
|
@@ -210,7 +214,7 @@ watch(datas, val => {
|
|
|
210
214
|
.filter(Boolean) as Entities[]
|
|
211
215
|
if (matched.length) {
|
|
212
216
|
matched.forEach(item => {
|
|
213
|
-
selectData.value
|
|
217
|
+
selectData.value.push(`${item.code}@R@${item.fieldMap[config.value.primaryColumn.sourceId]}`)
|
|
214
218
|
})
|
|
215
219
|
emit('update:modelValue', buildResult())
|
|
216
220
|
hasDefaultSelected.value = true
|
|
@@ -221,7 +225,7 @@ watch(datas, val => {
|
|
|
221
225
|
// 2) 兜底:自动选第一项
|
|
222
226
|
if (props.defaultSelectFirst) {
|
|
223
227
|
const first = val[0]
|
|
224
|
-
selectData.value
|
|
228
|
+
selectData.value.push(`${first.code}@R@${first.fieldMap[config.value.primaryColumn.sourceId]}`)
|
|
225
229
|
emit('update:modelValue', buildResult())
|
|
226
230
|
hasDefaultSelected.value = true
|
|
227
231
|
}
|
|
@@ -338,28 +342,28 @@ async function queryList(pageNo: number, pageSize: number) {
|
|
|
338
342
|
|
|
339
343
|
// 处理选项变化
|
|
340
344
|
function handleChange(item: Entities) {
|
|
341
|
-
|
|
342
|
-
|
|
345
|
+
const idx = selectData.value.findIndex(v => v.split('@R@')[0] === item.code)
|
|
346
|
+
if (idx >= 0) {
|
|
347
|
+
selectData.value.splice(idx, 1)
|
|
343
348
|
return
|
|
344
349
|
}
|
|
345
350
|
|
|
346
351
|
// 单选模式清空已有选择
|
|
347
352
|
if (props.extControlType === 'relselect' || props.extControlType === 'relselect-extdis') {
|
|
348
|
-
selectData.value =
|
|
353
|
+
selectData.value = []
|
|
349
354
|
}
|
|
350
355
|
|
|
351
|
-
selectData.value
|
|
356
|
+
selectData.value.push(`${item.code}@R@${item.fieldMap[config.value.primaryColumn.sourceId]}`)
|
|
352
357
|
}
|
|
353
358
|
|
|
354
359
|
// 提交确认
|
|
355
360
|
function confirm() {
|
|
356
|
-
|
|
357
|
-
if (!keys.length) {
|
|
361
|
+
if (!selectData.value.length) {
|
|
358
362
|
globalToast.warning(`请选择${props.title}`)
|
|
359
363
|
return
|
|
360
364
|
}
|
|
361
365
|
|
|
362
|
-
const payload =
|
|
366
|
+
const payload = [...selectData.value]
|
|
363
367
|
|
|
364
368
|
// 单选返回字符串,多选返回数组
|
|
365
369
|
let result: string | string[]
|
|
@@ -382,7 +386,7 @@ function cancel() {
|
|
|
382
386
|
|
|
383
387
|
// 清空选择
|
|
384
388
|
function clear() {
|
|
385
|
-
selectData.value =
|
|
389
|
+
selectData.value = []
|
|
386
390
|
emit(
|
|
387
391
|
'update:modelValue',
|
|
388
392
|
props.extControlType === 'relselect' || props.extControlType === 'relselect-extdis' ? '' : []
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, ref } from 'vue'
|
|
3
|
+
import type { TreeCheckNode } from './tree-check-panel.vue'
|
|
4
|
+
|
|
5
|
+
defineOptions({
|
|
6
|
+
name: 'TreeCheckNode',
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
type SelectMode = 'single' | 'multiple'
|
|
10
|
+
|
|
11
|
+
const props = withDefaults(
|
|
12
|
+
defineProps<{
|
|
13
|
+
node: TreeCheckNode
|
|
14
|
+
selectMode?: SelectMode
|
|
15
|
+
selectedKeys: string[]
|
|
16
|
+
loadChildren?: (node: TreeCheckNode) => Promise<TreeCheckNode[]>
|
|
17
|
+
}>(),
|
|
18
|
+
{
|
|
19
|
+
selectMode: 'single',
|
|
20
|
+
loadChildren: undefined,
|
|
21
|
+
}
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
const emit = defineEmits<{
|
|
25
|
+
(e: 'check', node: TreeCheckNode, checked: boolean): void
|
|
26
|
+
(e: 'select', node: TreeCheckNode): void
|
|
27
|
+
(e: 'expand', node: TreeCheckNode): void
|
|
28
|
+
}>()
|
|
29
|
+
|
|
30
|
+
const isExpanded = ref(false)
|
|
31
|
+
|
|
32
|
+
// 是否显示展开箭头:尚未尝试加载(未知是否叶子)或已有子节点或加载中
|
|
33
|
+
const showExpand = computed(() => {
|
|
34
|
+
if (props.node.loading) return true
|
|
35
|
+
if (props.node.loaded) return !!props.node.children?.length
|
|
36
|
+
return true // 未加载过,默认可能有子节点,允许点击展开懒加载
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
const isChecked = computed(() => props.selectedKeys.includes(props.node.id))
|
|
40
|
+
|
|
41
|
+
// 半选:自身未选中,但子节点部分已选中(全部选中时父节点会通过联动加入集合,isChecked 为 true)
|
|
42
|
+
const isIndeterminate = computed(() => {
|
|
43
|
+
if (isChecked.value) return false
|
|
44
|
+
const children = props.node.children || []
|
|
45
|
+
if (children.length === 0) return false
|
|
46
|
+
let checkedCount = 0
|
|
47
|
+
for (const child of children) {
|
|
48
|
+
if (props.selectedKeys.includes(child.id)) checkedCount++
|
|
49
|
+
}
|
|
50
|
+
return checkedCount > 0 && checkedCount < children.length
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
function handleExpand() {
|
|
54
|
+
// 已加载且确认是叶子节点(无子节点),无需展开
|
|
55
|
+
if (props.node.loaded && !props.node.children?.length) return
|
|
56
|
+
isExpanded.value = !isExpanded.value
|
|
57
|
+
emit('expand', props.node)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function handleCheckClick() {
|
|
61
|
+
if (props.selectMode === 'multiple') {
|
|
62
|
+
emit('check', props.node, !isChecked.value)
|
|
63
|
+
} else {
|
|
64
|
+
emit('select', props.node)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
</script>
|
|
68
|
+
|
|
69
|
+
<template>
|
|
70
|
+
<view class="tree-check-node">
|
|
71
|
+
<view class="tree-check-node__row">
|
|
72
|
+
<!-- 展开/折叠图标 -->
|
|
73
|
+
<view class="tree-check-node__expand" @click.stop="handleExpand">
|
|
74
|
+
<text v-if="showExpand" class="tree-check-node__triangle">{{ isExpanded ? '▾' : '▸' }}</text>
|
|
75
|
+
</view>
|
|
76
|
+
|
|
77
|
+
<!-- 勾选框(多选)或单选点 -->
|
|
78
|
+
<view class="tree-check-node__selector" @click.stop="handleCheckClick">
|
|
79
|
+
<template v-if="selectMode === 'multiple'">
|
|
80
|
+
<wd-checkbox
|
|
81
|
+
:model-value="isChecked"
|
|
82
|
+
:indeterminate="isIndeterminate"
|
|
83
|
+
/>
|
|
84
|
+
</template>
|
|
85
|
+
<template v-else>
|
|
86
|
+
<view class="tree-check-node__radio" :class="{ 'is-active': isChecked }" />
|
|
87
|
+
</template>
|
|
88
|
+
</view>
|
|
89
|
+
|
|
90
|
+
<!-- 节点名称 -->
|
|
91
|
+
<view class="tree-check-node__name" @click.stop="handleCheckClick">
|
|
92
|
+
{{ node.name }}
|
|
93
|
+
<text v-if="node.loading" class="tree-check-node__loading">加载中…</text>
|
|
94
|
+
</view>
|
|
95
|
+
</view>
|
|
96
|
+
|
|
97
|
+
<!-- 子节点 -->
|
|
98
|
+
<view v-if="node.children?.length && isExpanded" class="tree-check-node__children">
|
|
99
|
+
<tree-check-node
|
|
100
|
+
v-for="child in node.children"
|
|
101
|
+
:key="child.id"
|
|
102
|
+
:node="child"
|
|
103
|
+
:select-mode="selectMode"
|
|
104
|
+
:selected-keys="selectedKeys"
|
|
105
|
+
:load-children="loadChildren"
|
|
106
|
+
@check="(n, c) => emit('check', n, c)"
|
|
107
|
+
@select="n => emit('select', n)"
|
|
108
|
+
@expand="n => emit('expand', n)"
|
|
109
|
+
/>
|
|
110
|
+
</view>
|
|
111
|
+
</view>
|
|
112
|
+
</template>
|
|
113
|
+
|
|
114
|
+
<style scoped lang="scss">
|
|
115
|
+
.tree-check-node {
|
|
116
|
+
&__row {
|
|
117
|
+
display: flex;
|
|
118
|
+
align-items: center;
|
|
119
|
+
height: 88rpx;
|
|
120
|
+
padding: 0 24rpx;
|
|
121
|
+
cursor: pointer;
|
|
122
|
+
|
|
123
|
+
&:active {
|
|
124
|
+
background-color: #f5f7fa;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
&__expand {
|
|
129
|
+
width: 48rpx;
|
|
130
|
+
height: 88rpx;
|
|
131
|
+
display: flex;
|
|
132
|
+
align-items: center;
|
|
133
|
+
justify-content: center;
|
|
134
|
+
flex-shrink: 0;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
&__triangle {
|
|
138
|
+
display: block;
|
|
139
|
+
font-size: 36rpx;
|
|
140
|
+
line-height: 1;
|
|
141
|
+
color: #666;
|
|
142
|
+
font-weight: 600;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
&__selector {
|
|
146
|
+
width: 48rpx;
|
|
147
|
+
height: 88rpx;
|
|
148
|
+
display: flex;
|
|
149
|
+
align-items: center;
|
|
150
|
+
justify-content: center;
|
|
151
|
+
flex-shrink: 0;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
&__radio {
|
|
155
|
+
width: 36rpx;
|
|
156
|
+
height: 36rpx;
|
|
157
|
+
border-radius: 50%;
|
|
158
|
+
border: 2rpx solid #dcdfe6;
|
|
159
|
+
box-sizing: border-box;
|
|
160
|
+
|
|
161
|
+
&.is-active {
|
|
162
|
+
border-color: #4c7bff;
|
|
163
|
+
background-color: #4c7bff;
|
|
164
|
+
box-shadow: inset 0 0 0 6rpx #fff;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
&__name {
|
|
169
|
+
flex: 1;
|
|
170
|
+
min-width: 0;
|
|
171
|
+
font-size: 30rpx;
|
|
172
|
+
color: #333;
|
|
173
|
+
overflow: hidden;
|
|
174
|
+
text-overflow: ellipsis;
|
|
175
|
+
white-space: nowrap;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
&__loading {
|
|
179
|
+
font-size: 24rpx;
|
|
180
|
+
color: #999;
|
|
181
|
+
margin-left: 12rpx;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
&__children {
|
|
185
|
+
padding-left: 40rpx;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
</style>
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, ref, watch } from 'vue'
|
|
3
|
+
import TreeCheckNode from './tree-check-node.vue'
|
|
4
|
+
|
|
5
|
+
defineOptions({
|
|
6
|
+
name: 'TreeCheckPanel',
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
// 树节点结构
|
|
10
|
+
export interface TreeCheckNode {
|
|
11
|
+
id: string
|
|
12
|
+
name: string
|
|
13
|
+
children?: TreeCheckNode[]
|
|
14
|
+
loaded?: boolean // 是否已尝试懒加载子节点
|
|
15
|
+
loading?: boolean // 是否正在加载子节点
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type SelectMode = 'single' | 'multiple'
|
|
19
|
+
|
|
20
|
+
const props = withDefaults(
|
|
21
|
+
defineProps<{
|
|
22
|
+
/** 树数据(根节点,children 懒加载) */
|
|
23
|
+
treeData: TreeCheckNode[]
|
|
24
|
+
/** 选择模式 */
|
|
25
|
+
selectMode?: SelectMode
|
|
26
|
+
/** 已选中的节点 id 数组 */
|
|
27
|
+
selectedKeys: string[]
|
|
28
|
+
/** 懒加载子节点回调,返回子节点数组 */
|
|
29
|
+
loadChildren?: (node: TreeCheckNode) => Promise<TreeCheckNode[]>
|
|
30
|
+
/** 是否显示底部按钮 */
|
|
31
|
+
showFooter?: boolean
|
|
32
|
+
}>(),
|
|
33
|
+
{
|
|
34
|
+
selectMode: 'single',
|
|
35
|
+
loadChildren: undefined,
|
|
36
|
+
showFooter: true,
|
|
37
|
+
}
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
const emit = defineEmits<{
|
|
41
|
+
(e: 'update:selectedKeys', keys: string[]): void
|
|
42
|
+
(e: 'confirm'): void
|
|
43
|
+
(e: 'cancel'): void
|
|
44
|
+
}>()
|
|
45
|
+
|
|
46
|
+
// 内部勾选集合(多选)
|
|
47
|
+
const innerSelected = ref<string[]>(props.selectedKeys)
|
|
48
|
+
|
|
49
|
+
watch(
|
|
50
|
+
() => props.selectedKeys,
|
|
51
|
+
val => {
|
|
52
|
+
innerSelected.value = [...val]
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
// 单选选中的节点
|
|
57
|
+
const singleSelected = ref<string>(props.selectMode === 'single' ? props.selectedKeys[0] || '' : '')
|
|
58
|
+
|
|
59
|
+
const checkedCount = computed(() => innerSelected.value.length)
|
|
60
|
+
|
|
61
|
+
// 递归收集某节点子树所有 id(含自身)
|
|
62
|
+
function collectSubtreeIds(node: TreeCheckNode): string[] {
|
|
63
|
+
const ids = [node.id]
|
|
64
|
+
node.children?.forEach(child => {
|
|
65
|
+
ids.push(...collectSubtreeIds(child))
|
|
66
|
+
})
|
|
67
|
+
return ids
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 递归查找父节点(返回包含目标 id 的节点的父级数组,从根到父)
|
|
71
|
+
function findParents(nodes: TreeCheckNode[], targetId: string, parents: TreeCheckNode[] = []): TreeCheckNode[] | null {
|
|
72
|
+
for (const node of nodes) {
|
|
73
|
+
if (node.id === targetId) return parents
|
|
74
|
+
if (node.children?.length) {
|
|
75
|
+
const result = findParents(node.children, targetId, [...parents, node])
|
|
76
|
+
if (result) return result
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return null
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 判断父节点下所有直接子节点是否全部选中 / 全部未选 / 半选
|
|
83
|
+
// 注意:只统计子节点,不含父节点自身——当所有子节点都选中时,父节点应自动全选
|
|
84
|
+
function getSubtreeState(node: TreeCheckNode): 'checked' | 'unchecked' | 'indeterminate' {
|
|
85
|
+
const children = node.children || []
|
|
86
|
+
if (children.length === 0) return 'unchecked'
|
|
87
|
+
const set = new Set(innerSelected.value)
|
|
88
|
+
let checkedCount = 0
|
|
89
|
+
for (const child of children) {
|
|
90
|
+
if (set.has(child.id)) checkedCount++
|
|
91
|
+
}
|
|
92
|
+
if (checkedCount === 0) return 'unchecked'
|
|
93
|
+
if (checkedCount === children.length) return 'checked'
|
|
94
|
+
return 'indeterminate'
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 勾选节点后,向上同步祖先节点的选中状态(从最近的父节点到根,逐层向外联动)
|
|
98
|
+
function syncAncestors(targetId: string) {
|
|
99
|
+
const parents = findParents(props.treeData, targetId)
|
|
100
|
+
if (!parents) return
|
|
101
|
+
const set = new Set(innerSelected.value)
|
|
102
|
+
// 先更新自身集合,再从内到外联动祖先,确保每层都基于最新的集合判断
|
|
103
|
+
for (let i = parents.length - 1; i >= 0; i--) {
|
|
104
|
+
const parent = parents[i]
|
|
105
|
+
const state = getSubtreeState(parent)
|
|
106
|
+
if (state === 'checked') {
|
|
107
|
+
set.add(parent.id)
|
|
108
|
+
} else {
|
|
109
|
+
// unchecked 或 indeterminate 时,父节点自身都不应处于选中态
|
|
110
|
+
set.delete(parent.id)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
innerSelected.value = Array.from(set)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 处理节点勾选(多选,带父子联动)
|
|
117
|
+
function handleCheck(node: TreeCheckNode, checked: boolean) {
|
|
118
|
+
if (props.selectMode === 'single') return
|
|
119
|
+
const set = new Set(innerSelected.value)
|
|
120
|
+
if (checked) {
|
|
121
|
+
// 勾选:选中自身 + 全部子孙
|
|
122
|
+
collectSubtreeIds(node).forEach(id => set.add(id))
|
|
123
|
+
} else {
|
|
124
|
+
// 取消:取消自身 + 全部子孙
|
|
125
|
+
collectSubtreeIds(node).forEach(id => set.delete(id))
|
|
126
|
+
}
|
|
127
|
+
innerSelected.value = Array.from(set)
|
|
128
|
+
// 先更新自身集合,再向上联动祖先(syncAncestors 依赖最新 innerSelected)
|
|
129
|
+
syncAncestors(node.id)
|
|
130
|
+
emit('update:selectedKeys', [...innerSelected.value])
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// 处理单选
|
|
134
|
+
function handleSingle(node: TreeCheckNode) {
|
|
135
|
+
if (props.selectMode !== 'single') return
|
|
136
|
+
singleSelected.value = node.id
|
|
137
|
+
emit('update:selectedKeys', [node.id])
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// 展开/加载子节点
|
|
141
|
+
async function toggleExpand(node: TreeCheckNode) {
|
|
142
|
+
// 首次展开且未加载过子节点
|
|
143
|
+
if (!node.loaded && props.loadChildren) {
|
|
144
|
+
node.loading = true
|
|
145
|
+
try {
|
|
146
|
+
const children = await props.loadChildren(node)
|
|
147
|
+
node.children = children
|
|
148
|
+
node.loaded = true
|
|
149
|
+
// 若父节点处于选中态,新加载的子节点自动继承选中
|
|
150
|
+
if (props.selectMode === 'multiple' && innerSelected.value.includes(node.id)) {
|
|
151
|
+
const set = new Set(innerSelected.value)
|
|
152
|
+
children.forEach(child => collectSubtreeIds(child).forEach(id => set.add(id)))
|
|
153
|
+
innerSelected.value = Array.from(set)
|
|
154
|
+
emit('update:selectedKeys', [...innerSelected.value])
|
|
155
|
+
}
|
|
156
|
+
} finally {
|
|
157
|
+
node.loading = false
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// 确认
|
|
163
|
+
function confirm() {
|
|
164
|
+
if (props.selectMode === 'single') {
|
|
165
|
+
emit('update:selectedKeys', singleSelected.value ? [singleSelected.value] : [])
|
|
166
|
+
} else {
|
|
167
|
+
emit('update:selectedKeys', [...innerSelected.value])
|
|
168
|
+
}
|
|
169
|
+
emit('confirm')
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// 取消
|
|
173
|
+
function cancel() {
|
|
174
|
+
emit('cancel')
|
|
175
|
+
}
|
|
176
|
+
</script>
|
|
177
|
+
|
|
178
|
+
<template>
|
|
179
|
+
<view class="tree-check-panel">
|
|
180
|
+
<scroll-view scroll-y class="tree-check-panel__scroll">
|
|
181
|
+
<view v-if="treeData.length === 0" class="tree-check-panel__empty">
|
|
182
|
+
暂无数据
|
|
183
|
+
</view>
|
|
184
|
+
<tree-check-node
|
|
185
|
+
v-for="node in treeData"
|
|
186
|
+
:key="node.id"
|
|
187
|
+
:node="node"
|
|
188
|
+
:select-mode="selectMode"
|
|
189
|
+
:selected-keys="selectMode === 'single' ? [singleSelected] : innerSelected"
|
|
190
|
+
:load-children="loadChildren"
|
|
191
|
+
@check="handleCheck"
|
|
192
|
+
@select="handleSingle"
|
|
193
|
+
@expand="toggleExpand"
|
|
194
|
+
/>
|
|
195
|
+
</scroll-view>
|
|
196
|
+
|
|
197
|
+
<view v-if="showFooter" class="tree-check-panel__footer">
|
|
198
|
+
<text class="tree-check-panel__count">
|
|
199
|
+
已选 {{ checkedCount }} 项
|
|
200
|
+
</text>
|
|
201
|
+
<view class="tree-check-panel__btns">
|
|
202
|
+
<wd-button custom-class="tree-check-panel__btn" size="small" plain @click="cancel">
|
|
203
|
+
取消
|
|
204
|
+
</wd-button>
|
|
205
|
+
<wd-button custom-class="tree-check-panel__btn" size="small" type="primary" @click="confirm">
|
|
206
|
+
确定
|
|
207
|
+
</wd-button>
|
|
208
|
+
</view>
|
|
209
|
+
</view>
|
|
210
|
+
</view>
|
|
211
|
+
</template>
|
|
212
|
+
|
|
213
|
+
<style scoped lang="scss">
|
|
214
|
+
.tree-check-panel {
|
|
215
|
+
display: flex;
|
|
216
|
+
flex-direction: column;
|
|
217
|
+
height: 60vh;
|
|
218
|
+
|
|
219
|
+
&__scroll {
|
|
220
|
+
flex: 1;
|
|
221
|
+
min-height: 0;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
&__empty {
|
|
225
|
+
padding: 60rpx 0;
|
|
226
|
+
text-align: center;
|
|
227
|
+
color: #999;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
&__footer {
|
|
231
|
+
display: flex;
|
|
232
|
+
align-items: center;
|
|
233
|
+
justify-content: space-between;
|
|
234
|
+
padding: 20rpx 32rpx;
|
|
235
|
+
border-top: 1rpx solid #eee;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
&__count {
|
|
239
|
+
font-size: 28rpx;
|
|
240
|
+
color: #666;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
&__btns {
|
|
244
|
+
display: flex;
|
|
245
|
+
gap: 24rpx;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
&__btn {
|
|
249
|
+
margin: 0;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
</style>
|
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, onMounted, ref, watch } from 'vue'
|
|
3
|
+
import { treeConfigApi, treeDataApi, treeNodeDataApi } from '../../api/tree'
|
|
4
|
+
import { pageConfig } from '../../api/page'
|
|
5
|
+
import TreeCheckPanel from './tree-check-panel.vue'
|
|
6
|
+
import type { TreeCheckNode } from './tree-check-panel.vue'
|
|
7
|
+
|
|
8
|
+
defineOptions({
|
|
9
|
+
name: 'WuiTreeCascaderPopup',
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
type SelectMode = 'single' | 'multiple'
|
|
13
|
+
|
|
14
|
+
const props = withDefaults(
|
|
15
|
+
defineProps<{
|
|
16
|
+
/** v-model 绑定值(单选为 "code@R@name",多选为 "code@R@name" 数组) */
|
|
17
|
+
modelValue: string | string[]
|
|
18
|
+
/** 数据源 sourceId(树配置根节点) */
|
|
19
|
+
sourceId: string
|
|
20
|
+
/** 标题 */
|
|
21
|
+
title?: string
|
|
22
|
+
/** 左侧标签文字 */
|
|
23
|
+
label?: string
|
|
24
|
+
/** 占位文本 */
|
|
25
|
+
placeholder?: string
|
|
26
|
+
/** 选择模式:single 单选 / multiple 多选 */
|
|
27
|
+
selectMode?: SelectMode
|
|
28
|
+
/** 只读 */
|
|
29
|
+
readonly?: boolean
|
|
30
|
+
/** 是否可清空 */
|
|
31
|
+
clearable?: boolean
|
|
32
|
+
}>(),
|
|
33
|
+
{
|
|
34
|
+
title: '',
|
|
35
|
+
label: '',
|
|
36
|
+
placeholder: '请选择',
|
|
37
|
+
selectMode: 'single',
|
|
38
|
+
readonly: false,
|
|
39
|
+
clearable: true,
|
|
40
|
+
}
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
const emit = defineEmits<{
|
|
44
|
+
(e: 'update:modelValue', value: string | string[]): void
|
|
45
|
+
(e: 'confirm', value: string | string[]): void
|
|
46
|
+
(e: 'cancel'): void
|
|
47
|
+
(e: 'open'): void
|
|
48
|
+
(e: 'close'): void
|
|
49
|
+
}>()
|
|
50
|
+
|
|
51
|
+
// ---------- 树配置 ----------
|
|
52
|
+
const treeId = ref('') // 树 ID(ttmplConfig.id,用于加载根数据)
|
|
53
|
+
const branchRatmplId = ref('') // 分支模板 id(用于加载子节点)
|
|
54
|
+
const leafRatmplId = ref('') // 叶子模板 id(用于加载叶子子节点)
|
|
55
|
+
const rootGtmplId = ref('') // 根节点模板 id(用于取根节点名称字段)
|
|
56
|
+
const rootNameField = ref('') // 根节点名称字段 sourceId
|
|
57
|
+
const branchNameField = ref('') // 分支/叶子节点名称字段 sourceId
|
|
58
|
+
let configLoaded = false
|
|
59
|
+
|
|
60
|
+
// 参考 control-tree.ts:根数据用 treeDataApi(treeId),名称字段从 pageConfig(rootGtmplId) 的 primaryColumn.sourceId 取
|
|
61
|
+
async function loadTreeConfig() {
|
|
62
|
+
try {
|
|
63
|
+
const res = await treeConfigApi(props.sourceId)
|
|
64
|
+
treeId.value = res.ttmplConfig.id
|
|
65
|
+
branchRatmplId.value = res.ttmplConfig.branchRatmplId
|
|
66
|
+
leafRatmplId.value = res.ttmplConfig.leafRatmplId
|
|
67
|
+
rootGtmplId.value = res.ttmplConfig.rootGtmplId
|
|
68
|
+
|
|
69
|
+
// 根节点名称字段:pageConfig(rootGtmplId).ltmplConfig.primaryColumn.sourceId
|
|
70
|
+
if (rootGtmplId.value) {
|
|
71
|
+
const rootRes = await pageConfig(rootGtmplId.value)
|
|
72
|
+
rootNameField.value = rootRes.ltmplConfig?.primaryColumn?.sourceId || ''
|
|
73
|
+
}
|
|
74
|
+
// 分支/叶子节点名称字段:pageConfig(branchRatmplId).ltmplConfig.primaryColumn.sourceId
|
|
75
|
+
if (branchRatmplId.value) {
|
|
76
|
+
const branchRes = await pageConfig(branchRatmplId.value)
|
|
77
|
+
branchNameField.value = branchRes.ltmplConfig?.primaryColumn?.sourceId || ''
|
|
78
|
+
}
|
|
79
|
+
configLoaded = true
|
|
80
|
+
} catch (error) {
|
|
81
|
+
console.error('[WuiTreeCascaderPopup] 树配置加载失败:', error)
|
|
82
|
+
configLoaded = false
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 把后端节点 { code, fieldMap[nameId] } 转成 TreeCheckNode { id, name }
|
|
87
|
+
// 名称字段:根节点用 rootNameField,子节点用 branchNameField;取不到时兜底 fieldMap 第一个非空字符串
|
|
88
|
+
function mapNode(item: Record<string, any>, nameField = ''): TreeCheckNode {
|
|
89
|
+
let name = ''
|
|
90
|
+
const fieldMap = item.fieldMap || {}
|
|
91
|
+
if (nameField && fieldMap[nameField]) {
|
|
92
|
+
name = fieldMap[nameField]
|
|
93
|
+
} else {
|
|
94
|
+
// 兜底:取 fieldMap 中第一个非空字符串值(不同接口可能没带 primaryColumn.sourceId)
|
|
95
|
+
for (const key of Object.keys(fieldMap)) {
|
|
96
|
+
const val = fieldMap[key]
|
|
97
|
+
if (typeof val === 'string' && val.trim()) {
|
|
98
|
+
name = val
|
|
99
|
+
break
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (!name) name = item.name || ''
|
|
104
|
+
return {
|
|
105
|
+
id: item.code ?? item.id,
|
|
106
|
+
name,
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 加载根节点(参考 tree-page:用 treeId 请求 treeDataApi)
|
|
111
|
+
async function loadRoot(): Promise<TreeCheckNode[]> {
|
|
112
|
+
const res = await treeDataApi(treeId.value || props.sourceId)
|
|
113
|
+
const entities: Record<string, any>[] = res?.entities || []
|
|
114
|
+
return entities.map(item => mapNode(item, rootNameField.value))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 懒加载子节点(用 branchRatmplId / leafRatmplId 请求 /v3/ltmpl/top-data)
|
|
118
|
+
async function loadChildren(node: TreeCheckNode): Promise<TreeCheckNode[]> {
|
|
119
|
+
// 先用分支模板加载,没有分支模板再用叶子模板
|
|
120
|
+
const sourceId = branchRatmplId.value || leafRatmplId.value
|
|
121
|
+
if (!sourceId) return []
|
|
122
|
+
const res = await treeNodeDataApi(sourceId, node.id)
|
|
123
|
+
const entities: Record<string, any>[] = res?.entities || []
|
|
124
|
+
return entities.map(item => mapNode(item, branchNameField.value))
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// ---------- 弹窗开关 ----------
|
|
128
|
+
const show = ref(false)
|
|
129
|
+
const treeData = ref<TreeCheckNode[]>([])
|
|
130
|
+
const treeLoading = ref(false)
|
|
131
|
+
|
|
132
|
+
async function openPopup() {
|
|
133
|
+
if (props.readonly) return
|
|
134
|
+
if (!configLoaded) {
|
|
135
|
+
await loadTreeConfig()
|
|
136
|
+
}
|
|
137
|
+
show.value = true
|
|
138
|
+
emit('open')
|
|
139
|
+
// 每次打开重新加载根节点
|
|
140
|
+
if (treeData.value.length === 0) {
|
|
141
|
+
await refreshTree()
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function refreshTree() {
|
|
146
|
+
treeLoading.value = true
|
|
147
|
+
try {
|
|
148
|
+
treeData.value = await loadRoot()
|
|
149
|
+
} catch (error) {
|
|
150
|
+
console.error('[WuiTreeCascaderPopup] 树数据加载失败:', error)
|
|
151
|
+
} finally {
|
|
152
|
+
treeLoading.value = false
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function closePopup() {
|
|
157
|
+
show.value = false
|
|
158
|
+
emit('close')
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ---------- 选中值管理 ----------
|
|
162
|
+
const singleSelected = ref<{ value: string; text: string } | null>(null)
|
|
163
|
+
const multiSelected = ref<{ value: string; text: string }[]>([])
|
|
164
|
+
|
|
165
|
+
// "code@R@name" -> {value, text}
|
|
166
|
+
function parseNodeStr(str: string): { value: string; text: string } {
|
|
167
|
+
const idx = str.indexOf('@R@')
|
|
168
|
+
if (idx > -1) {
|
|
169
|
+
return { value: str.slice(0, idx), text: str.slice(idx + 3) }
|
|
170
|
+
}
|
|
171
|
+
return { value: str, text: str }
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// 外部 modelValue 同步到内部
|
|
175
|
+
watch(
|
|
176
|
+
() => props.modelValue,
|
|
177
|
+
val => {
|
|
178
|
+
if (props.selectMode === 'multiple') {
|
|
179
|
+
if (Array.isArray(val)) {
|
|
180
|
+
multiSelected.value = val.map(parseNodeStr)
|
|
181
|
+
} else if (val) {
|
|
182
|
+
multiSelected.value = String(val).split('@,@').filter(Boolean).map(parseNodeStr)
|
|
183
|
+
} else {
|
|
184
|
+
multiSelected.value = []
|
|
185
|
+
}
|
|
186
|
+
} else {
|
|
187
|
+
const v = Array.isArray(val) ? val[0] : val
|
|
188
|
+
singleSelected.value = v ? parseNodeStr(String(v)) : null
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
{ immediate: true }
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
// 格式化显示
|
|
195
|
+
const displayValue = computed(() => {
|
|
196
|
+
if (props.selectMode === 'multiple') {
|
|
197
|
+
return multiSelected.value.map(n => n.text).join(', ')
|
|
198
|
+
}
|
|
199
|
+
return singleSelected.value?.text || ''
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
// 面板选中的 id 数组
|
|
203
|
+
const panelSelectedKeys = ref<string[]>([])
|
|
204
|
+
|
|
205
|
+
// 每次打开面板时,把当前选中值同步给面板
|
|
206
|
+
watch(show, val => {
|
|
207
|
+
if (val) {
|
|
208
|
+
panelSelectedKeys.value = multiSelected.value.map(n => n.value)
|
|
209
|
+
}
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
function handlePanelConfirm() {
|
|
213
|
+
if (props.selectMode === 'multiple') {
|
|
214
|
+
// 用面板勾选的 id 回查文本(若树未展开到该节点,则沿用已有记录或 id 本身)
|
|
215
|
+
const selected: { value: string; text: string }[] = []
|
|
216
|
+
panelSelectedKeys.value.forEach(id => {
|
|
217
|
+
const existing = multiSelected.value.find(n => n.value === id)
|
|
218
|
+
const node = findNodeById(treeData.value, id)
|
|
219
|
+
const text = node?.name ?? existing?.text ?? id
|
|
220
|
+
selected.push({ value: id, text })
|
|
221
|
+
})
|
|
222
|
+
multiSelected.value = selected
|
|
223
|
+
console.log('[WuiTreeCascaderPopup] selected:', selected)
|
|
224
|
+
emit('update:modelValue', selected.map(n => `${n.value}@R@${n.text}`))
|
|
225
|
+
emit('confirm', selected.map(n => `${n.value}@R@${n.text}`))
|
|
226
|
+
} else {
|
|
227
|
+
const id = panelSelectedKeys.value[0]
|
|
228
|
+
if (id) {
|
|
229
|
+
const node = findNodeById(treeData.value, id)
|
|
230
|
+
const text = node?.name ?? id
|
|
231
|
+
singleSelected.value = { value: id, text }
|
|
232
|
+
const val = `${id}@R@${text}`
|
|
233
|
+
emit('update:modelValue', val)
|
|
234
|
+
emit('confirm', val)
|
|
235
|
+
} else {
|
|
236
|
+
singleSelected.value = null
|
|
237
|
+
emit('update:modelValue', '')
|
|
238
|
+
emit('confirm', '')
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
closePopup()
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// 递归查找节点
|
|
245
|
+
function findNodeById(nodes: TreeCheckNode[], id: string): TreeCheckNode | null {
|
|
246
|
+
for (const node of nodes) {
|
|
247
|
+
if (node.id === id) return node
|
|
248
|
+
if (node.children?.length) {
|
|
249
|
+
const found = findNodeById(node.children, id)
|
|
250
|
+
if (found) return found
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return null
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function cancel() {
|
|
257
|
+
emit('cancel')
|
|
258
|
+
closePopup()
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function clear() {
|
|
262
|
+
if (props.selectMode === 'multiple') {
|
|
263
|
+
multiSelected.value = []
|
|
264
|
+
emit('update:modelValue', [])
|
|
265
|
+
} else {
|
|
266
|
+
singleSelected.value = null
|
|
267
|
+
emit('update:modelValue', '')
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
onMounted(() => {
|
|
272
|
+
loadTreeConfig()
|
|
273
|
+
})
|
|
274
|
+
</script>
|
|
275
|
+
|
|
276
|
+
<template>
|
|
277
|
+
<view class="wui-tree-cascader-popup-wrapper">
|
|
278
|
+
<!-- ★ 触发区域 -->
|
|
279
|
+
<view class="trigger-cell" @click="openPopup">
|
|
280
|
+
<text v-if="label" class="cell-label dark:text-white">
|
|
281
|
+
{{ label }}
|
|
282
|
+
</text>
|
|
283
|
+
|
|
284
|
+
<view class="cell-content dark:text-white">
|
|
285
|
+
<text v-if="displayValue" class="cell-value">
|
|
286
|
+
{{ displayValue }}
|
|
287
|
+
</text>
|
|
288
|
+
<text v-else class="cell-placeholder">
|
|
289
|
+
{{ placeholder }}
|
|
290
|
+
</text>
|
|
291
|
+
</view>
|
|
292
|
+
|
|
293
|
+
<view class="cell-arrow">
|
|
294
|
+
<wd-icon v-if="clearable && !readonly && displayValue" name="close-circle" size="16px" @click.stop="clear" />
|
|
295
|
+
<wd-icon v-else-if="!readonly" name="right" size="16px" />
|
|
296
|
+
</view>
|
|
297
|
+
</view>
|
|
298
|
+
|
|
299
|
+
<!-- ★ 树形多选弹窗 -->
|
|
300
|
+
<wd-popup
|
|
301
|
+
v-model="show"
|
|
302
|
+
position="bottom"
|
|
303
|
+
:z-index="999999"
|
|
304
|
+
custom-style="height:80vh;width:100vw;border-radius:24rpx 24rpx 0 0;overflow:hidden;"
|
|
305
|
+
safe-area-inset-bottom
|
|
306
|
+
@close="closePopup"
|
|
307
|
+
>
|
|
308
|
+
<view class="tree-popup">
|
|
309
|
+
<view class="tree-popup__header">
|
|
310
|
+
<text class="tree-popup__title">
|
|
311
|
+
选择{{ title || label }}
|
|
312
|
+
</text>
|
|
313
|
+
</view>
|
|
314
|
+
<view v-if="treeLoading" class="tree-popup__loading">
|
|
315
|
+
<wd-loading />
|
|
316
|
+
</view>
|
|
317
|
+
<tree-check-panel
|
|
318
|
+
v-else
|
|
319
|
+
:tree-data="treeData"
|
|
320
|
+
:select-mode="selectMode"
|
|
321
|
+
:selected-keys="panelSelectedKeys"
|
|
322
|
+
:load-children="loadChildren"
|
|
323
|
+
@update:selected-keys="val => (panelSelectedKeys = val)"
|
|
324
|
+
@confirm="handlePanelConfirm"
|
|
325
|
+
@cancel="cancel"
|
|
326
|
+
/>
|
|
327
|
+
</view>
|
|
328
|
+
</wd-popup>
|
|
329
|
+
</view>
|
|
330
|
+
</template>
|
|
331
|
+
|
|
332
|
+
<style scoped lang="scss">
|
|
333
|
+
.wui-tree-cascader-popup-wrapper {
|
|
334
|
+
width: 100%;
|
|
335
|
+
position: relative;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// ★ 触发区域
|
|
339
|
+
.trigger-cell {
|
|
340
|
+
display: flex;
|
|
341
|
+
align-items: center;
|
|
342
|
+
justify-content: space-between;
|
|
343
|
+
cursor: pointer;
|
|
344
|
+
position: relative;
|
|
345
|
+
transition: background-color 0.2s;
|
|
346
|
+
|
|
347
|
+
&:active {
|
|
348
|
+
background-color: #f5f7fa;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
.cell-label {
|
|
352
|
+
flex-shrink: 0;
|
|
353
|
+
margin-right: 24rpx;
|
|
354
|
+
white-space: nowrap;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
.cell-content {
|
|
358
|
+
flex: 1;
|
|
359
|
+
min-width: 0;
|
|
360
|
+
|
|
361
|
+
.cell-value {
|
|
362
|
+
text-align: left;
|
|
363
|
+
overflow: hidden;
|
|
364
|
+
text-overflow: ellipsis;
|
|
365
|
+
white-space: nowrap;
|
|
366
|
+
display: block;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
.cell-placeholder {
|
|
370
|
+
color: #c0c4cc;
|
|
371
|
+
text-align: left;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
.cell-arrow {
|
|
376
|
+
flex-shrink: 0;
|
|
377
|
+
margin-left: 16rpx;
|
|
378
|
+
display: flex;
|
|
379
|
+
align-items: center;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
.tree-popup {
|
|
384
|
+
height: 80vh;
|
|
385
|
+
display: flex;
|
|
386
|
+
flex-direction: column;
|
|
387
|
+
|
|
388
|
+
&__header {
|
|
389
|
+
padding: 28rpx 32rpx;
|
|
390
|
+
border-bottom: 1rpx solid #eee;
|
|
391
|
+
text-align: left;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
&__title {
|
|
395
|
+
font-size: 34rpx;
|
|
396
|
+
font-weight: 600;
|
|
397
|
+
color: #333;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
&__loading {
|
|
401
|
+
flex: 1;
|
|
402
|
+
display: flex;
|
|
403
|
+
align-items: center;
|
|
404
|
+
justify-content: center;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
</style>
|
|
@@ -99,9 +99,9 @@ function emitUpdate(e: TreeNode) {
|
|
|
99
99
|
}
|
|
100
100
|
"
|
|
101
101
|
>
|
|
102
|
-
<text v-if="node.type !== 'leaf'" class="arrow"
|
|
102
|
+
<text v-if="node.type !== 'leaf'" class="arrow">
|
|
103
103
|
<wd-loading v-if="node.loading" size="16" />
|
|
104
|
-
<
|
|
104
|
+
<text v-else class="arrow-triangle">{{ node.expanded ? '▾' : '▸' }}</text>
|
|
105
105
|
</text>
|
|
106
106
|
<text v-else class="dot dark:text-white">•</text>
|
|
107
107
|
</view>
|
|
@@ -191,12 +191,16 @@ function emitUpdate(e: TreeNode) {
|
|
|
191
191
|
}
|
|
192
192
|
|
|
193
193
|
.arrow {
|
|
194
|
-
|
|
195
|
-
|
|
194
|
+
display: inline-flex;
|
|
195
|
+
align-items: center;
|
|
196
|
+
justify-content: center;
|
|
196
197
|
}
|
|
197
198
|
|
|
198
|
-
.arrow
|
|
199
|
-
|
|
199
|
+
.arrow-triangle {
|
|
200
|
+
font-size: 28rpx;
|
|
201
|
+
line-height: 1;
|
|
202
|
+
color: #999;
|
|
203
|
+
font-weight: 600;
|
|
200
204
|
}
|
|
201
205
|
|
|
202
206
|
.dot {
|