Skip to content

Commit 562ffdc

Browse files
committed
feat(tree-grid): adding option to expand/collapse #2530
1 parent 06f6c11 commit 562ffdc

File tree

5 files changed

+81
-15
lines changed

5 files changed

+81
-15
lines changed

Diff for: projects/igniteui-angular/src/lib/tree-grid/tree-cell.component.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,38 @@ import { IgxTreeGridRowComponent } from './tree-grid-row.component';
1111
})
1212
export class IgxTreeGridCellComponent extends IgxGridCellComponent {
1313
public get indentation() {
14-
const flatRowComponent = <IgxTreeGridRowComponent>this.row;
15-
return flatRowComponent.flatRow.indentationLevel;
14+
return this.row.flatRow.indentationLevel;
1615
}
1716

1817
public get hasChildren() {
19-
const flatRowComponent = <IgxTreeGridRowComponent>this.row;
20-
return flatRowComponent.flatRow.hasChildren;
18+
return this.row.flatRow.hasChildren;
2119
}
2220

2321
@HostBinding('attr.aria-expanded')
2422
get expanded(): boolean {
25-
return true;
23+
const states = (<IgxTreeGridComponent>this.grid).expandedStates;
24+
const rowID = this.row.rowID;
25+
const expanded = states.get(rowID);
26+
27+
if (expanded !== undefined) {
28+
return expanded;
29+
} else {
30+
return this.getDefaultExpandedState();
31+
}
32+
}
33+
34+
private getDefaultExpandedState(): boolean {
35+
const indentationLevel = this.indentation;
36+
const expandedLevels = (<IgxTreeGridComponent>this.grid).expandedLevels;
37+
38+
return expandedLevels < 0 || (expandedLevels >= 0 && indentationLevel < expandedLevels);
2639
}
2740

2841
public toggle() {
42+
const states = (<IgxTreeGridComponent>this.grid).expandedStates;
43+
const rowID = this.row.rowID;
44+
states.set(rowID, !this.expanded);
45+
46+
(<IgxTreeGridComponent>this.grid).expandedStates = states;
2947
}
3048
}

Diff for: projects/igniteui-angular/src/lib/tree-grid/tree-grid.component.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
<div class="igx-grid__tbody" role="rowgroup" [style.height.px]='calcHeight' [style.width.px]='calcWidth' #tbody (scroll)='scrollHandler($event)'>
4444
<span *ngIf="hasMovableColumns && draggedColumn && pinnedColumns.length <= 0" [igxColumnMovingDrop]="parentVirtDir" [attr.droppable]="true" id="left" class="igx-grid__scroll-on-drag-left"></span>
4545
<span *ngIf="hasMovableColumns && draggedColumn && pinnedColumns.length > 0" [igxColumnMovingDrop]="parentVirtDir" [attr.droppable]="true" id="left" class="igx-grid__scroll-on-drag-pinned" [style.left.px]="pinnedWidth"></span>
46-
<ng-template igxFor let-rowData [igxForOf]="data | treeGridHierarchizing:childDataKey:id:pipeTrigger | treeGridFlattening:id:pipeTrigger"
46+
<ng-template igxFor let-rowData [igxForOf]="data | treeGridHierarchizing:childDataKey:id:pipeTrigger |
47+
treeGridFlattening:id:expandedLevels:expandedStates:pipeTrigger"
4748
let-rowIndex="index" [igxForScrollOrientation]="'vertical'"
4849
[igxForContainerSize]='calcHeight' [igxForItemSize]="rowHeight" #verticalScrollContainer (onChunkPreload)="dataLoading($event)">
4950
<ng-template #record_template>

