Skip to content

Commit 4174433

Browse files
committed
feat(hgrid): allow to prevent exiting of edit mode when row is toggled
1 parent 3974655 commit 4174433

File tree

5 files changed

+110
-30
lines changed

5 files changed

+110
-30
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,13 @@ export class GridBaseAPIService<T extends GridType> implements GridServiceType {
397397
if (args.cancel) {
398398
return;
399399
}
400+
401+
const isHierarchicalGrid = grid.nativeElement.tagName.toLowerCase() === 'igx-hierarchical-grid';
402+
403+
if (isHierarchicalGrid) {
404+
grid.hgridAPI.endEditAll();
405+
}
406+
400407
expandedStates.set(rowID, expanded);
401408
grid.expansionStates = expandedStates;
402409
this.crudService.endEdit(false);

projects/igniteui-angular/src/lib/grids/common/grid.interface.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,23 @@ export interface IGridDataBindable {
5050
}
5151

5252
export interface CellType {
53-
value: any;
54-
editValue: any;
55-
selected: boolean;
56-
active: boolean;
57-
editable: boolean;
58-
editMode: boolean;
53+
value: any;
54+
editValue: any;
55+
selected: boolean;
56+
active: boolean;
57+
editable: boolean;
58+
editMode: boolean;
5959
nativeElement?: HTMLElement;
60-
column: ColumnType;
61-
row: RowType;
62-
grid: GridType;
63-
id: { rowID: any; columnID: number; rowIndex: number };
60+
column: ColumnType;
61+
row: RowType;
62+
grid: GridType;
63+
id: { rowID: any; columnID: number; rowIndex: number };
6464
cellID?: any;
6565
readonly?: boolean;
6666
title?: any;
67-
width: string;
67+
width: string;
6868
visibleColumnIndex?: number;
69-
update: (value: any) => void;
69+
update: (value: any) => void;
7070
setEditMode?(value: boolean): void;
7171
calculateSizeToFit?(range: any): number;
7272
activate?(event: FocusEvent | KeyboardEvent): void;
@@ -95,7 +95,7 @@ export interface RowType {
9595
children?: RowType[];
9696
parent?: RowType;
9797
hasChildren?: boolean;
98-
treeRow? : ITreeGridRecord;
98+
treeRow?: ITreeGridRecord;
9999
addRowUI?: boolean;
100100
focused?: boolean;
101101
grid: GridType;
@@ -247,6 +247,7 @@ export interface GridServiceType {
247247
getChildGrid?(path: IPathSegment[]): GridType;
248248
// XXX: Fix type
249249
unsetChildRowIsland?(rowIsland: any): void;
250+
endEditAll?(): void;
250251
}
251252

252253

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ import { Subject } from 'rxjs';
33
import { GridType, IPathSegment } from '../common/grid.interface';
44
import { Injectable } from '@angular/core';
55
import { GridBaseAPIService } from '../api.service';
6-
76
@Injectable()
87
export class IgxHierarchicalGridAPIService extends GridBaseAPIService<GridType> {
98
protected childRowIslands: Map<string, IgxRowIslandComponent> = new Map<string, IgxRowIslandComponent>();
10-
protected childGrids: Map<string, Map<any, GridType>> =
9+
protected childGrids: Map<string, Map<any, GridType>> =
1110
new Map<string, Map<any, GridType>>();
1211

1312
public registerChildRowIsland(rowIsland: IgxRowIslandComponent) {
@@ -127,4 +126,15 @@ export class IgxHierarchicalGridAPIService extends GridBaseAPIService<GridType>
127126
const index = this.get_row_index_in_data(rowID, data);
128127
return data[index];
129128
}
129+
130+
public endEditAll(): void {
131+
if (this.grid.gridAPI.crudService.cellInEditMode) {
132+
this.grid.gridAPI.crudService.endEdit();
133+
}
134+
this.getChildGrids(true).forEach(g => {
135+
if (g.gridAPI.crudService.cellInEditMode) {
136+
g.gridAPI.crudService.endEdit();
137+
}
138+
});
139+
}
130140
}

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,76 @@ describe('Basic IgxHierarchicalGrid #hGrid', () => {
438438
expect(childGridSecondRow.inEditMode).toBeFalsy();
439439
});
440440

441+
it('should be able to prevent exiting of edit mode when row is toggled through the UI', async () => {
442+
hierarchicalGrid.primaryKey = 'ID';
443+
hierarchicalGrid.rowEditable = true;
444+
hierarchicalGrid.rowToggle.subscribe((e) => {
445+
e.cancel = true;
446+
});
447+
fixture.detectChanges();
448+
wait();
449+
450+
const masterGridFirstRow = hierarchicalGrid.hgridAPI.get_row_by_index(0) as IgxHierarchicalRowComponent;
451+
expect(masterGridFirstRow.expanded).toBeFalsy();
452+
453+
const masterGridSecondCell = masterGridFirstRow.cells.find(c => c.columnIndex === 1);
454+
expect(masterGridSecondCell.editMode).toBeFalsy();
455+
456+
masterGridSecondCell.setEditMode(true);
457+
fixture.detectChanges();
458+
wait();
459+
460+
expect(masterGridSecondCell.editMode).toBeTruthy();
461+
462+
UIInteractions.simulateClickAndSelectEvent(masterGridFirstRow.expander);
463+
fixture.detectChanges();
464+
wait();
465+
466+
expect(masterGridFirstRow.expanded).toBeFalsy();
467+
expect(masterGridSecondCell.editMode).toBeTruthy();
468+
469+
hierarchicalGrid.rowToggle.subscribe((e) => {
470+
e.cancel = false;
471+
});
472+
UIInteractions.simulateClickAndSelectEvent(masterGridFirstRow.expander);
473+
fixture.detectChanges();
474+
wait();
475+
476+
expect(masterGridFirstRow.expanded).toBeTruthy();
477+
expect(masterGridSecondCell.editMode).toBeFalsy();
478+
479+
const childGrid = hierarchicalGrid.hgridAPI.getChildGrids(false)[0] as IgxHierarchicalGridComponent;
480+
expect(childGrid).toBeDefined();
481+
482+
childGrid.primaryKey = 'ID';
483+
childGrid.rowEditable = true;
484+
childGrid.rowToggle.subscribe((e) => {
485+
e.cancel = true;
486+
});
487+
fixture.detectChanges();
488+
wait();
489+
490+
childGrid.columnList.find(c => c.index === 1).editable = true;
491+
const childGridSecondRow = childGrid.gridAPI.get_row_by_index(1) as IgxHierarchicalRowComponent;
492+
expect(childGridSecondRow.expanded).toBeFalsy();
493+
494+
const childGridSecondCell = childGridSecondRow.cells.find(c => c.columnIndex === 1);
495+
expect(childGridSecondCell.editMode).toBeFalsy();
496+
497+
childGridSecondCell.setEditMode(true);
498+
fixture.detectChanges();
499+
wait();
500+
501+
expect(childGridSecondCell.editMode).toBeTruthy();
502+
503+
UIInteractions.simulateClickAndSelectEvent(masterGridFirstRow.expander);
504+
fixture.detectChanges();
505+
wait();
506+
507+
expect(childGrid.gridAPI.crudService.cellInEditMode).toBeTruthy();
508+
expect(childGridSecondRow.inEditMode).toBeTruthy();
509+
});
510+
441511
it('child grid width should be recalculated if parent no longer shows scrollbar.', async () => {
442512
hierarchicalGrid.height = '1000px';
443513
fixture.detectChanges();

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class IgxHierarchicalRowComponent extends IgxRowDirective {
5858
}
5959

6060
public get viewIndex(): number {
61-
return this.index + (this.grid.paginator?.page || 0 ) * (this.grid.paginator?.perPage || 0);
61+
return this.index + (this.grid.paginator?.page || 0) * (this.grid.paginator?.perPage || 0);
6262
}
6363

6464
/**
@@ -80,7 +80,7 @@ export class IgxHierarchicalRowComponent extends IgxRowDirective {
8080
}
8181

8282
public get hasChildren() {
83-
return !!this.grid.childLayoutKeys.length;
83+
return !!this.grid.childLayoutKeys.length;
8484
}
8585

8686
/**
@@ -91,10 +91,10 @@ export class IgxHierarchicalRowComponent extends IgxRowDirective {
9191
return this.grid && this.grid.highlightedRowID === this.key;
9292
}
9393

94-
/**
95-
* @hidden
96-
*/
97-
public expanderClick(event) {
94+
/**
95+
* @hidden
96+
*/
97+
public expanderClick(event) {
9898
event.stopPropagation();
9999
this.toggle();
100100
}
@@ -109,7 +109,6 @@ export class IgxHierarchicalRowComponent extends IgxRowDirective {
109109
if (this.added) {
110110
return;
111111
}
112-
this.endEdit(this.grid.rootGrid);
113112
this.grid.gridAPI.set_row_expansion_state(this.key, !this.expanded);
114113
this.grid.cdr.detectChanges();
115114
}
@@ -150,13 +149,6 @@ export class IgxHierarchicalRowComponent extends IgxRowDirective {
150149

151150
// TODO: consider moving into CRUD
152151
protected endEdit(grid: GridType) {
153-
if (grid.gridAPI.crudService.cellInEditMode) {
154-
grid.gridAPI.crudService.endEdit();
155-
}
156-
grid.hgridAPI.getChildGrids(true).forEach(g => {
157-
if (g.gridAPI.crudService.cellInEditMode) {
158-
g.gridAPI.crudService.endEdit();
159-
}
160-
});
152+
grid.gridAPI.endEditAll();
161153
}
162154
}

0 commit comments

Comments
 (0)