@@ -39,14 +39,14 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
39
39
public updateCascadeSelectionOnFilterAndCRUD (
40
40
parents : Set < any > ,
41
41
crudRowID ?: any ,
42
- visibleRowIDs : any [ ] = null ) {
42
+ visibleRowIDs : Set < any > = null ) {
43
43
if ( visibleRowIDs === null ) {
44
44
// if the tree grid has flat structure
45
45
// do not explicitly handle the selection state of the rows
46
46
if ( ! parents . size ) {
47
47
return ;
48
48
}
49
- visibleRowIDs = this . getRowIDs ( this . allData ) ;
49
+ visibleRowIDs = new Set ( this . getRowIDs ( this . allData ) ) ;
50
50
this . rowsToBeSelected = new Set ( this . rowSelection ) ;
51
51
this . rowsToBeIndeterminate = new Set ( this . indeterminateRows ) ;
52
52
if ( crudRowID ) {
@@ -147,8 +147,10 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
147
147
* retrieve the rows which should be added/removed to/from the old selection
148
148
*/
149
149
private handleAddedAndRemovedArgs ( args : any ) {
150
- args . removed = args . oldSelection . filter ( x => args . newSelection . indexOf ( x ) < 0 ) ;
151
- args . added = args . newSelection . filter ( x => args . oldSelection . indexOf ( x ) < 0 ) ;
150
+ const newSelectionSet = new Set ( args . newSelection ) ;
151
+ const oldSelectionSet = new Set ( args . oldSelection ) ;
152
+ args . removed = args . oldSelection . filter ( x => ! newSelectionSet . has ( x ) ) ;
153
+ args . added = args . newSelection . filter ( x => ! oldSelectionSet . has ( x ) ) ;
152
154
}
153
155
154
156
/**
@@ -158,14 +160,15 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
158
160
* @param visibleRowIDs list of all visible rowIds
159
161
* @returns a new set with all direct parents of the rows within rowsToBeProcessed set
160
162
*/
161
- private collectRowsChildrenAndDirectParents ( rowsToBeProcessed : Set < any > , visibleRowIDs : any [ ] ) : Set < any > {
163
+ private collectRowsChildrenAndDirectParents ( rowsToBeProcessed : Set < any > , visibleRowIDs : Set < any > , adding : boolean ) : Set < any > {
162
164
const processedRowsParents = new Set < any > ( ) ;
163
165
Array . from ( rowsToBeProcessed ) . forEach ( ( rowID ) => {
166
+ this . selectDeselectRow ( rowID , adding ) ;
164
167
const rowTreeRecord = this . grid . gridAPI . get_rec_by_id ( rowID ) ;
165
168
const rowAndAllChildren = this . get_all_children ( rowTreeRecord ) ;
166
169
rowAndAllChildren . forEach ( row => {
167
- if ( visibleRowIDs . indexOf ( row . key ) >= 0 ) {
168
- rowsToBeProcessed . add ( row . key ) ;
170
+ if ( visibleRowIDs . has ( row . key ) ) {
171
+ this . selectDeselectRow ( row . key , adding ) ;
169
172
}
170
173
} ) ;
171
174
if ( rowTreeRecord && rowTreeRecord . parent ) {
@@ -184,20 +187,15 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
184
187
this . rowsToBeSelected = new Set < any > ( args . oldSelection ? args . oldSelection : this . getSelectedRows ( ) ) ;
185
188
this . rowsToBeIndeterminate = new Set < any > ( this . getIndeterminateRows ( ) ) ;
186
189
187
- const visibleRowIDs = this . getRowIDs ( this . allData ) ;
190
+ const visibleRowIDs = new Set ( this . getRowIDs ( this . allData ) ) ;
188
191
189
192
const removed = new Set ( args . removed ) ;
190
193
const added = new Set ( args . added ) ;
191
194
192
195
if ( removed && removed . size ) {
193
196
let removedRowsParents = new Set < any > ( ) ;
194
197
195
- removedRowsParents = this . collectRowsChildrenAndDirectParents ( removed , visibleRowIDs ) ;
196
-
197
- removed . forEach ( removedRow => {
198
- this . rowsToBeSelected . delete ( removedRow ) ;
199
- this . rowsToBeIndeterminate . delete ( removedRow ) ;
200
- } ) ;
198
+ removedRowsParents = this . collectRowsChildrenAndDirectParents ( removed , visibleRowIDs , false ) ;
201
199
202
200
Array . from ( removedRowsParents ) . forEach ( ( parent ) => {
203
201
this . handleParentSelectionState ( parent , visibleRowIDs ) ;
@@ -207,12 +205,7 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
207
205
if ( added && added . size ) {
208
206
let addedRowsParents = new Set < any > ( ) ;
209
207
210
- addedRowsParents = this . collectRowsChildrenAndDirectParents ( added , visibleRowIDs ) ;
211
-
212
- added . forEach ( addedRow => {
213
- this . rowsToBeSelected . add ( addedRow ) ;
214
- this . rowsToBeIndeterminate . delete ( addedRow ) ;
215
- } ) ;
208
+ addedRowsParents = this . collectRowsChildrenAndDirectParents ( added , visibleRowIDs , true ) ;
216
209
217
210
Array . from ( addedRowsParents ) . forEach ( ( parent ) => {
218
211
this . handleParentSelectionState ( parent , visibleRowIDs ) ;
@@ -223,7 +216,7 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
223
216
/**
224
217
* recursively handle the selection state of the direct and indirect parents
225
218
*/
226
- private handleParentSelectionState ( treeRow : ITreeGridRecord , visibleRowIDs : any [ ] ) {
219
+ private handleParentSelectionState ( treeRow : ITreeGridRecord , visibleRowIDs : Set < any > ) {
227
220
if ( ! treeRow ) {
228
221
return ;
229
222
}
@@ -236,30 +229,26 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
236
229
/**
237
230
* Handle the selection state of a given row based the selection states of its direct children
238
231
*/
239
- private handleRowSelectionState ( treeRow : ITreeGridRecord , visibleRowIDs : any [ ] ) {
232
+ private handleRowSelectionState ( treeRow : ITreeGridRecord , visibleRowIDs : Set < any > ) {
240
233
let visibleChildren = [ ] ;
241
234
if ( treeRow && treeRow . children ) {
242
- visibleChildren = treeRow . children . filter ( child => visibleRowIDs . indexOf ( child . key ) >= 0 ) ;
235
+ visibleChildren = treeRow . children . filter ( child => visibleRowIDs . has ( child . key ) ) ;
243
236
}
244
237
if ( visibleChildren . length ) {
245
238
if ( visibleChildren . every ( row => this . rowsToBeSelected . has ( row . key ) ) ) {
246
- this . rowsToBeSelected . add ( treeRow . key ) ;
247
- this . rowsToBeIndeterminate . delete ( treeRow . key ) ;
239
+ this . selectDeselectRow ( treeRow . key , true ) ;
248
240
} else if ( visibleChildren . some ( row => this . rowsToBeSelected . has ( row . key ) || this . rowsToBeIndeterminate . has ( row . key ) ) ) {
249
241
this . rowsToBeIndeterminate . add ( treeRow . key ) ;
250
242
this . rowsToBeSelected . delete ( treeRow . key ) ;
251
243
} else {
252
- this . rowsToBeIndeterminate . delete ( treeRow . key ) ;
253
- this . rowsToBeSelected . delete ( treeRow . key ) ;
244
+ this . selectDeselectRow ( treeRow . key , false ) ;
254
245
}
255
246
} else {
256
247
// if the children of the row has been deleted and the row was selected do not change its state
257
248
if ( this . isRowSelected ( treeRow . key ) ) {
258
- this . rowsToBeSelected . add ( treeRow . key ) ;
259
- this . rowsToBeIndeterminate . delete ( treeRow . key ) ;
249
+ this . selectDeselectRow ( treeRow . key , true ) ;
260
250
} else {
261
- this . rowsToBeSelected . delete ( treeRow . key ) ;
262
- this . rowsToBeIndeterminate . delete ( treeRow . key ) ;
251
+ this . selectDeselectRow ( treeRow . key , false ) ;
263
252
}
264
253
}
265
254
}
@@ -276,4 +265,14 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
276
265
277
266
}
278
267
268
+ private selectDeselectRow ( rowID : any , select : boolean ) {
269
+ if ( select ) {
270
+ this . rowsToBeSelected . add ( rowID ) ;
271
+ this . rowsToBeIndeterminate . delete ( rowID ) ;
272
+ } else {
273
+ this . rowsToBeSelected . delete ( rowID ) ;
274
+ this . rowsToBeIndeterminate . delete ( rowID ) ;
275
+ }
276
+ }
277
+
279
278
}
0 commit comments