• 分类选择,最多五级


    效果图,这种竖向的分类选择,每一列可以用不同的背景颜色
    在这里插入图片描述

    组件代码

    <template>
    	<view class="toolTypeBox" :style="`max-height:${maxHeight}`">
    		<block v-for="(item,index) in datalist">
    			<block v-if="item.list.length>0">
    				<view>
    					<scroll-view scroll-y class="toolType-each" :style="`background-color: ${item.bgcolor};max-height:${maxHeight}`">
    						<view>
    							<view v-for="(ite,inde) in item.list" :key="ite[idValue]" class="toolType-each-box" :class="[pip(index,inde)&&ite[idValue]==nowObject[idValue]?'active':'',item.current===inde?'moactive':'']" @tap="changeTab(index,inde)">{{ite[nameValue]}}</view>
    						</view>
    					</scroll-view>
    				</view>
    			</block>
    		</block>
    	</view>
    </template>
    
    <script>
    	export default{
    		name:'tool-type',
    		data(){
    			return{
    				datalist:[
    					{
    						list:[],
    						current:0,
    						bgcolor:'#ECECEC',
    					},
    					{
    						list:[],
    						current:0,
    						bgcolor:'#F8F8F8',
    					},
    					{
    						list:[],
    						current:0,
    						bgcolor:'#FFF',
    					},
    					{
    						list:[],
    						current:0,
    						bgcolor:'#FFF',
    					},
    					{
    						list:[],
    						current:0,
    						bgcolor:'#FFF',
    					},
    				],
    				nowCurrent:[null,null],//当前选中
    				nowObject:{},//当前选中个的数据
    			}
    		},
    		props:{
    			dataList:{
    				type:Array,
    				default:() =>{
    					return []
    				}
    			},
    			//默认选中id
    			defaultId:{
    				type:String|Number,
    				default:''
    			},
    			maxHeight:{
    				type:String,
    				default:'800rpx'
    			},
    			parentIdValue:{
    				type:String,
    				default:'parentId'
    			},
    			idValue:{
    				type:String,
    				default:'id'
    			},
    			nameValue:{
    				type:String,
    				default:'name'
    			},
    			childrenValue:{
    				type:String,
    				default:'children'
    			},
    		},
    		methods:{
    			//获取默认id在数组中的位置
    		  	getParent(data2, nodeId2) {
    			      var arrRes = [];
    			      if (data2.length == 0) {
    			        if (!!nodeId2) {
    			          arrRes.unshift(data2);
    			        }
    			        return arrRes;
    			      }
    			      let rev = (data, nodeId) => {
    			        for (var i = 0, length = data.length; i < length; i++) {
    			          let node = data[i];
    			          if (node[this.idValue] == nodeId) {
    			            arrRes.unshift(i);
    			            rev(data2, node[this.parentIdValue]);
    			            break;
    			          } else {
    			            if (!!node[this.childrenValue]) {
    			              rev(node[this.childrenValue], nodeId);
    			            }
    			          }
    			        }
    			        return arrRes;
    			      };
    			      arrRes = rev(data2, nodeId2);
    			      return arrRes;
    		    },
    			mate(list){
    				let aaa = this.getParent(list,this.defaultId)
    				//没有该id时,清空选中的位置
    				if(!aaa || aaa.length == 0){
    					this.nowCurrent = [null,null];
    					this.changeTab(0,0,'noId')
    					return
    				}
    				let bbb = [];
    				aaa.map((r,v) =>{
    					this.datalist[v].current = r;
    					if(v === 0){
    						let ll = this.datalist[0].list[aaa[0]][this.childrenValue];
    						bbb = ll;
    						this.datalist[v+1].list = ll;
    					}else{
    						let ll = bbb[r][this.childrenValue];
    						bbb = ll;
    						this.datalist[v+1].list = ll;
    					}
    					//将默认选中ID的子集展示出来
    					if(v === aaa.length-1){
    						this.changeTab(aaa.length-1,aaa[aaa.length-1],'zidong')
    					}
    				})
    			},
    			//获取已选中的Id
    			getSelectId(){
    				return this.nowObject;
    			},
    			pip(i,v){
    				if(this.nowCurrent[0] === i && this.nowCurrent[1] === v){
    					return true
    				}else{
    					return false
    				}
    			},
    			//选择
    			changeTab(i,v,s){
    				console.log('选择',i,v)
    				switch(i){
    					case 0:
    						//第一层
    						if(this.datalist[i].list){
    							
    							// if(v === this.datalist[i].current)return;
    							this.datalist[i].current = v;
    							this.datalist[1].current = 0;
    							this.datalist[2].current = 0;
    							this.datalist[3].current = 0;
    							this.datalist[4].current = 0;
    							this.datalist[1].list = [];
    							this.datalist[2].list = [];
    							this.datalist[3].list = [];
    							this.datalist[4].list = [];
    							let aa = this.datalist[i].list[v][this.childrenValue];
    							if(aa?.length>0){
    								this.datalist[1].list = aa;
    								let bb = aa[0][this.childrenValue];
    								if(bb?.length>0){
    									this.datalist[2].list = bb;
    									let cc = bb[0][this.childrenValue];
    									if(cc?.length>0){
    										this.datalist[3].list = cc;
    										let dd = cc[0][this.childrenValue];
    										if(dd?.length>0){
    											this.datalist[4].list = dd;
    										}
    									}
    								}
    								console.log('叔叔叔苏火速火速',this.datalist)
    							}else{
    								if(s !== 'noId'){
    									this.nowCurrent[0] = i;
    									this.nowCurrent[1] = v;
    									if(s !== 'zidong'){
    										this.$emit('selectType',this.datalist[i].list[v])
    										this.nowObject = this.datalist[i].list[v];
    									}
    								}
    							}
    						}else{
    							this.nowCurrent[0] = i;
    							this.nowCurrent[1] = null;
    						}
    						break
    					case 1:
    						//第二层
    						if(this.datalist[i].list){
    							
    							// if(v === this.datalist[i].current)return;
    							this.datalist[i].current = v;
    							this.datalist[2].current = 0;
    							this.datalist[3].current = 0;
    							this.datalist[4].current = 0;
    							this.datalist[2].list = [];
    							this.datalist[3].list = [];
    							this.datalist[4].list = [];
    							let aa = this.datalist[i].list[v][this.childrenValue];
    							if(aa?.length>0){
    								this.datalist[2].list = aa;
    								let bb = aa[0][this.childrenValue];
    								if(bb?.length>0){
    									this.datalist[3].list = bb;
    									let cc = bb[0][this.childrenValue];
    									if(cc?.length>0){
    										this.datalist[4].list = cc;
    									}
    								}
    							}else{
    								if(s !== 'noId'){
    									this.nowCurrent[0] = i;
    									this.nowCurrent[1] = v;
    									if(s !== 'zidong'){
    										this.$emit('selectType',this.datalist[i].list[v])
    										this.nowObject = this.datalist[i].list[v];
    									}
    								}
    							}
    						}else{
    							this.nowCurrent[0] = i;
    							this.nowCurrent[1] = null;
    						}
    						break
    					case 2:
    						//第三层
    						if(this.datalist[i].list){
    							
    							// if(v === this.datalist[i].current)return;
    							this.datalist[i].current = v;
    							this.datalist[3].current = 0;
    							this.datalist[4].current = 0;
    							this.datalist[3].list = [];
    							this.datalist[4].list = [];
    							let aa = this.datalist[i].list[v][this.childrenValue];
    							if(aa?.length>0){
    								this.datalist[3].list = aa;
    								let bb = aa[0][this.childrenValue];
    								if(bb?.length>0){
    									this.datalist[4].list = bb;
    								}
    							}else{
    								if(s !== 'noId'){
    									this.nowCurrent[0] = i;
    									this.nowCurrent[1] = v;
    									if(s !== 'zidong'){
    										this.$emit('selectType',this.datalist[i].list[v])
    										this.nowObject = this.datalist[i].list[v];
    									}
    								}
    							}
    						}else{
    							this.nowCurrent[0] = i;
    							this.nowCurrent[1] = null;
    						}
    						break
    					case 3:
    						//第四层
    						if(this.datalist[i].list){
    
    							// if(v === this.datalist[i].current)return;
    							this.datalist[i].current = v;
    							this.datalist[4].current = 0;
    							this.datalist[4].list = [];
    							let aa = this.datalist[i].list[v][this.childrenValue];
    							if(aa?.length>0){
    								this.datalist[4].list = aa;
    							}else{
    								if(s !== 'noId'){
    									this.nowCurrent[0] = i;
    									this.nowCurrent[1] = v;
    									if(s !== 'zidong'){
    										this.$emit('selectType',this.datalist[i].list[v])
    										this.nowObject = this.datalist[i].list[v];
    									}
    								}
    							}
    						}else{
    							this.nowCurrent[0] = i;
    							this.nowCurrent[1] = null;
    						}
    						break
    					case 4:
    						//第五层
    						if(this.datalist[i].list){
    							
    							// if(v === this.datalist[i].current)return;
    							this.datalist[i].current = v;
    							if(s !== 'noId'){
    								this.nowCurrent[0] = i;
    								this.nowCurrent[1] = v;
    								if(s !== 'zidong'){
    									this.$emit('selectType',this.datalist[i].list[v])
    									this.nowObject = this.datalist[i].list[v];
    								}
    							}
    						}else{
    							this.nowCurrent[0] = i;
    							this.nowCurrent[1] = null;
    						}
    						break
    				}
    			}
    		},
    		watch:{
    			dataList:{
    				immediate:true,
    				handler(newlist){
    					// console.log('来了',newlist)
    					if(this.defaultId&&newlist.length>0){
    						this.datalist[0].list = newlist;
    						this.mate(newlist) //匹配选中默认id
    					}else{
    						if(newlist?.length>0){
    							this.datalist[0].list = newlist;
    							let aa = newlist[0][this.childrenValue];
    							if(aa?.length>0){
    								this.datalist[1].list = aa;
    								let bb = aa[0][this.childrenValue];
    								if(bb?.length>0){
    									this.datalist[2].list = bb;
    									let cc = bb[0][this.childrenValue];
    									if(cc?.length>0){
    										this.datalist[3].list = cc;
    										let dd = cc[0][this.childrenValue];
    										if(dd?.length>0){
    											this.datalist[4].list = dd;
    										}
    									}
    								}
    							}
    						}else{
    							this.datalist[0].list = [];
    							this.datalist[0].current = 0;
    							this.datalist[1].list = [];
    							this.datalist[1].current = 0;
    							this.datalist[2].list = [];
    							this.datalist[2].current = 0;
    							this.datalist[3].list = [];
    							this.datalist[3].current = 0;
    							this.datalist[4].list = [];
    							this.datalist[4].current = 0;
    						}
    					}
    				}
    			},
    			defaultId:{
    				immediate:true,
    				handler(newid){
    					// console.log('默认',newid)
    					if(this.dataList.length>0){
    						this.mate(this.dataList) //匹配选中默认id
    					}
    				}
    			}
    		},
    	}
    </script>
    
    <style lang="scss">
    	.toolTypeBox{
    		// width:100%;
    		// height:100%;
    		display: flex;
    		background:#fff;
    	}
    	.toolType-each{
    		width:220rpx;
    	}
    	.toolType-each-box{
    		width:100%;
    		padding:26rpx 20rpx;
    		box-sizing: border-box;
    		font-size: 30rpx;
    		color: #666;
    		text-align: center;
    		word-break:break-all;
    	}
    	.moactive{
    		background-color:#fff;
    	}
    	.active{
    		background-color:#FFC526;
    		color:#fff;
    	}
    </style>
    
    • 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
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 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
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 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
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
  • 相关阅读:
    日撸 Java 三百行day54-55
    从零开始学习调用百度地图网页API:三、鼠标点击绘图功能
    C | 在ubuntu22下开发的一些配置
    Mosquitto 安装指南
    sed使用技巧-在replacement块获取pattern匹配的值
    Docker SpringBoot项目连接本地数据库
    Vue(3.3.4)+three.js(0.161.0)实现3D可视化地图
    Kubeadm安装k8s高可用集群实战
    leetcode 1884-鸡蛋掉落-两枚鸡蛋
    Locust负载测试工具实操
  • 原文地址:https://blog.csdn.net/m0_46978096/article/details/133886073