Skip to content

Commit 78a519b

Browse files
authored
Merge pull request #5797 from IgniteUI/nrobakova/fix-issue-5763-81
Should be able to update different cell from onCellEdit event
2 parents cd309bf + 73584f5 commit 78a519b

File tree

6 files changed

+92
-16
lines changed

6 files changed

+92
-16
lines changed

projects/igniteui-angular/src/lib/core/grid-selection.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ export class IgxGridCRUDService {
152152
}
153153

154154
begin(cell): void {
155-
this.cell = this.createCell(cell);
156-
this.cell.primaryKey = this.primaryKey;
155+
const newCell = this.createCell(cell);
156+
newCell.primaryKey = this.primaryKey;
157157
const args = {
158-
cellID: this.cell.id,
159-
rowID: this.cell.id.rowID,
160-
oldValue: this.cell.value,
158+
cellID: newCell.id,
159+
rowID: newCell.id.rowID,
160+
oldValue: newCell.value,
161161
cancel: false
162162
};
163163

@@ -168,20 +168,22 @@ export class IgxGridCRUDService {
168168
return;
169169
}
170170

171-
172171
if (this.rowEditing) {
173-
if (!this.row) {
172+
if (this.row && !this.sameRow(newCell.id.rowID)) {
173+
this.grid.endEdit(true);
174+
this.cell = newCell;
174175
this.beginRowEdit();
175176
return;
176177
}
177178

178-
if (this.row && !this.sameRow(this.cell.id.rowID)) {
179-
this.grid.endEdit(true);
180-
this.cell = this.createCell(cell);
179+
this.cell = newCell;
180+
181+
if (!this.row) {
181182
this.beginRowEdit();
182183
return;
183184
}
184185
} else {
186+
this.cell = newCell;
185187
this.endRowEdit();
186188
}
187189
}

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: 44 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';
@@ -839,6 +839,49 @@ describe('IgxGrid - Cell component', () => {
839839
});
840840
});
841841

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