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