-
Notifications
You must be signed in to change notification settings - Fork 352
/
Copy pathBaseSelectMultiple.vue
91 lines (87 loc) · 2.04 KB
/
BaseSelectMultiple.vue
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
<template>
<base-table thead-classes="thead-light" :data="data">
<template slot="columns">
<th></th>
<th v-for="(column, index) in columns" :key="index">{{ column }}</th>
</template>
<template slot-scope="{row, index}">
<th>
<base-checkbox
v-if="binds.length > 0"
v-model="binds[index].status"
v-on:input="handleSelection(row, $event)"
></base-checkbox>
</th>
<td v-for="(column, index) in columns" :key="index">{{ itemValue(row, column) }}</td>
</template>
</base-table>
</template>
<script>
export default {
name: "base-select-multiple",
props: {
columns: {
type: Array,
default: () => [],
description: "Table columns"
},
data: {
type: Array,
default: () => [],
description: "Table data"
},
selecteds: {
type: Array,
default: () => [],
description: "Table data"
}
},
data() {
return {
binds: [],
result: []
};
},
methods: {
itemValue(item, column) {
return item[column.toLowerCase()];
},
handleSelection(value, selection) {
if (selection) {
this.result.push(value);
} else {
if (this.result.length === 1) {
this.result = [];
} else {
const index = this.result.findIndex(e => e.id === value.id);
const result = [];
for (let i = 0; i < this.result.length; i++) {
if (index === i) continue;
result.push(this.result[i]);
}
this.result = result;
}
}
this.$emit("onSelectionChange", this.result);
}
},
updated() {
this.binds = [];
for (let element of this.data) {
let finded = false;
for (let a of this.selecteds) {
if (a.id === element.id) {
finded = true;
break;
}
}
if (finded) {
this.binds.push({ status: finded });
this.result.push(element);
} else {
this.binds.push({ status: finded });
}
}
}
};
</script>