Skip to content

Commit d0fc3e7

Browse files
authored
fix(hgrid): do not use h-pipe data when data is set by user (#13887)
1 parent dfe3884 commit d0fc3e7

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

+26-17
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ export class IgxChildGridRowComponent implements AfterViewInit, OnInit {
110110

111111
public set data(value: any) {
112112
this._data = value;
113-
if (this.hGrid) {
114-
this.hGrid.data = this._data.childGridsData[this.layout.key];
113+
if (this.hGrid && !this.hGrid.dataSetByUser) {
114+
this.hGrid.setDataInternal(this._data.childGridsData[this.layout.key]);
115115
}
116116
}
117117

@@ -194,7 +194,7 @@ export class IgxChildGridRowComponent implements AfterViewInit, OnInit {
194194
public ngOnInit() {
195195
const ref = this.container.createComponent(IgxHierarchicalGridComponent, { injector: this.container.injector });
196196
this.hGrid = ref.instance;
197-
this.hGrid.data = this.data.childGridsData[this.layout.key];
197+
this.hGrid.setDataInternal(this.data.childGridsData[this.layout.key]);
198198
this.layout.layoutChange.subscribe((ch) => {
199199
this._handleLayoutChanges(ch);
200200
});
@@ -385,6 +385,9 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
385385
*/
386386
public childLayoutKeys = [];
387387

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

461452
/**
@@ -796,6 +787,24 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
796787
return super.pinRow(rowID, index, row);
797788
}
798789

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

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

+40-11
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)