|
27 | 27 | ...MdPropValidator('md-selectable', ['multiple', 'single'])
|
28 | 28 | },
|
29 | 29 | mdDisabled: Boolean,
|
30 |
| - mdAutoSelect: Boolean |
| 30 | + mdAutoSelect: Boolean, |
| 31 | + mdItem: Object |
31 | 32 | },
|
32 | 33 | inject: ['MdTable'],
|
33 | 34 | data: () => ({
|
|
39 | 40 | return Object.keys(this.MdTable.selectable).length
|
40 | 41 | },
|
41 | 42 | isSingleSelected () {
|
42 |
| - return this.MdTable.singleSelection === this.mdId |
| 43 | + return this.MdTable.singleSelection === this.mdItem |
43 | 44 | },
|
44 | 45 | hasMultipleSelection () {
|
45 | 46 | return this.MdTable.hasValue && this.mdSelectable === 'multiple'
|
|
55 | 56 | 'md-selected-single': this.isSingleSelected
|
56 | 57 | }
|
57 | 58 | }
|
| 59 | + }, |
| 60 | + isInSelectedItems () { |
| 61 | + return this.MdTable.selectedItems.includes(this.mdItem) |
58 | 62 | }
|
59 | 63 | },
|
60 | 64 | watch: {
|
|
65 | 69 | this.addSelectableItem()
|
66 | 70 | }
|
67 | 71 | },
|
68 |
| - mdId (newId, oldId) { |
69 |
| - this.removeSelectableItem(oldId) |
70 |
| - this.addSelectableItem(newId) |
| 72 | + isSelected (val) { |
| 73 | + let noChange = (val && this.isInSelectedItems) || (!val && !this.isInSelectedItems) |
| 74 | +
|
| 75 | + if (noChange) { |
| 76 | + return false |
| 77 | + } |
| 78 | +
|
| 79 | + this.MdTable.manageItemSelection(this.mdItem) |
| 80 | + }, |
| 81 | + isInSelectedItems (val) { |
| 82 | + this.isSelected = val |
71 | 83 | },
|
72 |
| - isSelected () { |
73 |
| - this.MdTable.manageItemSelection(this.mdIndex) |
| 84 | + mdSelectable () { |
| 85 | + this.MdTable.selectingMode = this.mdSelectable |
74 | 86 | }
|
75 | 87 | },
|
76 | 88 | methods: {
|
|
87 | 99 | this.isSelected = !this.isSelected
|
88 | 100 | },
|
89 | 101 | selectRowIfSingle () {
|
90 |
| - if (this.MdTable.singleSelection === this.mdId) { |
| 102 | + if (this.MdTable.singleSelection === this.mdItem) { |
91 | 103 | this.MdTable.singleSelection = null
|
92 |
| - this.$emit('md-selected', null) |
93 | 104 | } else {
|
94 |
| - this.MdTable.singleSelection = this.mdId |
95 |
| - this.$emit('md-selected', this.MdTable.getModelItem(this.mdIndex)) |
| 105 | + this.MdTable.singleSelection = this.mdItem |
96 | 106 | }
|
97 | 107 | },
|
98 | 108 | selectRowIfMultiple () {
|
99 | 109 | if (this.mdAutoSelect) {
|
100 | 110 | this.toggleSelection()
|
101 | 111 | }
|
102 | 112 | },
|
103 |
| - addSelectableItem (id) { |
104 |
| - if (this.hasMultipleSelection && !this.mdDisabled) { |
105 |
| - this.$set(this.MdTable.selectable, id || this.mdId, isSelected => { |
106 |
| - this.isSelected = isSelected |
107 |
| - }) |
| 113 | + addSelectableItem () { |
| 114 | + if (!this.hasMultipleSelection || this.mdDisabled) { |
| 115 | + return |
108 | 116 | }
|
| 117 | +
|
| 118 | + if (this.MdTable.selectable.includes(this.mdItem)) { |
| 119 | + return |
| 120 | + } |
| 121 | +
|
| 122 | + this.MdTable.selectable.push(this.mdItem) |
109 | 123 | },
|
110 |
| - removeSelectableItem (id) { |
111 |
| - if (this.hasMultipleSelection) { |
112 |
| - this.$delete(this.MdTable.selectable, id || this.mdId) |
| 124 | + removeSelectableItem () { |
| 125 | + if (!this.hasMultipleSelection) { |
| 126 | + return |
113 | 127 | }
|
| 128 | +
|
| 129 | + this.MdTable.selectable = this.MdTable.selectable.filter(item => item !== this.mdItem) |
114 | 130 | }
|
115 | 131 | },
|
116 | 132 | created () {
|
117 | 133 | this.addSelectableItem()
|
| 134 | + this.MdTable.selectingMode = this.mdSelectable |
118 | 135 | },
|
119 | 136 | beforeDestroy () {
|
120 | 137 | this.removeSelectableItem()
|
|
0 commit comments