import { Component, ViewChild, ViewEncapsulation, OnInit, Inject } from '@angular/core';
import { DataManager, WebApiAdaptor, Query, DataUtil } from '@syncfusion/ej2-data';
import { TreeGridComponent } from '@syncfusion/ej2-angular-treegrid';
let primaryKey: any;
let selectedRecord: any;
@Component({
selector: 'fetchdata',
templateUrl: './fetchdata.component.html',
encapsulation: ViewEncapsulation.Emulated
})
export class FetchDataComponent {
public data: any;
public toolbarOptions: string[] | undefined;
public editSettings: Object | undefined;
@ViewChild('treegrid')
public treegrid: TreeGridComponent;
ngOnInit(): void {
this.editSettings = { allowEditing: true, allowAdding: true, allowDeleting: true, mode: "Row", newRowPosition:"Below" };
this.toolbarOptions = ['Add', 'Delete', 'Update', 'Cancel'];
this.data = new DataManager({
url: 'api/SelfReferenceData',
adaptor: new CustomAdaptor()
});
}
actionBegin(args: any): void {
if (args.requestType == "save" && args.action == "add") {
primaryKey = selectedRecord.TaskID;
}
}
rowSelected(args: any): void {
selectedRecord = args.data;
}
}
////// Extending the WebApiAdaptor and overriding the insert method.
class CustomAdaptor extends WebApiAdaptor {
public insert(dm: DataManager, data: Object, tableName?: string): Object {
dm.dataSource.url = dm.dataSource.url + "?$key=" + primaryKey //// sending the primary key of selected record in query string
return {
type: 'POST',
url: dm.dataSource.url,
data: JSON.stringify(data),
};
}
}
[Controller.cs]
[HttpPost]
public object Post([FromBody]SelfReferenceData value)
{
var queryString = Request.Query;
StringValues keys;
queryString.TryGetValue("$key", out keys); /// retrieving the key value from querystring
int key = Int32.Parse(keys[0]);
SelfReferenceData.tree.Insert(0, value);
var data = SelfReferenceData.tree.ToList();
return Json(new { result = data, count = data.Count });
}
|
fetchdata.Component.html
<ejs-treegrid #treegrid [dataSource]='data' [allowPaging]="true" [loadChildOnDemand] ="true" hasChildMapping="isParent" [toolbar]='toolbarOptions' [treeColumnIndex]='1' height='270' [editSettings]='editSettings' idMapping='TaskID' parentIdMapping='ParentIDs' (actionBegin)='actionBegin($event)' (rowSelected)='rowSelected($event)'> |
OrdersController.cs
public object Get()
{
. . . . . . . . . . . . . . . . . .
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();
data = data.Skip(skip).Take(top);
data = CollectChildRecords(data, queryString);
return new { result = data, count = count };
}
else
{
return SelfReferenceData.GetTree();
}
}
public IEnumerable<SelfReferenceData> CollectChildRecords(IEnumerable<SelfReferenceData> DataSource, IQueryCollection Queries)
{
StringValues IdMapping;
Queries.TryGetValue("IdMapping", out IdMapping);
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<SelfReferenceData> ChildRecords = null;
foreach (int id in TaskIds)
{
IEnumerable<SelfReferenceData> records = SelfReferenceData.tree.Where(ds => ds.ParentIDs == id);
ChildRecords = ChildRecords == null || (ChildRecords.AsQueryable().Count() == 0) ? records : ((IEnumerable<SelfReferenceData>)ChildRecords).Concat((IEnumerable<SelfReferenceData>)records);
}
if (ChildRecords != null && ChildRecords.Count() > 0)
{
ChildRecords = CollectChildRecords(ChildRecords, Queries);
DataSource = ((IEnumerable<SelfReferenceData>)DataSource).Concat((IEnumerable<SelfReferenceData>)ChildRecords);
}
return DataSource;
} |
fetchdata.Component.ts
class CustomAdaptor extends WebApiAdaptor {
public insert(dm: DataManager, data: object, tableName?: string): Object {
var returnValue = super.insert(dm, data, tableName);
setValue("url", dm.dataSource.url + "?$key=" + primaryKey, returnValue);
return returnValue;
}
}
OrdersController.cs
public object Post([FromBody]SelfReferenceData value)
{
var queryString = Request.Query;
StringValues keys;
queryString.TryGetValue("$key", out keys); /// retrieving the key value from querystring
int index;
Int32.TryParse(keys[0],out index);
var i = 0;
for (; i < SelfReferenceData.tree.Count; i++)
{
if (SelfReferenceData.tree[i].TaskID == index)
{
break;
}
}
i += FindChildRecords(index);
SelfReferenceData.tree.Insert(i+1, value);
var data = SelfReferenceData.tree.ToList();
return Json(new { result = data, count = data.Count });
}
public int FindChildRecords(int? id)
{
var count = 0;
for (var i = 0; i < SelfReferenceData.tree.Count; i++)
{
if (SelfReferenceData.tree[i].ParentIDs == id)
{
count++;
count += FindChildRecords(SelfReferenceData.tree[i].TaskID);
}
}
return count;
} |