本文實例為大家分享了小程序實現自定義多層級單選和多選的具體代碼,供大家參考,具體內容如下
效果:
ps:這兒是用自定義的下拉框,我把它封裝成了一個組件
wxml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
< view class = "select-box" > < view class = "select-title" > < view class = "cell-border" > < van-field value = "{{ layout }}" data-key = "layout" placeholder = "請輸入" required icon = "arrow" label = "戶型" bind:tap = "onChange" /> </ view > </ view > < view class = "select-list" wx:if = "{{show}}" > < view class = "option" wx:for = "{{layouts}}" wx:key = "index" > < view class = "{{curItem.checked ? 'option-item-active' : 'option-item'}}" wx:for = "{{item.column}}" wx:key = "index" wx:for-item = "curItem" data-key = "{{curItem.key}}" data-colkey = "{{item.colKey}}" data-name = "{{curItem.name}}" bind:tap = "getOptionItem" > {{curItem.name}} </ view > </ view > </ view > </ view > |
wxss
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
|
.select-box{ width : 100% ; padding : 20 rpx; box-sizing: border-box; } .cell-border { border-radius: 6 rpx; border : 1px solid #999 ; margin-bottom : 10 rpx; } .select-list{ display : flex; flex- direction : row; justify- content : space-around; width : 100% ; height : 360 rpx; padding : 20 rpx; box-sizing: border-box; background-color : #fff ; border : 1px solid #eee ; } .select-list .option{ display : flex; flex- direction : column; font-size : 24 rpx; } .option-item{ width : 80 rpx; height : 100 rpx; background-color : #eee ; text-align : center ; margin-top : 5px ; padding : 2px ; } .option-item-active{ width : 80 rpx; height : 100 rpx; background-color : #FF6600 ; text-align : center ; margin-top : 5px ; padding : 2px ; color : #fff ; } json { "component" : true, "usingComponents" : { "van-field" : "../../vant/field/index" , } } |
js
ps:data是組件本身的數據,layouts是數據源
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
|
Component({ properties:{ }, data:{ show: false , curKey:-1, colKey:-1, layouts:[ { colKey:0, column:[ {name: "1室" ,key:0,}, {name: "2室" ,key:1,}, {name: "3室" ,key:2,}, {name: "4室" ,key:3,}, {name: "5室" ,key:4,}, {name: "6室" ,key:5,} ] }, { colKey:1, column:[ {name: "1廳" ,key:0,}, {name: "2廳" ,key:1,}, {name: "3廳" ,key:2,}, {name: "4廳" ,key:3,}, {name: "5廳" ,key:4,}, {name: "6廳" ,key:5,} ] }, { colKey:2, column:[ {name: "1廚" ,key:0,}, {name: "2廚" ,key:1,}, {name: "3廚" ,key:2,}, {name: "4廚" ,key:3,}, {name: "5廚" ,key:4,}, {name: "6廚" ,key:5,}] }, { colKey:3, column:[ {name: "1衛" ,key:0,}, {name: "2衛" ,key:1,}, {name: "3衛" ,key:2,}, {name: "4衛" ,key:3,}, {name: "5衛" ,key:4,}, {name: "6衛" ,key:5,} ] }, { colKey:4, column:[ {name: "1陽臺" ,key:0,}, {name: "2陽臺" ,key:1,}, {name: "3陽臺" ,key:2,}, {name: "4陽臺" ,key:3,}, {name: "5陽臺" ,key:4,}, {name: "6陽臺" ,key:5,} ] } ] }, methods:{ onChange(){ const {show} = this .data; this .setData({ show:!show }) }, getOptionItem(event){ console.log( "event" ,event) const key = event.currentTarget.dataset.key; const cK = event.currentTarget.dataset.colkey; const {curKey,colKey,layouts} = this .data; this .setData({ curKey:key, colKey:cK }) //用checked字段判斷,允許每列之間單選,多行之間多選 layouts[cK].column.map(cur => { return cur.checked = false ; }) layouts[cK].column[key].checked = true ; this .setData({layouts}) } } }) |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_44268473/article/details/103612075