We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

TreeGrid WebApiAdaptor

Hi,
I am trying to implement treegrid with the WebApiAdaptor, I am looking at your example here: https://ej2.syncfusion.com/aspnetcore/TreeGrid/RemoteData#/material.
As I can see you have SelfReferenceData controller to handle the treegrid (url="https://ej2services.syncfusion.com/production/web-services/api/SelfReferenceData").
Can you please send me code for the SelfReferenceData controller because I need to know how this example works.
Thank you in advance!
Elena


3 Replies

VN Vignesh Natarajan Syncfusion Team March 11, 2019 11:43 AM UTC

Hi Elena, 
 
Thanks for contacting Syncfusion support. 
 
Query : Can you please send me code for the SelfReferenceData controller because I need to know how this example works. 
 
According to your query we understand that you need to know how to handle the data in the server side for WebAPI service in EJ2 Treegrid. We have achieved your requirement using WebAPI Adaptor of EJS TreeGrid. Please refer the below code example for the same. 
 
[index.cshtml] 
<ejs-treegrid id="TreeGrid" hasChildMapping="isParent" idMapping="taskID" parentIdMapping="parentItem" treeColumnIndex="1" allowPaging="true"> 
        <e-data-manager url="/api/SelfReference" adaptor="WebApiAdaptor" crossDomain="true"></e-data-manager> 
        <e-treegrid-columns> 
            <e-treegrid-column field="taskID" headerText="Task ID" textAlign="Right" width="120"></e-treegrid-column> 
            <e-treegrid-column field="taskName" headerText="Task Name" width="150"></e-treegrid-column> 
            <e-treegrid-column field="startDate" headerText=" Start Date" textAlign="Right" format="yMd" type="date" width="120"></e-treegrid-column> 
            <e-treegrid-column field="endDate" headerText=" End Date" textAlign="Right" format="yMd" type="date" width="120"></e-treegrid-column> 
            <e-treegrid-column field="duration" headerText="Duration" textAlign="Right" width="110"></e-treegrid-column> 
            <e-treegrid-column field="progress" headerText="Progress" textAlign="Right" width="110"></e-treegrid-column> 
        </e-treegrid-columns> 
    </ejs-treegrid> 
 
[selfreferencecontroller.cs] 
 
[Route("api/[controller]")] 
    [ApiController] 
    public class SelfReferenceController : ControllerBase 
    { 
 
        // GET: api/SelfReference 
        [HttpGet] 
        public object Get() 
        { 
            var queryString = Request.Query; 
            if (SelfReferenceData.tree.Count == 0) 
                SelfReferenceData.GetTree(); 
            if (queryString.Keys.Contains("$filter") &&   // this is used to handle the fiter query to send  
!queryString.Keys.Contains("$inlinecount"))                                                                     the child records when a parent record is expanded 
            { 
                StringValues filter; 
                queryString.TryGetValue("$filter", out filter); 
                int fltr = Int32.Parse(filter[0].ToString().Split("eq")[1]); 
                IQueryable<SelfReferenceData> data1 = SelfReferenceData.tree.Where(f => f.ParentItem == fltr).AsQueryable(); 
                return data1.ToList(); 
            } 
            List<SelfReferenceData> data = SelfReferenceData.tree.ToList(); 
 
            if (queryString.Keys.Contains("$select"))    // select query is used to fetch the parent records if                 
            {                                  hasChildMapping is not specified. 
                data = (from ord in SelfReferenceData.tree 
                        select new SelfReferenceData 
                        { 
                            ParentItem = ord.ParentItem 
                        } 
                        ).ToList(); 
                return data; 
            } 
            data = data.Where(p => p.ParentItem == 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 { result = data.Skip(skip).Take(top), count = count }; 
            } 
            else 
            { 
                return data; 
            } 
        } 
 
Refer our online documentation for your reference 
 
 
Please get back to us if you have further queries. 
 
Regards, 
Vignesh Natarajan. 
 



EP Elena Popova March 13, 2019 09:21 AM UTC

Thank you for the information provided!


VN Vignesh Natarajan Syncfusion Team March 14, 2019 03:57 AM UTC

Hi Elena, 

Thanks for the update. 

Please get back to us if you need further assistance on this.  

Regards, 
Vignesh Natarajan. 


Loader.
Live Chat Icon For mobile
Up arrow icon