Skip to content

Commit 3edba8f

Browse files
committed
fix(grid): call detectChanges when update cell or row #5763
1 parent 103f1df commit 3edba8f

File tree

6 files changed

+86
-9
lines changed

6 files changed

+86
-9
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ export class GridBaseAPIService <T extends IgxGridBaseComponent & IGridDataBinda
118118
public submit_value() {
119119
const cell = this.grid.crudService.cell;
120120
if (cell) {
121-
const args = this.update_cell(cell, cell.editValue);
122-
if (args.cancel) {
123-
return;
121+
if (!(isEqual(cell.value, cell.editValue))) {
122+
const args = this.update_cell(cell, cell.editValue);
123+
if (args.cancel) {
124+
return;
125+
}
124126
}
125127
this.escape_editMode();
126128
}

projects/igniteui-angular/src/lib/grids/grid-base.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3619,7 +3619,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
36193619
this.gridAPI.escape_editMode();
36203620
}
36213621

3622-
this.cdr.markForCheck();
3622+
this.cdr.detectChanges();
36233623
}
36243624
}
36253625
}
@@ -3645,7 +3645,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
36453645
}
36463646
const row = new IgxRow(rowSelector, -1, this.gridAPI.getRowData(rowSelector));
36473647
this.gridAPI.update_row(row, value);
3648-
this.cdr.markForCheck();
3648+
this.cdr.detectChanges();
36493649
}
36503650
}
36513651

projects/igniteui-angular/src/lib/grids/grid/cell.spec.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Component, ViewChild, OnInit } from '@angular/core';
22
import { async, TestBed, fakeAsync, tick } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
5-
import { IgxColumnComponent, IgxGridCellComponent, IgxGridComponent, IgxGridModule, IGridCellEventArgs } from './index';
5+
import { IgxColumnComponent, IgxGridCellComponent, IgxGridComponent, IgxGridModule, IGridCellEventArgs, IGridEditEventArgs } from './index';
66
import { SortingDirection } from '../../data-operations/sorting-expression.interface';
77
import { UIInteractions, wait } from '../../test-utils/ui-interactions.spec';
88
import { configureTestSuite } from '../../test-utils/configure-suite';
@@ -830,6 +830,50 @@ describe('IgxGrid - Cell component', () => {
830830
});
831831
});
832832

