button-checkbox.vue
3.23 KB
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
<template>
<div class="btn-group" data-toggle="buttons">
<label :class="['btn', btnVariant, btnSize, checked(index) ? 'active' : '']" v-for="(item,index) in list">
<input type="checkbox"
:value="item.value"
autocomplete="off"
v-model="item.checked"
:disabled="item.disabled"
v-html="item.text"
/>
</label>
</div>
</template>
<script>
export default {
replace: true,
computed: {
btnVariant() {
return !this.variant || this.variant === `default` ? `btn-secondary` : `btn-${this.variant}`;
},
btnSize() {
return !this.size || this.size === `default` ? `` : `btn-${this.size}`;
}
},
props: {
list: {
type: Array,
default: [],
required: true
},
model: {
type: Array,
default: []
},
size: {
type: String,
default: 'md'
},
variant: {
type: String,
default: 'default'
},
returnObject: {
type: Boolean,
default: false
}
},
methods: {
checked(index) {
if (!this.list) {
return false;
}
let result = false;
if (this.returnObject) {
for (let i = 0; i < this.model.length; i++) {
if (this.model[i].value === this.list[index].value) {
result = true;
}
}
} else {
result = this.model.indexOf(this.list[index].value) !== -1;
}
return result;
}
},
watch: {
list: {
// handler(val) {
// this.model = []
// this.list.forEach((item) = > {
// if (item.checked
// )
// {
// if (this.returnObject) {
// this.model.push(item)
// } else {
// this.model.push(item.value)
// }
// }
// })
// ;
// console.log(changed);
// // Emit an event
// this.$emit('changed', this.model)
// },
// deep: true,
}
},
mounted() {
// handle initial selection
this.list.forEach(item => {
if (this.returnObject) {
this.model.forEach(modelItem => {
if (modelItem.value === item.value
) {
item.checked = true;
}
});
} else if (this.model.indexOf(item.value) !== -1) {
item.checked = true;
}
});
}
};
</script>