Articles in this section

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

This article explains how to maintain the expand/collapse state of rows during a page refresh in Vue TreeGrid.

You can maintain expand/collapse state of rows during a page refresh using the 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.vue

<template>
<div id="app">
         <ejs-treegrid ref='treegrid' :dataSource='data' childMapping='subtasks' 
         :treeColumnIndex='1' :height='380' :editSettings='editSettings' 
         :dataBound='dataBound' :expanded='expanded' :collapsed='collapsed'>
            <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>
</template>
<script>
import Vue from "vue";
import { TreeGridPlugin, Page, Edit } from "@syncfusion/ej2-vue-treegrid";
import { sampleData } from "./datasource.js";
Vue.use(TreeGridPlugin);
 
export default {
  data ()  {
    return {
      currentData : [],
      data: sampleData,
      pageSettings: { pageSize: 7 },
      editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, mode:"Row" },
    };
  },
  provide: {
      treegrid: [ Page, Edit ]
  },
  methods:{
    dataBound:function (args) {
        // if (this.$refs.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.$refs.treegrid.ej2Instances.grid.dataSource;
        var primaryKeyFieldName = this.$refs.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.$refs.treegrid.collapseRow(null, value); //collapsing row 
          using collapseRow method
        }
        this.currentData = [];
        window.localStorage.setItem('currentData',JSON.stringify(this.currentData));
      }
    },
    
    expanded:function (args) {
      this.currentData.push(args.data);
      this.store(args);
    },
    collapsed:function (args) {
      this.currentData.push(args.data);
      this.store(args);
    },
    store:function (args) {
       this.currentData.push(...this.$refs.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));
     }
   }
  }
</script>

 

maintain collapse state of rows during page refresh in vue

 

Sample: View Sample in GitHub


Conclusion

We hope you enjoyed learning how to maintain expand/collapsed state of rows during page refresh in Vue TreeGrid.

You can refer to our Vue Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Vue Diagram example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsBoldDesk Support, or feedback portal. We are always happy to assist you!

 

 

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