keep the level of rows when refresh data

when I refresh data. my treegrid collapse all level, but I want keep pre-level. Can you help me?


8 Replies 1 reply marked as answer

TB Toan Bui June 1, 2021 02:47 PM UTC

Hi, thank for your reply. But it's not my problem. When I add record, i want to refresh data to show change. But treegrid expand all but I want to keep treegrid level 


FS Farveen Sulthana Thameeztheen Basha Syncfusion Team June 1, 2021 02:50 PM UTC

Hi Toan, 
 
Please Ignore the previous update. 

Thanks for your interest in Syncfusion Components. 

Query#:- when I refresh data. my treegrid collapse all level, but I want keep pre-level.  
 
We have checked your query and before proceeding with this query, we need some more additional information in order to provide appropriate solution. So share the following information with us 

  1. Type of data that you have bound to TreeGrid(whether Local or Remote data).
  2. Have you enabled EnableCollapseAll property (for rendering rows in Collapsed state)
  3. TreeGrid code example.
  4. Share Video demo(demonstrating any specific scenario in which you want to keep the level or at TreeGrid Initial render)

Regards, 
Farveen sulthana T 



TB Toan Bui June 2, 2021 02:01 AM UTC

My data is remote data. When I insert new data, this treemap expands all. I don't want it to happen. The video is in the attachment below

Attachment: HaGiang__Google_Chrome_20210602_085317_b6cfc122.rar


FS Farveen Sulthana Thameeztheen Basha Syncfusion Team June 2, 2021 02:59 PM UTC

Hi Toan, 

Query#:- My data is remote data. When I insert new data, this treemap expands all. I don't want it to happen. 
 
From your provided Video demonstration, we conclude that you have used LoadChildOnDemand property to render all rows in Expanded state while using RemoteData.  

After collapsing the certain records, If you are performing Add action within CurrentView data, we can maintain the state of the records with the help of getCurrentViewRecords method. We have achieved this using actionBegin and beforeDataBound event of the TreeGrid. 

  1. On inserting new record actionBegin event of the TreeGrid will get trigger. When requestType as Add, Using getCurrentViewRecords method, we have checked the “expanded” property of each record from current view data. If a record is in collapsed state, it’s expanded property will be “false”. We have collected the “primary key” of the collapsed records in those events.

2.    After performing add action beforeDataBound event will be triggered. In this event, we have updated the “expanded” state of the records(with the help of saved Primary key). Thus collapsed state will be maintained after add action. 

Refer to the code below:- 

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 
                } 
            } 
 
          }   
        } 
    } 


Screenshot:- 
 
 
 
API Link:- 

The above suggested solution is not applicable when we perform Paging action.i.e,( If you perform Collapse action in one Page and add new record in  another Page the collapsed state will not be maintained). While using remote data, on performing page action records has been newly rendered so that we cannot map the records with collapsed state with the saved Primarykey list.  

Please get back to us if you need any further assistance. 

Regards, 
Farveen sulthana T 



TB Toan Bui June 3, 2021 04:11 AM UTC

I want to keep expanded for all my pages. Maybe your solution is not what I'm looking for. However can you tell me how to set expanded for all the data in all my pages?


FS Farveen Sulthana Thameeztheen Basha Syncfusion Team June 3, 2021 02:44 PM UTC

Hi Toan, 

Query#:- However can you tell me how to set expanded for all the data in all my pages? 
 
For rendering the Tree Grid with certain nodes in open state(expanded) and few in collapsed state, we suggest you to use “expandStateMapping” property of Tree Grid. Using expandStateMapping, we can set the expanded state as true or false in dataSource of Tree Grid and render it based on the state in the dataSource. Also, we can maintain the state on performing Insert action with this property. 
 
Refer to the code below:- 
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 
            }, 
            .   .   . 
       ]; 
 
 
Please check the below API help documentation and sample, 
 
Please get back to us if you need any further assistance. 
 
Regards, 
Farveen sulthana T 


Marked as answer

TB Toan Bui June 4, 2021 03:02 AM UTC

I made it through with your help. Thank you very much


FS Farveen Sulthana Thameeztheen Basha Syncfusion Team June 4, 2021 05:23 AM UTC

Hi Toan, 

Thanks for your update. Please get back to us if you need any further assistance. We are happy to assist you. 

Regards, 
Farveen sulthana T 


Loader.
Up arrow icon