Blame view

uni_modules/uview-plus/components/u-cascader/u-cascader.vue 8.76 KB
252962bb   刘淇   uni_modu
1
  <template>
ecbfbe06   刘淇   树巡检记录
2
3
4
5
6
7
8
9
10
11
12
13
14
  	<up-popup 
  		:show="popupShow" 
  		mode="bottom" 
  		:popup="false"
  		:mask="true" 
  		:closeable="closeable" 
  		:safe-area-inset-bottom="true"
  		:close-icon-color="closeIconColor" 
  		:z-index="uZIndex"
  		:maskCloseAble="maskCloseAble" 
  		@close="close"
  	>
  		<view class="up-p-t-30 up-p-l-20 up-m-b-10" v-if="headerDirection ==='column'">
252962bb   刘淇   uni_modu
15
  			<up-steps v-if="popupShow" dot direction="column" v-model:current="tabsIndex">
ecbfbe06   刘淇   树巡检记录
16
17
18
                  <view  v-for="(item, index) in genTabsList"  @click="toFatherIndex(index)">
                      <up-steps-item :title="item.name"/>
                  </view>
252962bb   刘淇   uni_modu
19
20
21
  			</up-steps>
  		</view>
  		<view class="up-p-t-20 up-m-b-10" v-else>
ecbfbe06   刘淇   树巡检记录
22
23
24
25
26
27
28
29
  			<up-tabs 
  				v-if="popupShow" 
  				:list="genTabsList"
  				:scrollable="true" 
  				v-model:current="tabsIndex" 
  				@change="tabsChange" 
  				ref="tabs"
  			></up-tabs>
252962bb   刘淇   uni_modu
30
31
32
33
34
  		</view>
  		<view class="area-box">
  			<view class="u-flex" :class="{ 'change':isChange }"
  				:style="{transform: optionsCols == 2 && isChange ? 'translateX(-33.3333333%)' : ''}">
  				<template v-for="(levelData, levelIndex) in levelList" :key="levelIndex">
ecbfbe06   刘淇   树巡检记录
35
36
37
38
39
40
  					<view 
  						v-if="optionsCols == 2 || levelIndex == tabsIndex" 
  						class="area-item"
  						:style="{ width: optionsCols == 2 ? '33.33333%' : '750rpx'}"
  					>
  						<view class="u-padding-10" :style="[levelPaneStyle, { height: '100%' }]">
252962bb   刘淇   uni_modu
41
42
  							<scroll-view :scroll-y="true" style="height: 100%">
  								<up-cell-group v-if="levelIndex === 0 || selectedValueIndexs[levelIndex - 1] !== undefined">
ecbfbe06   刘淇   树巡检记录
43
44
45
46
47
48
49
50
  									<up-cell 
  										v-for="(item,index) in levelData"
  										:title="item[labelKey]" 
  										:arrow="false"
  										:index="index" 
  										:key="index"
  										@click="levelChange(levelIndex, index)"
  									>
252962bb   刘淇   uni_modu
51
  										<template v-slot:right-icon>
ecbfbe06   刘淇   树巡检记录
52
53
54
55
56
  											<up-icon 
  												v-if="selectedValueIndexs[levelIndex] === index"
  												size="17" 
  												name="checkbox-mark"
  											></up-icon>
252962bb   刘淇   uni_modu
57
58
59
60
61
62
63
64
65
  										</template>
  									</up-cell>
  								</up-cell-group>
  							</scroll-view>
  						</view>
  					</view>
  				</template>
  			</view>
  		</view>
ecbfbe06   刘淇   树巡检记录
66
  		<!-- 按钮区域 -->
252962bb   刘淇   uni_modu
67
68
69
70
71
72
73
74
75
76
77
78
  		<view class="u-cascader-action up-flex up-flex-between">
  			<view class="u-padding-20 up-flex-fill">
  				<up-button @click="handleCancel" type="default">{{ t("up.common.cancel") }}</up-button>
  			</view>
  			<view class="u-padding-20 up-flex-fill">
  				<up-button @click="handleConfirm" type="primary">{{ t("up.common.confirm") }}</up-button>
  			</view>
  		</view>
  	</up-popup>
  </template>
  
  <script>
