Skip to content

Commit 3e50952

Browse files
Merge pull request #13889 from IgniteUI/bpachilova/fix-13881-16.1.x
fix(hierarchical-grid): do not use hierarchy pipe data when data is explicitly set by user - 16.1.x
2 parents 2ab5429 + 9fb2267 commit 3e50952

File tree

2 files changed

+67
-29
lines changed

2 files changed

+67
-29
lines changed

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ export class IgxChildGridRowComponent implements AfterViewInit, OnInit {
112112

113113
public set data(value: any) {
114114
this._data = value;
115-
if (this.hGrid) {
116-
this.hGrid.data = this._data.childGridsData[this.layout.key];
115+
if (this.hGrid && !this.hGrid.dataSetByUser) {
116+
this.hGrid.setDataInternal(this._data.childGridsData[this.layout.key]);
117117
}
118118
}
119-
119+
120120
/**
121121
* The index of the row.
122122
*
@@ -196,7 +196,7 @@ export class IgxChildGridRowComponent implements AfterViewInit, OnInit {
196196
public ngOnInit() {
197197
const ref = this.container.createComponent(IgxHierarchicalGridComponent, { injector: this.container.injector });
198198
this.hGrid = ref.instance;
199-
this.hGrid.data = this.data.childGridsData[this.layout.key];
199+
this.hGrid.setDataInternal(this.data.childGridsData[this.layout.key]);
200200
this.layout.layoutChange.subscribe((ch) => {
201201
this._handleLayoutChanges(ch);
202202
});
@@ -387,6 +387,9 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
387387
*/
388388
public childLayoutKeys = [];
389389

390+
/** @hidden @internal */
391+
public dataSetByUser = false;
392+
390393
/**
391394
* @hidden
392395
*/
@@ -444,20 +447,8 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
444447
*/
445448
@Input()
446449
public set data(value: any[] | null) {
447-
this._data = value || [];
448-
this.summaryService.clearSummaryCache();
449-
if (!this._init) {
450-
this.validation.updateAll(this._data);
451-
}
452-
if (this.shouldGenerate) {
453-
this.setupColumns();
454-
this.reflow();
455-
}
456-
this.cdr.markForCheck();
457-
if (this.parent && (this.height === null || this.height.indexOf('%') !== -1)) {
458-
// If the height will change based on how much data there is, recalculate sizes in igxForOf.
459-
this.notifyChanges(true);
460-
}
450+
this.setDataInternal(value);
451+
this.dataSetByUser = true;
461452
}
462453

463454
/**
@@ -798,6 +789,24 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
798789
return super.pinRow(rowID, index, row);
799790
}
800791

792+
/** @hidden @internal */
793+
public setDataInternal(value: any) {
794+
this._data = value || [];
795+
this.summaryService.clearSummaryCache();
796+
if (!this._init) {
797+
this.validation.updateAll(this._data);
798+
}
799+
if (this.shouldGenerate) {
800+
this.setupColumns();
801+
this.reflow();
802+
}
803+
this.cdr.markForCheck();
804+
if (this.parent && (this.height === null || this.height.indexOf('%') !== -1)) {
805+
// If the height will change based on how much data there is, recalculate sizes in igxForOf.
806+
this.notifyChanges(true);
807+
}
808+
}
809+
801810
public override unpinRow(rowID: any): boolean {
802811
const row = this.getRowByKey(rowID);
803812
return super.unpinRow(rowID, row);

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

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -464,21 +464,24 @@ describe('Basic IgxHierarchicalGrid #hGrid', () => {
464464
UIInteractions.simulateClickAndSelectEvent(row.expander);
465465
fixture.detectChanges();
466466

467-
let childGrids = fixture.debugElement.queryAll(By.css('igx-child-grid-row'));
468-
let childGrid = childGrids[0].query(By.css('igx-hierarchical-grid')).componentInstance;
467+
// check by adding multiple rows
468+
for (let i = 0; i < 3; i++) {
469+
let childGrids = fixture.debugElement.queryAll(By.css('igx-child-grid-row'));
470+
let childGrid = childGrids[0].query(By.css('igx-hierarchical-grid')).componentInstance;
469471

470-
fixture.componentInstance.data[0].childData = [...hierarchicalGrid.data[0].childData ?? [], { ID: 10, ProductName: 'New child' }];
471-
fixture.componentInstance.data = [...fixture.componentInstance.data];
472-
fixture.detectChanges();
472+
fixture.componentInstance.data[0].childData = [...hierarchicalGrid.data[0].childData ?? [], { ID: i * 10, ProductName: 'New child' + i.toString() }];
473+
fixture.componentInstance.data = [...fixture.componentInstance.data];
474+
fixture.detectChanges();
473475

474-
childGrids = fixture.debugElement.queryAll(By.css('igx-child-grid-row'));
475-
childGrid = childGrids[0].query(By.css('igx-hierarchical-grid')).componentInstance;
476+
childGrids = fixture.debugElement.queryAll(By.css('igx-child-grid-row'));
477+
childGrid = childGrids[0].query(By.css('igx-hierarchical-grid')).componentInstance;
476478

477-
const length = fixture.componentInstance.data[0].childData.length;
478-
const newRow = childGrid.gridAPI.get_row_by_index(length - 1) as IgxHierarchicalRowComponent;
479+
const length = fixture.componentInstance.data[0].childData.length;
480+
const newRow = childGrid.gridAPI.get_row_by_index(length - 1) as IgxHierarchicalRowComponent;
479481

480-
expect(newRow).not.toBeUndefined();
481-
expect(childGrid.data).toBe(fixture.componentInstance.data[0].childData);
482+
expect(newRow).not.toBeUndefined();
483+
expect(childGrid.data).toBe(fixture.componentInstance.data[0].childData);
484+
}
482485
});
483486

484487
it('when child width is in percents its width should be update if parent width changes while parent row is collapsed. ', async () => {
@@ -1182,6 +1185,32 @@ describe('Basic IgxHierarchicalGrid #hGrid', () => {
11821185
expect(iconTxt).toBe('unfold_less');
11831186
expect(icon.getActive).toBe(false);
11841187
});
1188+
1189+
it('should keep already expanded child grids\' data when expanding subsequent ones', fakeAsync(() => {
1190+
const hierarchicalGrid = fixture.componentInstance.instance;
1191+
1192+
fixture.componentInstance.databind();
1193+
fixture.detectChanges();
1194+
1195+
const row0 = hierarchicalGrid.gridAPI.get_row_by_index(0) as IgxHierarchicalRowComponent;
1196+
UIInteractions.simulateClickAndSelectEvent(row0.expander);
1197+
fixture.detectChanges();
1198+
tick();
1199+
1200+
let childGrids = hierarchicalGrid.gridAPI.getChildGrids();
1201+
expect(childGrids.length).toBe(1);
1202+
expect(childGrids[0].data.length).toBeGreaterThan(0);
1203+
1204+
const row1 = hierarchicalGrid.gridAPI.get_row_by_index(2) as IgxHierarchicalRowComponent;
1205+
UIInteractions.simulateClickAndSelectEvent(row1.expander);
1206+
fixture.detectChanges();
1207+
tick();
1208+
1209+
childGrids = hierarchicalGrid.gridAPI.getChildGrids();
1210+
expect(childGrids.length).toBe(2);
1211+
expect(childGrids[0].data.length).toBeGreaterThan(0);
1212+
expect(childGrids[1].data.length).toBeGreaterThan(0);
1213+
}));
11851214
});
11861215

11871216
describe('IgxHierarchicalGrid Template Changing Scenarios #hGrid', () => {

0 commit comments

Comments
 (0)