Example using UrlAdaptor with self-referenced data

Hi, could you provide me an example of TreeGrid using UrlAdaptor with self-referenced data?

Been trying to use this from the documentation but our data keeps duplicating the nodes each time we open them. Like this:



An example could be realy helpful.

Thank you.

5 Replies 1 reply marked as answer

FS Farveen Sulthana Thameeztheen Basha Syncfusion Team January 11, 2021 12:26 PM UTC

Hi Rodrigo, 

Thanks for contacting Syncfusion Support. 

Query#:- Been trying to use this from the documentation but our data keeps duplicating the nodes each time we open them

We suspect that the reported problem occurs when we return entire data on initial rendering without filtering the child records. On expanding, the child seems to get returned again along with parent. In such case the problem will occur. Also we regret for the inconvenience. In our URL Adaptor documentation we doesn’t included the code about filtering the child records. We will correct the documentation and let you know once it has been refreshed. 

Until then, we have prepared sample using url Adaptor with Self-reference. So please ensure to filter the child records using PerformFiltering as like provided code below:- 

Index.html 
@(Html.EJS().TreeGrid("TreeGrid") 
     .DataSource(dataManager => { dataManager.Url("/TreeGrid/UrlDatasource").Adaptor("UrlAdaptor"); }) 
       .AllowPaging().PageSettings(page => page.PageSize(12)) 
      .HasChildMapping("isParent").IdMapping("TaskID").ParentIdMapping("ParentItem") 
                        .Columns(col =>{ 
                                  col.Field("TaskID").HeaderText("Task ID").IsPrimaryKey(true).Width(110.Add(); 
                                                  .   .    .                             
 
}).TreeColumnIndex(1).Render()) 

Controller:- 

public ActionResult UrlDatasource(DataManagerRequest dm) 
        { 
            if (TreeData.tree.Count == 0) 
                TreeData.GetTree(); 
            IEnumerable DataSource = TreeData.tree; 
            DataOperations operation = new DataOperations(); 
            if (dm.Where != null && dm.Where.Count > 0) 
            { 
                DataSource = operation.PerformFiltering(DataSource, dm.Where, "and"); 
            } 
            if (dm.Sorted != null && dm.Sorted.Count > 0) 
            { 
                DataSource = operation.PerformSorting(DataSource, dm.Sorted); 
            } 
            var count = DataSource.ToList<TreeData>().Count(); 
            if (dm.Skip != 0) 
            { 
                DataSource = operation.PerformSkip(DataSource, dm.Skip);   //Paging 
            } 
            if (dm.Take != 0) 
            { 
                DataSource = operation.PerformTake(DataSource, dm.Take); 
            } 
 
            return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource); 
        } 



Refer to the screenshot:- 
 

After expanding childNodes:- 
 


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

Regards, 
Farveen sulthana T 


Marked as answer

RO Rodrigo January 12, 2021 07:02 PM UTC

Thank you very much. Works Perfectly!

Prior to your response, we had to improvise to something like this:

if (dataManager.Where == null)
     DataSource = db.vw_proyectos_partidas.Where(x => x.FK_project == 14).ToList();
else
     if (dataManager.Where[0].value == null)
          DataSource = db.vw_proyectos_partidas.Where(x => x.FK_project == 14 && x.FK_parent == null).ToList();
     else
     {
          int FK_parent = (int)dataManager.Where[0].value;
          DataSource = db.vw_proyectos_partidas.Where(x => x.FK_parent == FK_parent).ToList();
     }

Your example works so much better! Much appreciated!

One more question: is it possible to expand all nodes on intial loading, while using UrlAdapter? or expand all nodes to a certain depth level, for example: all nodes on level 2?

Thank you.





FS Farveen Sulthana Thameeztheen Basha Syncfusion Team January 13, 2021 04:21 PM UTC

Hi Rodrigo, 

Thanks for your update. 

