index.vue
4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<template>
<next-tree
:changeVerify="changeVerify"
:title="getTitle"
ref="nextTreeRef"
:checkStrictly="checkStrictly"
:selectParent="selectParent"
:multiple="multiple"
:treeData="filteredTreeData"
@cancel="close"
@confirm="onconfirm">
<template #topBar>
<view class="search-container">
<view class="search-box">
<text class="search-icon">🔍</text>
<input
class="search-input"
placeholder="搜索处置场所负责人姓名"
v-model="searchText" />
<text
v-if="searchText"
class="clear-icon"
@click="clearSearch">✕</text>
</view>
</view>
</template>
</next-tree>
</template>
<script setup>import { nextTick, ref, unref, computed } from 'vue';
// @ts-ignore
import nextTree from '../next-tree/next-tree.vue';
const props = defineProps({
dataList: {
type: Array,
default: []
},
valueKey: {
type: String,
default: 'id'
},
multiple: {
type: Boolean,
default: true
},
selectParent: {
type: Boolean,
default: false
},
checkStrictly: {
type: Boolean,
default: false
},
onconfirm: {
type: Function,
default: () => { }
}
})
const treeData = ref([])
const nextTreeRef = ref()
const searchText = ref('')
// 添加计算属性用于过滤数据
const filteredTreeData = computed(() => {
if (!searchText.value) {
return treeData.value
}
return treeData.value.map(item => {
const filteredChildren = item.children.filter(child =>
child.label.toLowerCase().includes(searchText.value.toLowerCase())
)
return {
...item,
children: filteredChildren
}
}).filter(item => item.children.length > 0)
})
function getTitle(checked) {
return `已选:${checked.length}位处置场所负责人`
}
function changeVerify(current, chooseList) {
// 注意:返回非空字符串会阻止原有行为,并提示返回的字符串
// 如果函数体不做return返回值,即验证通过,控件正常处理业务
// 限制条件 只能选择一个处置场所
if (chooseList && chooseList.length < 2) {
for (let index = 0; index < chooseList.length; index++) {
const element = chooseList[index];
if (current.id.indexOf(element.id) === -1 && element.label.indexOf(current.label) != -1) {
return "该处置场所负责人已经被选中了"
}
}
} else {
return "只能选择一个处置场所"
}
}
function open(dataList) {
treeData.value = handlerTreeData(dataList)
treeData.value = treeData.value.filter(item => {
return item.children[0].id
})
setTimeout(() => {
nextTick(() => {
unref(nextTreeRef).showTree = true
})
}, 0)
}
function handlerTreeData(dataList) {
return dataList
.map((item, index) => {
return {
"id": (index + 1),
"name": item.garOrderDisposalCompanyName,
"label": item.garOrderDisposalCompanyName,
"children": item.personnelInfo.map((childrenItem, childrenIndex) => {
return {
"id": (index + 1) + '-' + (childrenIndex + 1),
"tel": childrenItem.tel,
"companyName": item.garOrderDisposalCompanyName,
"companyId": item.garOrderDisposalCompanyId,
"name": childrenItem.personName,
"personName": childrenItem.personName,
"label": childrenItem.personName + "-" + childrenItem.tel,
"checked": childrenItem.checked,
"disabled": childrenItem.checked ? true : childrenItem.tel ? false : true
}
})
}
})
}
function cleanTreeData(treeData) {
treeData.map(item => {
item.checked = false
if (item.children && item.children.length) {
cleanTreeData(item.children)
}
})
}
function close() {
// 清除treeData的选中状态
cleanTreeData(unref(treeData))
searchText.value = '' // 清空搜索文本
}
// 添加清空搜索功能
function clearSearch() {
searchText.value = ''
}
defineExpose({
open, close, nextTreeRef
})
</script>
<style lang="scss">.line-block {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
.img {
width: 40rpx;
height: 40rpx;
border-radius: 10rpx;
margin: 0 20rpx;
}
}
.search-container {
padding: 15px 10px 10px;
background-color: #fff;
.search-box {
display: flex;
align-items: center;
background-color: #f5f5f5;
border-radius: 20px;
padding: 8px 15px;
.search-icon {
font-size: 16px;
margin-right: 8px;
color: #999;
}
.search-input {
flex: 1;
border: none;
background: transparent;
font-size: 14px;
outline: none;
padding: 5px 0;
&::placeholder {
color: #aaa;
}
}
.clear-icon {
font-size: 16px;
color: #999;
padding: 2px;
cursor: pointer;
}
}
}
</style>