diff --git a/projects/igniteui-angular/src/lib/grids/grid-base.component.ts b/projects/igniteui-angular/src/lib/grids/grid-base.component.ts index 0bb9d4cdf51..84cfd6badad 100644 --- a/projects/igniteui-angular/src/lib/grids/grid-base.component.ts +++ b/projects/igniteui-angular/src/lib/grids/grid-base.component.ts @@ -2896,11 +2896,8 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements } } - /** - * @hidden - * @param - */ - public deleteRowById(rowId: any) { + /** @hidden */ + protected deleteRowById(rowId: any) { let index: number; const data = this.gridAPI.get_all_data(this.id); if (this.primaryKey) { diff --git a/projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid-integration.spec.ts b/projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid-integration.spec.ts index 4018bdd5156..c4ac2deb7cf 100644 --- a/projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid-integration.spec.ts +++ b/projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid-integration.spec.ts @@ -668,7 +668,7 @@ describe('IgxTreeGrid - Integration', () => { expect(targetCell.nativeElement.classList).toContain('igx-grid__td--edited'); // Commit - trans.commit(treeGrid.data, treeGrid.childDataKey, treeGrid.primaryKey); + trans.commit(treeGrid.data, treeGrid.primaryKey, treeGrid.childDataKey); tick(); // Verify the correct value is set @@ -749,7 +749,7 @@ describe('IgxTreeGrid - Integration', () => { expect(rowData[475].Age).not.toEqual(treeGrid.getRowByKey(475).rowData.Age); expect(rowData[19].Name).not.toEqual(treeGrid.getRowByKey(19).rowData.Name); expect(treeGridData[0].Employees[475]).toEqual(initialData[0].Employees[475]); - trans.commit(treeGridData, treeGrid.childDataKey, treeGrid.primaryKey); + trans.commit(treeGridData, treeGrid.primaryKey, treeGrid.childDataKey); expect(treeGridData[0].Name).toEqual('Testy Testington'); expect(treeGridData[0].Employees[0].Age).toEqual(42); expect(treeGridData[1].Name).toEqual('Old Richard'); @@ -890,7 +890,7 @@ describe('IgxTreeGrid - Integration', () => { expect(treeGrid.data.findIndex(e => e.ID === rowData.child.ID)).toEqual(-1); expect(treeGrid.transactions.getAggregatedChanges(true).length).toEqual(2); // 4. Commit - treeGrid.transactions.commit(treeGrid.data, treeGrid.childDataKey, treeGrid.primaryKey); + treeGrid.transactions.commit(treeGrid.data, treeGrid.primaryKey, treeGrid.childDataKey); // 5. verify the rows are committed, the styles are OK expect(treeGrid.data.findIndex(e => e.ID === rowData.parent.ID)).not.toEqual(-1); expect(treeGrid.data.findIndex(e => e.ID === rowData.child.ID)).not.toEqual(-1); @@ -1241,7 +1241,7 @@ describe('IgxTreeGrid - Integration', () => { treeGrid.addRow(childRow, 11); expect(treeGrid.getRowByKey(11).nativeElement.classList).toContain('igx-grid__tr--edited'); expect(treeGrid.getRowByKey(12).nativeElement.classList).toContain('igx-grid__tr--edited'); - trans.commit(treeGrid.data, treeGrid.childDataKey, treeGrid.primaryKey); + trans.commit(treeGrid.data, treeGrid.primaryKey, treeGrid.childDataKey); expect(treeGrid.getRowByKey(11).nativeElement.classList).not.toContain('igx-grid__tr--edited'); expect(treeGrid.getRowByKey(12).nativeElement.classList).not.toContain('igx-grid__tr--edited'); treeGrid.addRow(grandChildRow, 12); diff --git a/projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid.component.ts b/projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid.component.ts index c8a88f1ac8a..ff1de5d61f9 100644 --- a/projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid.component.ts +++ b/projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid.component.ts @@ -397,30 +397,29 @@ export class IgxTreeGridComponent extends IgxGridBaseComponent { } } - /** - * Removes the `IgxGridRowComponent` and the corresponding data record by primary key. - * Requires that the `primaryKey` property is set. - * The method accept rowSelector as a parameter, which is the rowID. - * ```typescript - * this.grid1.deleteRow(0); - * ``` - * @param rowSelector - * @memberof IgxGridBaseComponent - */ - public deleteRowById(rowId: any) { - if (this.primaryKey && this.foreignKey && this.cascadeOnDelete && this.transactions.enabled) { + /** @hidden */ + protected deleteRowById(rowId: any) { + // if this is flat self-referencing data, and CascadeOnDelete is set to true + // and if we have transactions we should start pending transaction. This allows + // us in case of delete action to delete all child rows as single undo action + const flatDataWithCascadeOnDeleteAndTransactions = + this.primaryKey && + this.foreignKey && + this.cascadeOnDelete && + this.transactions.enabled; + + if (flatDataWithCascadeOnDeleteAndTransactions) { this.transactions.startPending(); } + super.deleteRowById(rowId); - if (this.primaryKey && this.foreignKey && this.cascadeOnDelete && this.transactions.enabled) { + if (flatDataWithCascadeOnDeleteAndTransactions) { this.transactions.endPending(true); } } - /** - * @hidden - */ + /** @hidden */ protected deleteRowFromData(rowID: any, index: number) { if (this.primaryKey && this.foreignKey) { super.deleteRowFromData(rowID, index); @@ -436,21 +435,23 @@ export class IgxTreeGridComponent extends IgxGridBaseComponent { } } else { const record = this.records.get(rowID); - const childData = record.parent ? record.parent.data[this.childDataKey] : - this.transactions.enabled ? this._gridAPI.get_all_data(this.id, true) : this.data; - index = this.primaryKey ? childData.map(c => c[this.primaryKey]).indexOf(rowID) : - childData.indexOf(rowID); + const collection = record.parent ? record.parent.data[this.childDataKey] : this.data; + index = this.primaryKey ? + collection.map(c => c[this.primaryKey]).indexOf(rowID) : + collection.indexOf(rowID); + if (this.transactions.enabled) { const path = this.generateRowPath(rowID); this.transactions.add({ - id: rowID, - type: TransactionType.DELETE, - newValue: null, - path: path - }, - childData[index]); + id: rowID, + type: TransactionType.DELETE, + newValue: null, + path: path + }, + collection[index] + ); } else { - childData.splice(index, 1); + collection.splice(index, 1); } } } diff --git a/projects/igniteui-angular/src/lib/services/transaction/igx-hierarchical-transaction.ts b/projects/igniteui-angular/src/lib/services/transaction/igx-hierarchical-transaction.ts index ab11e7f7167..2f722f5d4e3 100644 --- a/projects/igniteui-angular/src/lib/services/transaction/igx-hierarchical-transaction.ts +++ b/projects/igniteui-angular/src/lib/services/transaction/igx-hierarchical-transaction.ts @@ -36,7 +36,7 @@ export class IgxHierarchicalTransactionService { - if (v.path.indexOf(transaction.id) !== -1) { + if (v.path && v.path.indexOf(transaction.id) !== -1) { switch (v.type) { case TransactionType.ADD: states.delete(k); @@ -50,7 +50,7 @@ export class IgxHierarchicalTransactionService