@(Html.EJ().Grid<object>("Grid")
.Datasource(ds => ds.URL("/odata/Orders").Adaptor(AdaptorType.ODataV4Adaptor))
.AllowPaging() //Paging
.AllowGrouping()
.AllowSorting() // sorting
.AllowResizing() // Resizing
.AllowFiltering() // Filtering
.FilterSettings(f => f.FilterType(FilterType.Excel))
.PageSettings(page => { page.PageSize(8); })
.EnableAltRow()
.Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(90).Add();
col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add();
..
})
)
OrdersController.cs
// GET api/<controller>
public PageResult<OrdersView> Get(ODataQueryOptions opts)
{
var results = db.OrdersViews.AsQueryable();
var count = results.Count();
if (opts.OrderBy != null)// perform sorting
results = opts.OrderBy.ApplyTo(results);
if (opts.Filter != null)
{
if (opts.Filter.RawValue.Contains("substring")) //perform searching
{
string key = opts.Filter.RawValue.Split(new string[] { "'" }, StringSplitOptions.None)[1];
results = results.Where(fil => fil.CustomerID.Contains(key) || fil.EmployeeID.ToString().Contains(key) || fil.Freight.ToString().Contains(key) || fil.OrderID.ToString().Contains(key) || fil.ShipCountry.Contains(key));
}
else
results = opts.Filter.ApplyTo(results, new ODataQuerySettings()).Cast<OrdersView>();
}
if (opts.InlineCount != null)
count = results.Count();
if (opts.Skip != null) //perform skip
results = opts.Skip.ApplyTo(results, new ODataQuerySettings());
if (opts.Top != null) //perform take
results = opts.Top.ApplyTo(results, new ODataQuerySettings());
var data = results;
return new PageResult<OrdersView>(results, null, count);
} |
Hi Jayaprakash,Thank you for the response. This is good if you have a strongly typed object on the server side for processing the output. (PageResult<OrdersView> )Could you please let me know if you have an example for a "dyanmic" object.? Or using a JSON object.Regards,Aravind
public object Get()
{
List<OrdersView> ord = db.OrdersViews.ToList();
var queryString = HttpContext.Current.Request.QueryString;
int skip = Convert.ToInt32(queryString["$skip"]);
int take = Convert.ToInt32(queryString["$top"]);
string sort =queryString["$orderby"];//sorting
var filter = queryString["$filter"];
if (filter != null)
{
string key;
if (filter.Contains("substring"))//searching
{
key = filter.Split(new string[] { "'" }, StringSplitOptions.None)[1];
ord = ord.Where(fil => fil.CustomerID.Contains(key.ToUpper()) || fil.EmployeeID.ToString().Contains(key) || fil.Freight.ToString().Contains(key) || fil.OrderID.ToString().Contains(key) || fil.ShipCountry.Contains(key)).Distinct().ToList();
}
}
else
{
ord = ord.Skip(skip).Take(take).ToList();
}
var count = ord.Count;
return new {Items = ord, Count = count };
} |