833+
it(`Should be able to update other cell in 'onCellEdit' event`, () => {
834+
const fixture = TestBed.createComponent(CellEditingTestComponent);
835+
fixture.detectChanges();
836+
const grid = fixture.componentInstance.grid;
837+
grid.primaryKey = 'personNumber';
838+
fixture.detectChanges();
839+
840+
spyOn(grid.onCellEdit, 'emit').and.callThrough();
841+
grid.onCellEdit.subscribe((e: IGridEditEventArgs) => {
842+
if (e.cellID.columnID === 0) {
843+
grid.updateCell(1, e.rowID, 'age');
844+
}
845+
});
846+
847+
let cell = grid.getCellByColumn(0, 'fullName');
848+
849+
UIInteractions.simulateClickAndSelectCellEvent(cell);
850+
fixture.detectChanges();
851+
852+
cell.nativeElement.dispatchEvent(new MouseEvent('dblclick'));
853+
fixture.detectChanges();
854+
855+
expect(cell.editMode).toBe(true);
856+
let editTemplate = fixture.debugElement.query(By.css('input'));
857+
UIInteractions.sendInput(editTemplate, 'New Name');
858+
fixture.detectChanges();
859+
860+
// press tab on edited cell
861+
UIInteractions.triggerKeyDownWithBlur('tab', cell.nativeElement, true);
862+
fixture.detectChanges();
863+
864+
expect(grid.onCellEdit.emit).toHaveBeenCalledTimes(2);
865+
cell = grid.getCellByColumn(0, 'age');
866+
expect(cell.editMode).toBe(true);
867+
expect(cell.value).toEqual(1);
868+
expect(cell.editValue).toEqual(1);
869+
editTemplate = fixture.debugElement.query(By.css('input'));
870+
expect(editTemplate.nativeElement.value).toEqual('1');
871+
872+
cell = grid.getCellByColumn(0, 'fullName');
873+
expect(cell.value).toEqual('New Name');
874+
});
875+
});
876+
833877
it('should fit last cell in the available display container when there is vertical scroll.', async(() => {
834878
const fix = TestBed.createComponent(VirtualGridComponent);
835879
fix.detectChanges();

projects/igniteui-angular/src/lib/grids/grid/grid-selection.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ describe('IgxGrid - Row Selection', () => {
6666
const fix = TestBed.createComponent(GridWithPrimaryKeyComponent);
6767
fix.detectChanges();
6868
const grid = fix.componentInstance.grid;
69-
spyOn(grid.cdr, 'markForCheck').and.callThrough();
69+
spyOn(grid.onRowEdit, 'emit').and.callThrough();
7070
expect(grid.primaryKey).toBeTruthy();
7171
expect(grid.rowList.length).toEqual(10, 'All 10 rows should initialized');
7272
expect(grid.getRowByKey(2).rowData['JobTitle']).toMatch('Director');
7373
grid.updateRow({ID: 2, Name: 'Gilberto Todd', JobTitle: 'Vice President'}, 2);
74-
expect(grid.cdr.markForCheck).toHaveBeenCalledTimes(1);
74+
expect(grid.onRowEdit.emit).toHaveBeenCalledTimes(1);
7575
fix.detectChanges();
7676
expect(grid.getRowByIndex(1).rowData['JobTitle']).toMatch('Vice President');
7777
expect(grid.getRowByKey(2).rowData['JobTitle']).toMatch('Vice President');

projects/igniteui-angular/src/lib/grids/grid/grid.component.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,38 @@ describe('IgxGrid Component Tests', () => {
18131813
expect(grid.onRowEdit.emit).toHaveBeenCalledWith(rowArgs);
18141814
}));
18151815

1816+
it(`Should properly emit 'onCellEdit' event `, fakeAsync(() => {
1817+
const fix = TestBed.createComponent(IgxGridRowEditingComponent);
1818+
fix.detectChanges();
1819+
tick(16);
1820+
1821+
const grid = fix.componentInstance.grid;
1822+
spyOn(grid.onCellEdit, 'emit').and.callThrough();
1823+
spyOn(grid.onRowEdit, 'emit').and.callThrough();
1824+
1825+
let cell = grid.getCellByColumn(0, 'ProductName');
1826+
const cellArgs = { cellID: cell.cellID, rowID: cell.row.rowID, oldValue: 'Chai', newValue: 'New Value', cancel: false };
1827+
1828+
cell.nativeElement.dispatchEvent(new MouseEvent('dblclick'));
1829+
tick(16);
1830+
fix.detectChanges();
1831+
1832+
expect(cell.editMode).toBe(true);
1833+
const editTemplate = fix.debugElement.query(By.css('input'));
1834+
UIInteractions.sendInput(editTemplate, 'New Value');
1835+
fix.detectChanges();
1836+
1837+
// Click on cell in different row
1838+
cell = grid.getCellByColumn(2, 'ProductName');
1839+
UIInteractions.simulateClickAndSelectCellEvent(cell);
1840+
tick(16);
1841+
fix.detectChanges();
1842+
1843+
expect(grid.onRowEdit.emit).toHaveBeenCalledTimes(1);
1844+
expect(grid.onCellEdit.emit).toHaveBeenCalledTimes(1);
1845+
expect(grid.onCellEdit.emit).toHaveBeenCalledWith(cellArgs);
1846+
}));
1847+
18161848
it('Should display the banner below the edited row if it is not the last one', fakeAsync(() => {
18171849
const fix = TestBed.createComponent(IgxGridRowEditingComponent);
18181850
fix.detectChanges();

projects/igniteui-angular/src/lib/grids/grid/grid.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,6 @@ export class IgxGridComponent extends IgxGridBaseComponent implements IGridDataB
597597
*/
598598
public groupBy(expression: IGroupingExpression | Array<IGroupingExpression>): void {
599599
this.endEdit(true);
600-
this._gridAPI.submit_value();
601600
if (expression instanceof Array) {
602601
this._gridAPI.groupBy_multiple(expression);
603602
} else {

0 commit comments

Comments
 (0)