Skip to content

Commit 22d0229

Browse files
Merge pull request #13888 from IgniteUI/bpachilova/fix-13881-17.0.x
fix(hierarchical-grid): do not use hierarchy pipe data when data is explicitly set by user - 17.0.x
2 parents ca67a5f + aa03fda commit 22d0229

File tree

2 files changed

+66
-28
lines changed

2 files changed

+66
-28
lines changed

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ export class IgxChildGridRowComponent implements AfterViewInit, OnInit {
113113

114114
public set data(value: any) {
115115
this._data = value;
116-
if (this.hGrid) {
117-
this.hGrid.data = this._data.childGridsData[this.layout.key];
116+
if (this.hGrid && !this.hGrid.dataSetByUser) {
117+
this.hGrid.setDataInternal(this._data.childGridsData[this.layout.key]);
118118
}
119119
}
120120

@@ -197,7 +197,7 @@ export class IgxChildGridRowComponent implements AfterViewInit, OnInit {
197197
public ngOnInit() {
198198
const ref = this.container.createComponent(IgxHierarchicalGridComponent, { injector: this.container.injector });
199199
this.hGrid = ref.instance;
200-
this.hGrid.data = this.data.childGridsData[this.layout.key];
200+
this.hGrid.setDataInternal(this.data.childGridsData[this.layout.key]);
201201
this.layout.layoutChange.subscribe((ch) => {
202202
this._handleLayoutChanges(ch);
203203
});
@@ -388,6 +388,9 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
388388
*/
389389
public childLayoutKeys = [];
390390

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

464455
/**
@@ -799,6 +790,24 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
799790
return super.pinRow(rowID, index, row);
800791
}
801792

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