Diff for: projects/igniteui-angular/src/lib/tree-grid/tree-grid.component.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,29 @@ export class IgxTreeGridComponent extends IgxGridBaseComponent {
8181
@Input()
8282
public childDataKey;
8383

84+
/**
85+
* An @Input property that sets the child data key of the `IgxTreeGridComponent`.
86+
* ```html
87+
* <igx-tree-grid #grid [data]="localData" [showToolbar]="true" [childDataKey]="employees" [autoGenerate]="true"></iigx-tree-grid>
88+
* ```
89+
* @memberof IgxTreeGridRowComponent
90+
*/
91+
@Input()
92+
public expandedLevels = -1;
93+
94+
private _expandedStates: Map<any, boolean> = new Map<any, boolean>();
95+
96+
@Input()
97+
public get expandedStates() {
98+
return this._expandedStates;
99+
}
100+
101+
public set expandedStates(value) {
102+
this._expandedStates = this.cloneMap(value);
103+
104+
this.cdr.detectChanges();
105+
}
106+
84107
private gridAPI: IgxTreeGridAPIService;
85108

86109
constructor(
@@ -95,7 +118,20 @@ export class IgxTreeGridComponent extends IgxGridBaseComponent {
95118
this.gridAPI = <IgxTreeGridAPIService>gridAPI;
96119
}
97120

98-
/**
121+
122+
private cloneMap(mapIn: Map<any, boolean>): Map<any, boolean> {
123+
const mapCloned: Map<any, boolean> = new Map<any, boolean>();
124+
125+
mapIn.forEach((value: boolean, key: any, mapObj: Map<any, boolean>) => {
126+
127+
mapCloned.set(key, value);
128+
});
129+
130+
return mapCloned;
131+
}
132+
133+
134+
/**
99135
* @hidden
100136
*/
101137
public getContext(rowData): any {

Diff for: projects/igniteui-angular/src/lib/tree-grid/tree-grid.pipes.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,20 @@ export class IgxTreeGridFlatteningPipe implements PipeTransform {
7474
this.gridAPI = <IgxTreeGridAPIService>gridAPI;
7575
}
7676

77-
public transform(collection: IHierarchizedResult,
78-
id: string, pipeTrigger: number): any[] {
77+
public transform(collection: IHierarchizedResult, id: string,
78+
expandedLevels: number, expandedStates: Map<any, boolean>, pipeTrigger: number): any[] {
7979

8080
const grid: IgxTreeGridComponent = this.gridAPI.get(id);
81-
81+
const primaryKey = grid.primaryKey;
8282
const data: IFlattenedRecord[] = [];
8383

84-
this.getFlatDataRecusrive(collection.data, data, 0);
84+
this.getFlatDataRecusrive(collection.data, data, expandedLevels, expandedStates, primaryKey, 0);
8585

8686
return data;
8787
}
8888

89-
private getFlatDataRecusrive(collection: IHierarchicalRecord[], data: IFlattenedRecord[] = [], indentationLevel: number) {
90-
89+
private getFlatDataRecusrive(collection: IHierarchicalRecord[], data: IFlattenedRecord[] = [],
90+
expandedLevels: number, expandedStates: Map<any, boolean>, primaryKey: any, indentationLevel: number) {
9191
if (!collection || !collection.length) {
9292
return;
9393
}
@@ -100,7 +100,18 @@ export class IgxTreeGridFlatteningPipe implements PipeTransform {
100100
hasChildren: hirarchicalRecord.children && hirarchicalRecord.children.length > 0
101101
};
102102
data.push(flatRecord);
103-
this.getFlatDataRecusrive(hirarchicalRecord.children, data, indentationLevel + 1);
103+
104+
const rowID = primaryKey ? flatRecord.data[primaryKey] : flatRecord.data;
105+
106+
let isExpanded = expandedStates.get(rowID);
107+
if (isExpanded === undefined) {
108+
isExpanded = expandedLevels < 0 || (expandedLevels >= 0 && indentationLevel < expandedLevels);
109+
}
110+
111+
if (isExpanded) {
112+
this.getFlatDataRecusrive(hirarchicalRecord.children, data, expandedLevels,
113+
expandedStates, primaryKey, indentationLevel + 1);
114+
}
104115
}
105116
}
106117
}

Diff for: src/app/tree-grid/tree-grid.sample.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<div class="sample-content">
88
<div class="sample-column">
9-
<igx-tree-grid #grid1 [data]="data" childDataKey="ChildCompanies" [rowSelectable]="true" [paging]="false" [width]="'900px'" [height]="'500px'">
9+
<igx-tree-grid #grid1 [data]="data" childDataKey="ChildCompanies" expandedLevels="1" [rowSelectable]="true" [paging]="false" [width]="'900px'" [height]="'500px'">
1010
<igx-column *ngFor="let c of columns" [field]="c.field"
1111
[header]="c.field"
1212
[movable]="c.movable"

0 commit comments

Comments
 (0)