ecbfbe06   刘淇   树巡检记录
79
80
81
82
83
84
85
86
87
88
89
90
  import { t } from '../../libs/i18n'
  export default {
  	name: 'up-cascader',
  	props: {
  		show: {
  			type: Boolean,
  			default: false
  		},
  		data: {
  			type: Array,
  			default() {
  				return [];
252962bb   刘淇   uni_modu
91
92
  			}
  		},
ecbfbe06   刘淇   树巡检记录
93
94
95
96
  		modelValue: {
  			type: Array,
  			default() {
  				return [];
252962bb   刘淇   uni_modu
97
98
  			}
  		},
ecbfbe06   刘淇   树巡检记录
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
  		valueKey: {
  			type: String,
  			default: 'value'
  		},
  		labelKey: {
  			type: String,
  			default: 'label'
  		},
  		childrenKey: {
  			type: String,
  			default: 'children'
  		},
  		maskCloseAble: {
  			type: Boolean,
  			default: true
  		},
  		zIndex: {
  			type: [String, Number],
  			default: 0
  		},
  		autoClose: {
  			type: Boolean,
  			default: false
  		},
  		headerDirection: {
  			type: String,
  			default: 'row'
  		},
  		optionsCols: {
  			type: [Number],
  			default: 2
  		},
  		closeable: {
  			type: Boolean,
  			default: true
  		}
  	},
  	data() {
  		return {
  			levelList: [],
  			selectedValueIndexs: [],
  			tabsIndex: 0,
  			popupShow: false,
  			confirmValues: []
  		}
  	},
  	watch: {
  		data: {
  			handler() {
  				this.initLevelList();
  				this.setDefaultValue();
252962bb   刘淇   uni_modu
150
  			},
ecbfbe06   刘淇   树巡检记录
151
152
153
154
155
156
157
158
159
160
  			immediate: true
  		},
  		show: {
  			handler(val) {
  				this.popupShow = val;
  				if (val) {
  					this.tabsIndex = 0;
  					this.initLevelList();
  					this.setDefaultValue();
  				}
252962bb   刘淇   uni_modu
161
  			},
ecbfbe06   刘淇   树巡检记录
162
  			immediate: true
252962bb   刘淇   uni_modu
163
  		},
ecbfbe06   刘淇   树巡检记录
164
165
166
  		modelValue: {
  			handler() {
  				this.setDefaultValue();
252962bb   刘淇   uni_modu
167
  			},
ecbfbe06   刘淇   树巡检记录
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  			immediate: true
  		}
  	},
  	computed: {
  		isChange() {
  			return this.tabsIndex > 1;
  		},
  		genTabsList() {
  			let tabsList = [{ name: "请选择" }];
  			for (let i = 0; i < this.selectedValueIndexs.length; i++) {
  				if (this.selectedValueIndexs[i] !== undefined && this.levelList[i]) {
  					const selectedItem = this.levelList[i][this.selectedValueIndexs[i]];
  					if (selectedItem) {
  						tabsList[i] = { name: selectedItem[this.labelKey] };
  						if (i === this.selectedValueIndexs.length - 1 &&
  							selectedItem[this.childrenKey] &&
  							selectedItem[this.childrenKey].length > 0) {
  							tabsList.push({ name: "请选择" });
252962bb   刘淇   uni_modu
186
187
188
  						}
  					}
  				}
252962bb   刘淇   uni_modu
189
  			}
ecbfbe06   刘淇   树巡检记录
190
  			return tabsList;
252962bb   刘淇   uni_modu
191
  		},
ecbfbe06   刘淇   树巡检记录
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  		uZIndex() {
  			return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  		},
  		closeIconColor() {
  			return 'var(--up-main-color, #303133)';
  		},
  		levelPaneStyle() {
  			return {
  				backgroundColor: 'var(--up-bg-color, #f7f7f7)'
  			};
  		}
  	},
  	emits: ['update:modelValue', 'update:show', 'change', 'confirm', 'cancel'],
  	methods: {
  		t,
  		// 获取选中值数组
  		getSelectedValues() {
  			const result = [];
  			for (let i = 0; i < this.selectedValueIndexs.length; i++) {
  				const selectedIndex = this.selectedValueIndexs[i];
  				if (selectedIndex === undefined) continue;
  				const list = this.levelList[i];
  				if (!list || !list[selectedIndex]) continue;
  				result.push(list[selectedIndex][this.valueKey]);
  			}
  			return result;
  		},
  		// 判断当前选中项是否为最后一级(叶子节点)
  		isCurrentLeafNode() {
  			const len = this.selectedValueIndexs.length;
  			if (len === 0) return false;
  			const lastLevel = len - 1;
  			const lastIndex = this.selectedValueIndexs[lastLevel];
  			const lastItem = this.levelList[lastLevel]?.[lastIndex];
  			if (!lastItem) return false;
  			// 无子级 = 最后一级
  			return !lastItem[this.childrenKey] || lastItem[this.childrenKey].length === 0;
  		},
  		initLevelList() {
  			if (this.data && this.data.length > 0) {
  				this.levelList = [this.data];
252962bb   刘淇   uni_modu
233
  				this.selectedValueIndexs = [];
ecbfbe06   刘淇   树巡检记录
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  			} else {
  				this.levelList = [];
  				this.selectedValueIndexs = [];
  			}
  		},
  		setDefaultValue() {
  			if (!this.data || this.data.length === 0) {
  				this.confirmValues = [];
  				return;
  			}
  			if (!this.modelValue || this.modelValue.length === 0) {
  				this.confirmValues = [];
  				return;
  			}
  			this.selectedValueIndexs = [];
  			this.levelList = [];
  			let currentLevelData = this.data;
  
  			for (let i = 0; i < this.modelValue.length; i++) {
  				const value = this.modelValue[i];
  				const index = currentLevelData.findIndex(item => item[this.valueKey] === value);
  				this.levelList[i] = currentLevelData;
  
  				if (index !== -1) {
  					this.selectedValueIndexs.push(index);
  					if (currentLevelData[index][this.childrenKey]) {
  						currentLevelData = currentLevelData[index][this.childrenKey];
252962bb   刘淇   uni_modu
261
  					} else {
252962bb   刘淇   uni_modu
262
263
  						break;
  					}
252962bb   刘淇   uni_modu
264
  				} else {
ecbfbe06   刘淇   树巡检记录
265
  					break;
252962bb   刘淇   uni_modu
266
  				}
ecbfbe06   刘淇   树巡检记录
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
  			}
  			this.confirmValues = this.getSelectedValues();
  		},
  
  		close() {
  			this.popupShow = false;
  			this.levelList = [];
  			this.selectedValueIndexs = [];
  			this.tabsIndex = 0;
  			this.confirmValues = [];
  			this.$emit('update:show', false);
  			this.$emit('cancel');
  		},
  
  		tabsChange(item) {},
  		toFatherIndex(index) {
  			this.tabsIndex = index;
  		},
  		levelChange(levelIndex, index) {
  			this.$set(this.selectedValueIndexs, levelIndex, index);
  			this.selectedValueIndexs.splice(levelIndex + 1);
  			this.tabsIndex = Math.min(this.tabsIndex, levelIndex);
  			this.levelList.splice(levelIndex + 1);
  
  			const currentItem = this.levelList[levelIndex][index];
  			if (currentItem && currentItem[this.childrenKey] && currentItem[this.childrenKey].length > 0) {
  				if (this.levelList.length <= levelIndex + 1) {
  					this.levelList.push(currentItem[this.childrenKey]);
  				} else {
  					this.$set(this.levelList, levelIndex + 1, currentItem[this.childrenKey]);
252962bb   刘淇   uni_modu
297
  				}
ecbfbe06   刘淇   树巡检记录
298
299
300
301
302
303
304
  				this.tabsIndex = levelIndex + 1;
  				// 有子级,不自动确认
  				if (this.autoClose) return;
  			} else {
  				// 已选到最后一级,自动确认
  				if (this.autoClose) {
  					this.handleConfirm();
252962bb   刘淇   uni_modu
305
  				}
ecbfbe06   刘淇   树巡检记录
306
307
308
309
310
311
312
  			}
  		},
  		emitChange(closePopup = true) {
  			const result = this.getSelectedValues();
  			this.confirmValues = [...result];
  			this.$emit('change', this.confirmValues);
  			if (closePopup) {
252962bb   刘淇   uni_modu
313
314
  				this.close();
  			}
ecbfbe06   刘淇   树巡检记录
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
  		},
  		handleCancel() {
  			this.close();
  		},
  		handleConfirm() {
  			// 校验:必须选择到最后一级
  			if (!this.isCurrentLeafNode()) {
  				uni.showToast({ title: '请选择到最后一级', icon: 'none' });
  				return;
  			}
  			const values = this.getSelectedValues();
  			this.confirmValues = [...values];
  			// 向外输出 选中值数组
  			this.$emit('update:modelValue', values);
  			this.$emit('confirm', values);
  			this.close();
252962bb   刘淇   uni_modu
331
332
  		}
  	}
ecbfbe06   刘淇   树巡检记录
333
  }
252962bb   刘淇   uni_modu
334
  </script>
ecbfbe06   刘淇   树巡检记录
335
  
252962bb   刘淇   uni_modu
336
337
338
339
340
341
342
343
344
345
346
  <style lang="scss">
  	.area-box {
  		width: 100%;
  		overflow: hidden;
  		height: 800rpx;
  
  		>view {
  			width: 150%;
  			transition: transform 0.3s ease-in-out 0s;
  			transform: translateX(0);
  
ecbfbe06   刘淇   树巡检记录
347
348
  		&.change {
  				transform: translateX(-33.3333333%);
252962bb   刘淇   uni_modu
349
350
351
352
  			}
  		}
  
  		.area-item {
252962bb   刘淇   uni_modu
353
354
355
  			height: 800rpx;
  		}
  	}
ecbfbe06   刘淇   树巡检记录
356
  
252962bb   刘淇   uni_modu
357
  	.u-cascader-action {
ecbfbe06   刘淇   树巡检记录
358
  		border-top: 1px solid var(--up-border-color, #eee);
252962bb   刘淇   uni_modu
359
360
  	}
  </style>