Articles in this section
Category / Section

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

2 mins read

This article explains how to maintain expand/collapse state of rows during page refresh in Javascript 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.

 

Index.js

var currentData = []
var treeGridObj = new ej.treegrid.TreeGrid({
dataSource: sampleData,
childMapping: 'subtasks',
height: 350,
treeColumnIndex: 1,
allowPaging: true,
dataBound: dataBound,
collapsed: collapsed,
expanded: expanded,
columns: [
   {
    field: 'taskID',
    isPrimaryKey: true,
    headerText: 'Task ID',
    width: 70,
    textAlign: 'Right',
   },
   {
    field: 'taskName',
    headerText: 'Task Name',
    width: 200,
    textAlign: 'Left',
   },
   {
    field: 'startDate',
    headerText: 'Start Date',
    width: 90,
    textAlign: 'Right',
    type: 'date',
    format: 'yMd',
   },
   {
    field: 'endDate',
    headerText: 'End Date',
    width: 90,
    textAlign: 'Right',
    type: 'date',
    format: 'yMd',
   },
   {
    field: 'duration',
    headerText: 'Duration',
    width: 80,
    textAlign: 'Right',
   },
   {
    field: 'progress',
    headerText: 'Progress',
    width: 80,
    textAlign: 'Right',
   },
   { 
    field: 'priority', headerText: 'Priority', width: 90 },
   ],
  });
  treeGridObj.appendTo('#TreeGrid');
 
  function collapsed(args) {
    currentData.push(args.data);
    store(args);
    //storing collapsed record in local storage using setItem method
  }
  function expanded(args) {
    currentData.push(args.data);
    this.store(args);
    //storing collapsed record in local storage using setItem method
  }
  function store(args) {
    currentData.push(...treeGridObj.getCurrentViewRecords().
    filter((i) =>    i.hasChildRecords).filter((i) => !i.expanded));
    this.currentData = Array.from(new Set(this.currentData));
    window.localStorage.setItem('currentData', JSON.stringify(currentData));
  }
  function dataBound(args) {
    if (treeGridObj.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 = treeGridObj.grid.dataSource;
        var primaryKeyFieldName = treeGridObj.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];
            }
          }
          treeGridObj.collapseRow(null, value); //collapsing row using collapseRow           
        }
        currentData = [];
        window.localStorage.setItem('currentData', JSON.stringify(currentData));
      }
    }
  }

 

maintain collapse state of rows during page refresh in javascript

 

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