本文實例為大家分享了vue使用vant中的checkbox實現全選功能的具體代碼,供大家參考,具體內容如下
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
|
< template > < div class = "visiblePeople" > < topbar /> < ul class = "list clear_float" > < li v-for = "(item, index) in people" :key = "index" > < van-checkbox v-model = "item.flag" class = "listli" ></ van-checkbox > < div class = "right" > < p >{{ item.name }}</ p > < p >{{ item.id }}</ p > </ div > </ li > </ ul > < div class = "bottom" > < div class = "left" > < van-checkbox v-model = "allcheck" class = "all" >全選</ van-checkbox > </ div > < button @ click = "jump" >確定</ button > </ div > </ div > </ template > < script > export default { data() { return { people: [ { id: "002", name: "陳陽", flag: true }, { id: "003", name: "王苗苗", flag: true, }, { id: "004", name: "張梁俊", flag: true, }, { id: "005", name: "劉路", flag: true, }, ], }; }, methods: { //點擊確定后跳轉回新增合同頁面 jump() { this.$router.push("/addContract"); }, //單選按鈕切換 // change(index) { // this.people[index].flag = !this.people[index].flag; // console.log(this.people[index].flag); // }, }, computed:{ allcheck:{ get(){ //取值 //every方法,數組中每一項都滿足一個條件返回true return this.people.every(item=>item.flag) }, set(newValue){ //設置值 console.log('觸發set方法') this.people.map(item=>item.flag=newValue) } }, filterAll(){ return this.people.filter(item=>item.flag).length }, count(){ let checkedList=this.people.filter(item=>item.flag) return checkedList.length.reduce((total,item)=>{ return total+item.num },0) } } }; </ script > < style lang = "less" scoped> .list { background: #f8f9fb; height: 574px; li { height: 56px; margin: 10px 0 10px 0; float: left; img { width: 19px; height: 19px; float: left; margin: 13px; &.on { display: block; } &.off { display: none; } } .listli { float: left; margin: 19px 13px 0 13px; } .right { float: left; background: #ffffff; width: 328px; height: 56px; padding: 0px 0 0 13px; box-sizing: border-box; p:nth-of-type(1) { font-size: 15px; font-family: PingFang SC; font-weight: 400; color: #000000; line-height: 29px; } p:nth-of-type(2) { font-size: 13px; font-family: PingFang SC; font-weight: 400; color: #666666; line-height: 14px; } } } } .bottom { height: 50px; position: fixed; bottom: 0; .left { width: 237px; background: #ffffff; height: 100%; float: left; img { width: 18px; float: left; margin: 18px 13px 0 18px; &.on { display: block; } &.off { display: none; } } .all { margin: 17px 0 0 14px; } p { float: left; font-size: 13px; font-family: PingFang SC; font-weight: 400; color: #333333; margin-top: 18px; } } button { float: left; width: 138px; height: 50px; line-height: 50px; background: #336afa; color: #ffffff; } } </ style > |
本次遇到的問題是自己一開始沒發現在people數組里面,定義的每一項flag的值設置的類型為字符串型即flag="true",導致一開始進入頁面全部無論值為true還是false,復選框都是選中效果,修改之后便沒有了此問題。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_47813845/article/details/109636953