Articles in this section
Category / Section

How to maintain expand/collapsed state of rows during page refresh in Angular TreeGrid?

2 mins read

This article explains how to maintain expand/collapse state of rows during page refresh in Angular Treegrid

You can maintain expand/collapse state of rows during page refresh using dataBound event.

  1. You can save the collapsed records to localStorage in collapsed event of Tree Grid by using the method setItem method.
  2. On page refresh, dataBound event will be triggered. In that event, retrieve the saved records by using the method getItem method and collapse the specific rows by using the collapseRow method of Tree Grid by passing the row detail.

 

App.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { sampleData } from './jsontreegriddata';
import {
  TreeGridComponent,
  PageService,
} from '@syncfusion/ej2-angular-treegrid';
 
@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  providers: [PageService],
})
export class AppComponent {
  public data: Object[] = [];
  @ViewChild('treegrid')
  public treegrid: TreeGridComponent;
  public currentData = [];
  ngOnInit(): void {
    this.data = sampleData;
  }
 
  dataBound(args: any): void {
    if (this.treegrid.initialRender) {
      //checking whether it is initial rendering
      var data = JSON.parse(window.localStorage.getItem('currentData'));
      //retriving collapsed record in local storage using getItem method
      if (data) {
        var completeData = this.treegrid.grid.dataSource;
        var primaryKeyFieldName = this.treegrid.getPrimaryKeyFieldNames()[0];
        for (var i = 0; i < data.length; i++) {
          var value;
          for (var j = 0; j < completeData.length; j++) {
            if (completeData[j][primaryKeyFieldName] == data[i][primaryKeyFieldName]) {
              value = completeData[j];
            }
          }
          this.treegrid.collapseRow(null, value); //collapsing row using collapseRow 
        }
        this.currentData = [];
        window.localStorage.setItem('currentData', JSON.stringify(this.currentData));
      }
    }
  }
 
  collapsed(args: any): void {
    this.currentData.push(args.data);
    this.store(args);
  }
 
  expanded(args: any): void {
    this.currentData.push(args.data);
    this.store(args);
  }
 
  store(args: any): void {
    this.currentData.push(...this.treegrid.getCurrentViewRecords().filter((i) => 
    i.hasChildRecords).filter((i) => !i.expanded));
    this.currentData = Array.from(new Set(this.currentData));
    window.localStorage.setItem('currentData', JSON.stringify(this.currentData));
  }
}

App.component.html

<div class="control-section">
  <ejs-treegrid
    #treegrid
    [dataSource]="data"
    allowPaging="true"
    childMapping="subtasks"
    height="350"
    [treeColumnIndex]="1"
    (dataBound)="dataBound($event)"
    (collapsed)="collapsed($event)"
    (expanded)="expanded($event)"
  >
    <e-columns>
      <e-column
        field="taskID"
        headerText="Task ID"
        isPrimaryKey="true"
        width="70"
        textAlign="Right"
      ></e-column>
      <e-column field="taskName" headerText="Task Name" width="200"></e-column>
      <e-column
        field="startDate"
        headerText="Start Date"
        width="90"
        format="yMd"
        textAlign="Right"
      ></e-column>
      <e-column
        field="endDate"
        headerText="End Date"
        width="90"
        format="yMd"
        textAlign="Right"
      ></e-column>
      <e-column
        field="duration"
        headerText="Duration"
        width="80"
        textAlign="Right"
      ></e-column>
      <e-column
        field="progress"
        headerText="Progress"
        width="80"
        textAlign="Right"
      ></e-column>
      <e-column field="priority" headerText="Priority" width="90"></e-column>
    </e-columns>
  </ejs-treegrid>
</div>

 

maintain collapse state of rows during page refresh in angular

 

View Sample in Stackblitz

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied