How to load data using web service in TreeGrid?

Answer:

We have added the API service call and rendered all the root nodes initially in a collapsed state. In the server end, we handled the separate query to filter out the child records on expanding its parent record by comparing Filter query value with the ParentID as highlighted in the below code snippet.

<div>

<SfTreeGrid @ref="grid" TValue="SelfReferenceData" IdMapping="TaskID" ParentIdMapping="ParentID" HasChildMapping="isParent" TreeColumnIndex="1" AllowPaging="true">

<SfDataManager Url="api/Default" Adaptor="Adaptors.WebApiAdaptor" CrossDomain="true">SfDataManager>

<TreeGridColumns>

…………………. . .

TreeGridColumns>

SfTreeGrid>

div>

Controller page

……………… . .

public class DefaultController : ControllerBase

{

………. . .

();

// GET: api/Default

[HttpGet]

public async Task < object >

Get(int ? code)

{

var queryString = Request.Query;

if (SelfReferenceData.tree.Count == 0)

SelfReferenceData.GetTree();

if (queryString.Keys.Contains("$filter") && !queryString.Keys.Contains("$top")) {

StringValues filter;

queryString.TryGetValue("$filter", out filter);

int fltr = Int32.Parse(filter[0].ToString().Split("eq")[1]); // get the selected parentID for expansion here

IQueryable < SelfReferenceData >

data1 = SelfReferenceData.tree.Where(f => f.ParentID == fltr).AsQueryable();// compare the filter value with parentID to get the child records from server end for the selected parent record

if (queryString.Keys.Contains("$orderby")) {

StringValues srt;

queryString.TryGetValue("$orderby", out srt);

srt = srt.ToString().Replace("desc", "descending");

data1 = SortingExtend.Sort(data1, srt);

}

return new { Items = data1.ToList(), Count = data1.Count() };

}

………………….

data = data.Where(p => p.ParentID == null).ToList();

int count = data.Count;

if (queryString.Keys.Contains("$inlinecount")) {

StringValues Skip;

StringValues Take;

int skip = (queryString.TryGetValue("$skip", out Skip)) ? Convert.ToInt32(Skip[0]) : 0;

int top = (queryString.TryGetValue("$top", out Take)) ? Convert.ToInt32(Take[0]) : data.Count();

return new { Items = data.Skip(skip).Take(top), Count = count };

}

else {

return SelfReferenceData.GetTree();

}

}

}

}


To bind remote data to Tree Grid component, assign data as an instance of SfDataManager to the DataSource property. To interact with remote data source, provide the endpoint url and define the HasChildMapping property of Tree Grid.

Also while using RemoteData we need to bind Self-Referential type of DataBinding(FlatData) to TreeGrid(which only supports remoteData). To bind self-reference data idMapping and parentIdMapping property is necessary to form a parent-child hierarchy.

Tree Grid provides Load on Demand support for rendering remote data. The Load on demand is considered in Tree Grid for the following actions.

• Expanding root nodes.

• Navigating pages, with paging enabled in Tree Grid.

Sample here.

Please refer to the Documentation Link:-

https://blazor.syncfusion.com/documentation/treegrid/data-binding/#remote-service-binding

https://blazor.syncfusion.com/documentation/treegrid/data-binding/#creating-web-api-controller



2 Replies

VI vivek March 1, 2023 05:15 AM UTC

Hi,


I am trying to get this working for my sample project but it is not working.

I am seeing the data returned by APIs in but TreeGrid is not displaying it.


Here is my Grid.



<SfTreeGrid IdMapping="TaskID" TValue="BusinessObject" ParentIdMapping="ParentItem" HasChildMapping="isParent" AllowPaging="true" TreeColumnIndex="1">

    <SfDataManager Url="api/TreeGrid" CrossDomain="true" Adaptor="Syncfusion.Blazor.Adaptors.WebApiAdaptor"></SfDataManager>

    <TreeGridColumns>

        <TreeGridColumn Field="TaskID" HeaderText="Task ID" Width="80" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></TreeGridColumn>

        <TreeGridColumn Field="TaskName" HeaderText="Task Name" Width="160"></TreeGridColumn>

        <TreeGridColumn Field="Duration" HeaderText="Duration" Width="100" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></TreeGridColumn>

        <TreeGridColumn Field="Progress" HeaderText="Progress" Width="100" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></TreeGridColumn>

        <TreeGridColumn Field="Priority" HeaderText="Priority" Width="80"></TreeGridColumn>

    </TreeGridColumns>

</SfTreeGrid>


Here is my controller code.


[HttpGet]

        public ActionResult Get()

        {

            var boList = GetBOs();

            return Ok(new

            {

                Items = boList,

                Count = boList.Count()

            });

        }


        private List<BusinessObject> GetBOs()

        {

   var boList = new List<BusinessObject>();


            boList.Add(new BusinessObject() { TaskID = 1, TaskName = "Parent Task 1", Duration = 10, Progress = "In Progress", Priority = "High", IsParent = true });

            boList.Add(new BusinessObject() { TaskID = 2, TaskName = "Parent task 2", Duration = 4, Progress = "In Progress", Priority = "Normal", IsParent = true });

            boList.Add(new BusinessObject() { TaskID = 3, TaskName = "Parent Task 3", Duration = 5, Progress = "In Progress", Priority = "Critical", IsParent = true });

            boList.Add(new BusinessObject() { TaskID = 4, TaskName = "Parent Task 4", Duration = 6, Progress = "In Progress", Priority = "Low", IsParent = true });

            boList.Add(new BusinessObject() { TaskID = 5, TaskName = "Parent Task 5", Duration = 9, Progress = "In Progress", Priority = "Normal", IsParent = true });


            return boList;

        }






FS Farveen Sulthana Thameeztheen Basha Syncfusion Team March 3, 2023 04:25 PM UTC

Hi Vivek,


Greetings from Syncfusion Support.


We have prepared sample from your provided code example but we are unable to replicate the problem at our end. Refer to the sample link:-

https://www.syncfusion.com/downloads/support/forum/162754/ze/webservice-sample2119249993.zip


Screenshot:-

We need some more additional details to find the cause of the issue. Share us the following details.


  1. Please confirm whether the data has been returned from server end. Share screenshot of it.
  2. Syncfusion Blazor TreeGrid package version details.
  3. Screenshot/Video demo to replicate the issue.
  4. If possible replicate it in the above sample and revert us back.
  5. Bind ActionFailure event to TreeGrid and share Exception details(if any) occurs.


Regards,
Farveen sulthana T


Loader.
Up arrow icon