|
App.Component.html:-
<ejs-treegrid #treegridObj [dataSource]='data' idMapping='TaskID' parentIdMapping='parentID' hasChildMapping='isParent' [treeColumnIndex]='1' loadChildOnDemand='true' [height]='280' (beforeDataBound)="dataBound($event)" (actionBegin)="actionBegin($event)">
<e-columns>
<e-column field='TaskID' headerText='Task ID' width='70' textAlign='Right'></e-column>
. . .
</e-columns>
</ejs-treegrid>
App.Component.ts
export class AppComponent {
@ViewChild('treegridObj')
public treegridObj: TreeGridComponent;
public primaryKeyList: any = [];
public data: Object[] = [];
dataBound(args) {
if (!this.initialRender && this.primaryKeyList.length) {
for (var key = 0; key < this.primaryKeyList.length; key++) { //looping saved primary key list
var iD = this.primaryKeyList[key];
var record = args.result.filter((e) => e["TaskID"] === iD); // filtering record of task id which matched saved primary key
if (record && record.length) {
if (record[0].expanded) { // checking if the expanded property is true
record[0].expanded = false; // resetting it to false to maintain expanded state
}
}
}
}
}
actionBegin(args): void {
if (args.requestType == "add") {
var currentViewRecord = this.getCurrentViewRecords();
this.primaryKeyList = [];
for (var i = 0; i < currentViewRecord.length; i++) { //looping current view data to check which records are in collapsed state
if (currentViewRecord[i].isParent && !currentViewRecord[i].expanded) { // if any record is in collapsed state
this.primaryKeyList.push(currentViewRecord[i].TaskID); // saving that record's primary key using which we can maintain state after adding new data
}
}
}
}
} |
|
App.Component.html:-
<ejs-treegrid [dataSource]="data"allowPaging="true" idMapping="TaskID" parentIdMapping="parentID" [treeColumnIndex]="1" expandStateMapping="isExpanded">
<e-columns>
<e-column field="TaskID"
headerText="Task ID"
width="90"
textAlign="Right"></e-column>
. . .
</e-columns>
</ejs-treegrid>
dataSource.ts
export let projectData = [
{
TaskID: 1,
TaskName: "Parent Task 1",
StartDate: new Date("02/23/2017"),
EndDate: new Date("02/27/2017"),
Progress: "40",
isExpanded: false
},
{
TaskID: 2,
TaskName: "Child Task 1",
StartDate: new Date("02/23/2017"),
EndDate: new Date("02/27/2017"),
Progress: "40",
parentID: 1,
isExpanded: false
},
{
TaskID: 3,
TaskName: "Child Task 2",
StartDate: new Date("02/23/2017"),
EndDate: new Date("02/27/2017"),
Progress: "40",
parentID: 1,
isExpanded: false
},
{
TaskID: 4,
TaskName: "Child Task 3",
StartDate: new Date("02/23/2017"),
EndDate: new Date("02/27/2017"),
Duration: 5,
Progress: "40",
parentID: 1,
isExpanded: false
},
{
TaskID: 5,
TaskName: "Parent Task 2",
StartDate: new Date("03/14/2017"),
EndDate: new Date("03/18/2017"),
Progress: "40",
isExpanded: true
},
. . .
];
|