Query#:- is it possible to expand all nodes on intial loading, while using UrlAdapter? 
 
By default TreeGrid parent rows has been rendered with collapsed state. In order to expand all rows while using remote Data, we have introduced an API named “loadOnDemandChild” and along with that we need to handle the childRecords on server end. So that records has been displayed in expanded state. 

Refer to the code example:- 
  @(Html.EJS().TreeGrid("TreeGrid") 
                       .DataSource(dataManager => { dataManager.Url("/Home/UrlDatasource").Adaptor("UrlAdaptor"); }) 
                        .AllowPaging() 
                        .LoadChildOnDemand(true) 
                        .HasChildMapping("isParent").IdMapping("TaskID").ParentIdMapping("ParentItem") 
                        .Columns(col =>{ 
                                 col.Field("TaskID").HeaderText("Task ID").IsPrimaryKey(true).Width(110).Add(); 
 
                     .     .   . 
       }).TreeColumnIndex(1).Render()) 


Serverside:- 
          public ActionResult UrlDatasource(DataManagerRequest dm) 
        { 
            if (TreeData.tree.Count == 0) 
                TreeData.GetTree(); 
            IEnumerable DataSource = TreeData.tree; 
 
            DataOperations operation = new DataOperations(); 
            if (dm.Where != null && dm.Where.Count > 0) 
            { 
                DataSource = operation.PerformFiltering(DataSource, dm.Where, "and"); //perform Filtering  
            } 
            if (dm.Sorted != null && dm.Sorted.Count > 0) 
            { 
                DataSource = operation.PerformSorting(DataSource, dm.Sorted); 
            } 
            var count = DataSource.ToList<TreeData>().Count(); 
            if (dm.Skip != 0) 
            { 
                DataSource = operation.PerformSkip(DataSource, dm.Skip);   //Paging 
            } 
            if (dm.Take != 0) 
            { 
                DataSource = operation.PerformTake(DataSource, dm.Take); 
            } 
            if (dm.Where != null) 
            { 
                DataSource = CollectChildRecords(DataSource, dm);  //method to collect child records  
 
            } 
 
           return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource); 
        } 
 
        public IEnumerable CollectChildRecords(IEnumerable datasource, DataManagerRequest dm) 
        { 
            DataOperations operation = new DataOperations(); 
            IEnumerable DataSource = TreeData.tree;    //use total dataSource here 
            string IdMapping = "TaskID"//define your IdMapping fieldname here 
 
            int[] TaskIds = new int[0]; 
            foreach (var rec in datasource) 
            { 
                int taskid = (int)rec.GetType().GetProperty(IdMapping).GetValue(rec); 
                TaskIds = TaskIds.Concat(new int[] { taskid }).ToArray(); 
            } 
            IEnumerable ChildRecords = null; 
            foreach (int id in TaskIds) 
            { 
                dm.Where[0].value = id; 
                IEnumerable records = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator); 
                ChildRecords = ChildRecords == null || (ChildRecords.AsQueryable().Count() == 0) ? records : ((IEnumerable<object>)ChildRecords).Concat((IEnumerable<object>)records); 
            } 
            if (ChildRecords != null) 
            { 
                ChildRecords = CollectChildRecords(ChildRecords, dm); 
                if (dm.Sorted != null && dm.Sorted.Count > 0) // perform Sorting 
                { 
                    ChildRecords = operation.PerformSorting(ChildRecords, dm.Sorted); 
                } 
                datasource = ((IEnumerable<object>)datasource).Concat((IEnumerable<object>)ChildRecords); 
            } 
            return datasource; 
        } 


Screenshot:- 
 


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

Regards, 
Farveen sulthana T 



RO Rodrigo January 15, 2021 04:37 AM UTC

Thank you so much. It works great!

Regards.


FS Farveen Sulthana Thameeztheen Basha Syncfusion Team January 18, 2021 04:21 AM UTC

Hi Rodrigo, 

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