forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrow-selection.js
More file actions
80 lines (76 loc) · 2 KB
/
row-selection.js
File metadata and controls
80 lines (76 loc) · 2 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
function getRowSelection (rows, selection, multiple) {
if (!selection) {
return []
}
return multiple ? rows.map(() => false) : [-1]
}
export default {
data () {
return {
rowSelection: []
}
},
created () {
this.rowSelection = getRowSelection(this.rows, this.config.selection, this.multipleSelection)
},
watch: {
'config.selection' (value) {
this.rowSelection = getRowSelection(this.rows, value, value === 'multiple')
},
rows (r) {
this.rowSelection = getRowSelection(r, this.config.selection, this.multipleSelection)
},
rowSelection () {
this.$nextTick(() => {
this.$emit('selection', this.rowsSelected, this.selectedRows)
if (this.rowsSelected) {
this.toolbar = 'selection'
return
}
if (this.toolbar === 'selection') {
this.toolbar = ''
}
})
}
},
computed: {
multipleSelection () {
return this.config.selection && this.config.selection === 'multiple'
},
rowsSelected () {
if (this.multipleSelection) {
return this.rowSelection.filter(r => r).length
}
return this.rowSelection.length && this.rowSelection[0] !== -1 ? 1 : 0
},
selectedRows () {
if (this.multipleSelection) {
return this.rowSelection
.map((selected, index) => [selected, this.rows[index].__index])
.filter(row => row[0])
.map(row => {
return { index: row[1], data: this.data[row[1]] }
})
}
if (!this.rowSelection.length || this.rowSelection[0] === -1) {
return []
}
const
index = this.rows[this.rowSelection[0]].__index,
row = this.data[index]
return [{index, data: row}]
}
},
methods: {
clearSelection () {
if (!this.multipleSelection) {
this.rowSelection = [-1]
return
}
this.rowSelection = this.rows.map(() => false)
},
emitRowClick (row) {
this.$emit('rowclick', row)
}
}
}