@@ -127,18 +127,15 @@ export default Vue.extend({
127
127
const states = this . states ;
128
128
states . isAllSelected = false ;
129
129
const oldSelection = states . selection ;
130
- if ( states . selection . length ) {
130
+ if ( oldSelection . length ) {
131
131
states . selection = [ ] ;
132
- }
133
- if ( oldSelection . length > 0 ) {
134
- this . table . $emit ( 'selection-change' , states . selection ? states . selection . slice ( ) : [ ] ) ;
132
+ this . table . $emit ( 'selection-change' , [ ] ) ;
135
133
}
136
134
} ,
137
135
138
136
cleanSelection ( ) {
139
- const selection = this . states . selection || [ ] ;
140
- const data = this . states . data ;
141
- const rowKey = this . states . rowKey ;
137
+ const states = this . states ;
138
+ const { data, rowKey, selection } = states ;
142
139
let deleted ;
143
140
if ( rowKey ) {
144
141
deleted = [ ] ;
@@ -150,24 +147,19 @@ export default Vue.extend({
150
147
}
151
148
}
152
149
} else {
153
- deleted = selection . filter ( ( item ) => {
154
- return data . indexOf ( item ) === - 1 ;
155
- } ) ;
150
+ deleted = selection . filter ( item => data . indexOf ( item ) === - 1 ) ;
156
151
}
157
-
158
- deleted . forEach ( ( deletedItem ) => {
159
- selection . splice ( selection . indexOf ( deletedItem ) , 1 ) ;
160
- } ) ;
161
-
162
152
if ( deleted . length ) {
163
- this . table . $emit ( 'selection-change' , selection ? selection . slice ( ) : [ ] ) ;
153
+ const newSelection = selection . filter ( item => deleted . indexOf ( item ) === - 1 ) ;
154
+ states . selection = newSelection ;
155
+ this . table . $emit ( 'selection-change' , newSelection . slice ( ) ) ;
164
156
}
165
157
} ,
166
158
167
159
toggleRowSelection ( row , selected , emitChange = true ) {
168
160
const changed = toggleRowStatus ( this . states . selection , row , selected ) ;
169
161
if ( changed ) {
170
- const newSelection = this . states . selection ? this . states . selection . slice ( ) : [ ] ;
162
+ const newSelection = ( this . states . selection || [ ] ) . slice ( ) ;
171
163
// 调用 API 修改选中值,不触发 select 事件
172
164
if ( emitChange ) {
173
165
this . table . $emit ( 'select' , newSelection , row ) ;
@@ -207,17 +199,15 @@ export default Vue.extend({
207
199
208
200
updateSelectionByRowKey ( ) {
209
201
const states = this . states ;
210
- const { selection, rowKey, data = [ ] } = states ;
202
+ const { selection, rowKey, data } = states ;
211
203
const selectedMap = getKeysMap ( selection , rowKey ) ;
212
- // TODO:这里的代码可以优化
213
- states . selection = data . reduce ( ( prev , row ) => {
204
+ data . forEach ( row => {
214
205
const rowId = getRowIdentity ( row , rowKey ) ;
215
206
const rowInfo = selectedMap [ rowId ] ;
216
207
if ( rowInfo ) {
217
- prev . push ( row ) ;
208
+ selection [ rowInfo . index ] = row ;
218
209
}
219
- return prev ;
220
- } , [ ] ) ;
210
+ } ) ;
221
211
} ,
222
212
223
213
updateAllSelected ( ) {
0 commit comments