<ejs-grid #grid [dataSource]='data' allowFiltering="true" allowPaging="true" allowSorting="true" height="320">
<e-columns>
<e-column field='OrderID' headerText='Order ID' isPrimaryKey=true width='150'></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
<e-column field='ShipCity' headerText='ShipCity' width='150' textAlign='Right'></e-column>
</e-columns>
</ejs-grid> |
export class FetchDataComponent {
public data: any;
@ViewChild('grid')
public grid: GridComponent;
ngOnInit(): void {
this.data = new DataManager({
url: 'api/Orders',
adaptor: new WebApiAdaptor
});
}
} |
public object Get()
{
var queryString = Request.Query;
var data = OrdersDetails.GetAllRecords().ToList();
string sort = queryString["$orderby"]; //sorting
string filter = queryString["$filter"]; //filtering
if (filter != null)
{
var newfiltersplits = filter;
var filtersplits = newfiltersplits.Split('(', ')', ' ');
var filterfield = filtersplits[1];
var filtervalue = filtersplits[3];
if (filtersplits.Length != 5)
{
filterfield = filter.Split('(', ')', '\'')[3]; //get field from query
filtervalue = filter.Split('(', ')', '\'')[5]; // get value from query
}
switch (filterfield)
{
case "OrderID":
data = (from cust in data
where cust.OrderID.ToString() == filtervalue.ToString()
select cust).ToList();
break;
case "CustomerID":
data = (from cust in data
wherecust.CustomerID.ToLower().StartsWith(filtervalue.ToString())
select cust).ToList();
break;
}
}
int skip = Convert.ToInt32(queryString["$skip"]); // paging
int take = Convert.ToInt32(queryString["$top"]);
return take != 0 ? new { Items = data.Skip(skip).Take(take).ToList(), Count = data.Count() } : new { Items = data, Count = data.Count() };
}
|
[fetchdata.component.html]
<ejs-grid #grid [dataSource]='data' allowFiltering="true" allowPaging="true" allowSorting="true" height="320" [filterSettings]="filterSettings" (actionComplete)="onActionComplete($event)" >
<e-columns>
<e-column field='OrderID' headerText='Order ID' isPrimaryKey=true width='150'></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
<e-column field='ShipCity' headerText='ShipCity' width='150' textAlign='Right'></e-column>
</e-columns>
[fetchdata.component.ts]
public onActionComplete(args:any){
if (args.requestType == 'filterafteropen') {
args.filterModel.sInput.onkeyup = this.addfilterParams.bind(this);
}
}
public addfilterParams(args: any) {
if (this.grid.query.params.length > 0) {
this.grid.query.params = [];
}
this.grid.query.addParams("where", args.currentTarget.value); // when put input value of excel filter search box to trigger and pass argument to server(its make search action in excel filtering)
}
}
[OrderDetailsController.cs]
public object Get()
{
var queryString = Request.Query;
var data = OrdersDetails.GetAllRecords().ToList();
string filter = queryString["$filter"];
string where = queryString["where"]; //Get the addParam value from client
if (where != null && filter !=null)
{
var newfiltersplits = filter;
var filtersplits = newfiltersplits.Split('(', ')', ' ');
var filterfield = filtersplits[1] == "tolower" ? filtersplits[2] : filtersplits[1];
var filtervalue = where;
if (data.Count == 0) {
data = OrdersDetails.GetAllRecords().ToList();
}
switch (filterfield)
{
case "OrderID":
data = (from cust in data
where cust.OrderID.ToString() == filtervalue.ToString()
select cust).ToList();
break;
case "CustomerID":
data = (from cust in data
where cust.CustomerID.StartsWith(filtervalue.ToString())
select cust).ToList();
break;
case "ShipCity":
data = (from cust in data
where cust.ShipCity.StartsWith(filtervalue.ToString())
select cust).ToList();
break;
}
}
int skip = Convert.ToInt32(queryString["$skip"]);
int take = Convert.ToInt32(queryString["$top"]);
return take != 0 ? new { Items = data.Skip(skip).Take(take).ToList(), Count = data.Count() } : new { Items = data, Count = data.Count() };
}
|