Skip to content

Commit 4a171a4

Browse files
Merge pull request #17320 from IgniteUI/ikitanov/fix-#17317-21.2.x
fix(grid): preserve refs and return new array to reflect changes
2 parents 9cf92ca + fd082f2 commit 4a171a4

3 files changed

Lines changed: 43 additions & 5 deletions

File tree

projects/igniteui-angular/grids/core/src/api.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ export class GridBaseAPIService<T extends GridType> implements GridServiceType {
320320
const transaction: Transaction = { id: rowId, type: TransactionType.ADD, newValue: rowData };
321321
grid.transactions.add(transaction);
322322
} else {
323-
grid.data.push(rowData);
324-
grid.data = cloneArray(grid.data);
323+
(grid.data ?? (grid.data = [])).push(rowData);
324+
grid.summaryService.clearSummaryCache();
325325
}
326326
grid.validation.markAsTouched(rowId);
327327
grid.validation.update(rowId, rowData);
@@ -336,8 +336,8 @@ export class GridBaseAPIService<T extends GridType> implements GridServiceType {
336336
const transaction: Transaction = { id: rowID, type: TransactionType.DELETE, newValue: null };
337337
grid.transactions.add(transaction, grid.data[index]);
338338
} else {
339-
grid.data.splice(index, 1);
340-
grid.data = cloneArray(grid.data);
339+
(grid.data ?? (grid.data = [])).splice(index, 1);
340+
grid.summaryService.clearSummaryCache();
341341
}
342342
} else {
343343
const state: State = grid.transactions.getState(rowID);

projects/igniteui-angular/grids/core/src/common/pipes.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ export class IgxGridTransactionPipe implements PipeTransform {
248248
this.grid.dataCloneStrategy);
249249
return result;
250250
}
251-
return collection;
251+
// Return a shallow copy so downstream pipes and igxGridForOf always
252+
// receive a new array reference when pipeTrigger changes, regardless
253+
// of whether the source array was mutated in place.
254+
return cloneArray(collection);
252255
}
253256
}
254257

projects/igniteui-angular/grids/grid/src/grid.crud.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,41 @@ describe('IgxGrid - CRUD operations #grid', () => {
4343
expect(grid.rowList.length).toEqual(expectedLength);
4444
});
4545

46+
it('should keep the external data array reference in sync after repeated addRow() calls', () => {
47+
const originalRef = fix.componentInstance.data;
48+
49+
for (let i = 2; i <= 6; i++) {
50+
grid.addRow({ index: i, value: i * 10 });
51+
}
52+
fix.detectChanges();
53+
54+
expect(grid.data).toBe(originalRef);
55+
expect(data.length).toEqual(6);
56+
expect(data.find(r => r.index === 6)).toBeDefined();
57+
expect(data).toBe(grid.data);
58+
});
59+
60+
it('should keep the external data array reference in sync after repeated deleteRow() calls', () => {
61+
const originalRef = fix.componentInstance.data;
62+
63+
for (let i = 2; i <= 5; i++) {
64+
grid.addRow({ index: i, value: i * 10 });
65+
}
66+
fix.detectChanges();
67+
68+
expect(data.length).toEqual(5);
69+
70+
grid.deleteRow(1);
71+
grid.deleteRow(3);
72+
fix.detectChanges();
73+
74+
expect(grid.data).toBe(originalRef);
75+
expect(data.length).toEqual(3);
76+
expect(data.find(r => r.index === 1)).toBeUndefined();
77+
expect(data.find(r => r.index === 3)).toBeUndefined();
78+
expect(data).toBe(grid.data);
79+
});
80+
4681
// No longer supported - array mutations are not detected automatically, need ref change.
4782
xit('should support adding rows by manipulating the `data` @Input of the grid', () => {
4883
// Add to the data array without changing the reference

0 commit comments

Comments
 (0)