Skip to content

Commit b30a517

Browse files
committed
fix(hgrid): Block child expansion based on hasChildrenKey prop #6816
1 parent 2f428f2 commit b30a517

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

projects/igniteui-angular/src/lib/grids/grid-base.directive.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3007,13 +3007,18 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
30073007
}
30083008

30093009
public set expansionStates(value) {
3010+
value = this.filterExpansionStates(value);
30103011
this._expansionStates = new Map<any, boolean>(value);
30113012
this.expansionStatesChange.emit(this._expansionStates);
30123013
if (this.gridAPI.grid) {
30133014
this.cdr.detectChanges();
30143015
}
30153016
}
30163017

3018+
protected filterExpansionStates(value) {
3019+
return value;
3020+
}
3021+
30173022
/**
30183023
* Expands all rows.
30193024
* @example

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,19 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
414414
}
415415
}
416416

417+
protected filterExpansionStates(expStates: Map<any, boolean>) {
418+
const results = new Map<any, boolean>(expStates);
419+
if (this.hasChildrenKey) {
420+
expStates.forEach((value, item) => {
421+
const rec = this.primaryKey ? this.data.find(x => x[this.primaryKey] === item) : item;
422+
if (!rec[this.hasChildrenKey]) {
423+
results.delete(item);
424+
}
425+
});
426+
}
427+
return results;
428+
}
429+
417430
protected onColumnsChanged(change: QueryList<IgxColumnComponent>) {
418431
this.updateColumnList();
419432
const cols = change.filter(c => c.gridAPI.grid === this);

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,43 @@ describe('Basic IgxHierarchicalGrid #hGrid', () => {
416416

417417
expect(childGrid.calcWidth - 370 ).toBeLessThan(3);
418418
});
419+
420+
it('should not expand children when hasChildrenKey is false for the row', () => {
421+
hierarchicalGrid.hasChildrenKey = 'hasChild';
422+
fixture.componentInstance.data = [
423+
{ID: 1, ProductName: 'Product: A1', hasChild: false, childData: fixture.componentInstance.generateDataUneven(1, 1)},
424+
{ID: 2, ProductName: 'Product: A2', hasChild: true, childData: fixture.componentInstance.generateDataUneven(1, 1)}
425+
];
426+
fixture.detectChanges();
427+
const row1 = hierarchicalGrid.getRowByIndex(0) as IgxHierarchicalRowComponent;
428+
const rowElems = fixture.debugElement.queryAll(By.directive(IgxHierarchicalRowComponent));
429+
expect(rowElems[0].query(By.css('igx-icon')).nativeElement.innerText).toEqual('');
430+
const row2 = hierarchicalGrid.getRowByIndex(1) as IgxHierarchicalRowComponent;
431+
expect(rowElems[1].query(By.css('igx-icon')).nativeElement.innerText).toEqual('chevron_right');
432+
hierarchicalGrid.expandRow(row1.rowData);
433+
hierarchicalGrid.expandRow(row2.rowData);
434+
expect(row1.expanded).toBe(false);
435+
expect(row2.expanded).toBe(true);
436+
});
437+
438+
it('should not expand children when hasChildrenKey is false for the row and there is primaryKey', () => {
439+
hierarchicalGrid.hasChildrenKey = 'hasChild';
440+
hierarchicalGrid.primaryKey = 'ID';
441+
fixture.componentInstance.data = [
442+
{ID: 1, ProductName: 'Product: A1', hasChild: false, childData: fixture.componentInstance.generateDataUneven(1, 1)},
443+
{ID: 2, ProductName: 'Product: A2', hasChild: true, childData: fixture.componentInstance.generateDataUneven(1, 1)}
444+
];
445+
fixture.detectChanges();
446+
const row1 = hierarchicalGrid.getRowByIndex(0) as IgxHierarchicalRowComponent;
447+
const rowElems = fixture.debugElement.queryAll(By.directive(IgxHierarchicalRowComponent));
448+
expect(rowElems[0].query(By.css('igx-icon')).nativeElement.innerText).toEqual('');
449+
const row2 = hierarchicalGrid.getRowByIndex(1) as IgxHierarchicalRowComponent;
450+
expect(rowElems[1].query(By.css('igx-icon')).nativeElement.innerText).toEqual('chevron_right');
451+
hierarchicalGrid.expandRow(1);
452+
hierarchicalGrid.expandRow(2);
453+
expect(row1.expanded).toBe(false);
454+
expect(row2.expanded).toBe(true);
455+
});
419456
});
420457

421458
describe('IgxHierarchicalGrid Row Islands #hGrid', () => {

0 commit comments

Comments
 (0)