我是靠谱客的博主 糟糕星星,这篇文章主要介绍vue封装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
<s-checkbox-group :sCheckedGroup="sCheckedGroup" v-model="checked" name="a"></s-checkbox-group> <s-checkbox-group :sCheckedGroup="sCheckedGroup1" v-model="checked1" @change="handlclick" name="a1"></s-checkbox-group> data(){ return { checked:[1,3], checked1:[1], sCheckedGroup:[ {label:'选项1',value:1}, {label:'选项2',value:2}, {label:'选项选项3',value:3}, ], sCheckedGroup1:[ {label:'选项1',value:1,disabled:"true"}, {label:'选项2',value:2,disabled:"true"}, {label:'选项选项3',value:3}, ] } }, methods:{ handlclick(val){ console.log(val) } }

checked值为数组,传value值得数组集合进去即可

组件

复制代码
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
<template> <div class="s-checkbox-bigBox"> <span class="s-checkbox-box" v-for="item,index in sCheckedGroup" :class="{ 'is-disabled': item.disabled, 'is-checked': model[index] }" @click.stop="chooseSCheckbox(item,index)" > <span class="s-checkbox__inner"></span> <input type="checkbox" v-model="model[index]" :value="model" id="s-checkbox" class="s-checkbox" > <label class="s-label" style="">{{item.label}}</label> </span> <!-- :value="curValue(item.value)" --> </div> </template> <script> export default { name:"sCheckboxGroup", data(){ return { curValue(val){ // if(this.value&&this.value!=""){ // this.model=this.value; // return this.value // }else{ // return val // } }, model:[], } }, props:{ sCheckedGroup:{ type:Array, default:[] }, disabled:Boolean, value:{ type:Array, default:[] }, }, computed:{ isDisabled(){ return this.disabled }, }, watch: { }, methods:{ chooseSCheckbox(val,index){ if(val.disabled){ return false; } this.model[index]=!this.model[index]; this.model.push(false); // 返回value值 let arr= this.model.slice(0,this.sCheckedGroup.length); let newArr=[]; for(let i in this.sCheckedGroup){ if(arr[i]){ newArr.push(this.sCheckedGroup[i].value) } } this.$emit('change',newArr); this.$emit('input', newArr); } }, created(){ console.log(this.value) for(let i in this.sCheckedGroup){ this.model[i]=false; for(let n in this.value){ if(this.value[n]==this.sCheckedGroup[i].value){ this.model[i]=true; } } } } } </script> <style scoped> .s-checkbox__inner{ border: 1px solid #dcdfe6; width: 14px; height: 14px; background-color: #fff; position: relative; cursor: pointer; display: inline-block; box-sizing: border-box; top: 2px; } .s-checkbox__inner:after { box-sizing: content-box; content: ""; border: 1px solid #fff; border-left: 0; border-top: 0; height: 7px; left: 4px; position: absolute; top: 1px; transform: rotate(45deg) scaleY(1); width: 3px; transition: transform .15s ease-in .05s; transform-origin: center; } .s-checkbox-box{ cursor: pointer; display: inline-block } .s-checkbox-box.is-checked .s-checkbox__inner { border-color: #008DFF; background: #008DFF; } .s-checkbox-box.is-checked .s-label{ color:#008DFF } .s-label{ cursor: pointer; margin-left:4px; margin-right:30px; color:#606266 } .s-checkbox{ outline: none; position: absolute; z-index: -1; top: 4px; left:1px; right: 0; bottom: 0; margin: 0; } .s-checkbox-box{ position: relative; cursor: pointer; } .s-checkbox-bigBox{ /* display: inline-block; */ margin:10px 0 } .s-checkbox-box.is-disabled{ cursor: not-allowed; } .s-checkbox-box.is-disabled .s-checkbox__inner{ background: #EEEEEE; cursor: not-allowed; } .s-checkbox-box.is-disabled label{ color: #868686; cursor: not-allowed; } .s-checkbox-box.is-disabled .s-checkbox__inner:after{ background: #EEEEEE; border-color:#EEEEEE; cursor: not-allowed; } .s-checkbox-box.is-disabled.is-checked label{ color:#868686 } .s-checkbox-box.is-disabled.is-checked .s-checkbox__inner{ border-color: #DCDFE6; } .s-checkbox-box.is-disabled.is-checked .s-checkbox__inner:after{ border-color:#A0A0A0; } </style>

Props

参数说明

属性类型参数说明参数默认值
sCheckedGroupArraycheckbox的数组选项[][]
disabledBoolean禁用true,falsefalse

Events

事件说明

属性类型参数说明参数默认值
changeEventsradio值tvale

最后

以上就是糟糕星星最近收集整理的关于vue封装checkbox 组件的全部内容,更多相关vue封装checkbox内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部