Skip to content

Commit

Permalink
refactor(igxTreeGrid): make deleteRowById protected and refactor it, #…
Browse files Browse the repository at this point in the history
…2921

Refactor and deleteRowFromData - it should not use data with added rows.
Switch primaryKey and childDataKey parameters positions in commit function.
  • Loading branch information
wnvko committed Dec 7, 2018
1 parent 867588c commit 8422d08
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class IgxHierarchicalTransactionService<T extends HierarchicalTransaction
// transaction type and value of UPDATE states
if (transaction.type === TransactionType.DELETE) {
states.forEach((v: S, k: any) => {
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);
Expand All @@ -50,7 +50,7 @@ export class IgxHierarchicalTransactionService<T extends HierarchicalTransaction
}
}

public commit(data: any[], childDataKey?: any, primaryKey?: any): void {
public commit(data: any[], primaryKey?: any, childDataKey?: any): void {
if (childDataKey) {
DataUtil.mergeHierarchicalTransactions(data, this.getAggregatedChanges(true), childDataKey, primaryKey, true);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/app/tree-grid-flat-data/tree-grid-flat-data.sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class TreeGridFlatDataSampleComponent implements OnInit {
}

public deleteRow() {
this.grid1.deleteRowById(this.grid1.selectedRows()[0]);
this.grid1.deleteRow(this.grid1.selectedRows()[0]);
}

public selectDensity(event) {
Expand Down
4 changes: 2 additions & 2 deletions src/app/tree-grid/tree-grid.sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ export class TreeGridSampleComponent implements OnInit {
}

public deleteRow() {
this.grid1.deleteRowById(this.grid1.selectedRows()[0]);
this.grid1.deleteRow(this.grid1.selectedRows()[0]);
}

public undo() {
Expand All @@ -459,6 +459,6 @@ export class TreeGridSampleComponent implements OnInit {
}

public commit() {
this.grid1.transactions.commit(this.data, this.grid1.childDataKey, this.grid1.primaryKey);
this.grid1.transactions.commit(this.data, this.grid1.primaryKey, this.grid1.childDataKey);
}
}

0 comments on commit 8422d08

Please sign in to comment.