@(Html.EJ().Grid<object>("Grid")
.Datasource(ds => ds.URL("/Home/DataSource").Adaptor("UrlAdaptor"))
.AllowPaging()
.AllowFiltering()
.FilterSettings(filter=>filter.FilterType(FilterType.Excel))
.AllowSorting()
.Columns(col =>
{
col.Field("OrderID").IsPrimaryKey(true).HeaderText("Order ID").TextAlign(TextAlign.Right).Add();
col.Field("EmployeeID").HeaderText("Employee ID").TextAlign(TextAlign.Right).Add();
col.Field("Freight").HeaderText("Freight").EditType(EditingType.Numeric).TextAlign(TextAlign.Right).Add();
})
) |
[HomeController.cs]
public ActionResult DataSource(DataManager dm) //Using DataManager class which contains Sorting, filtering as a parameter.
{
….
} |
using Syncfusion.JavaScript.DataSources;
using Syncfusion.Linq;
using Syncfusion.JavaScript.Models;
public ActionResult DataSource(DataManager dm)
{
IEnumerable data = OrderRepository.GetAllRecords().ToList();
DataOperations operation = new DataOperations();
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
data = operation.PerformSorting(data, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
data = operation.PerformWhereFilter(data, dm.Where, dm.Where[0].Operator);
}
int count = data.Cast<EditableOrder>().Count();
if (dm.Skip != 0)
{
data = operation.PerformSkip(data, dm.Skip);
}
if (dm.Take != 0)
{
data = operation.PerformTake(data, dm.Take);
}
return Json(new { result = data, count = count });
}
public class DataResult
{
public IEnumerable result { get; set; }
public int count { get; set; }
}
public ActionResult Update(EditableOrder value)
{
//perform update action
}
public ActionResult Insert(EditableOrder value)
{
//perform insert action
}
public ActionResult Remove(int key)
{
//perform delete action
}
|
@(Html.EJ().Grid<object>("Grid")
.Datasource(ds => ds.URL("/Home/DataSource").Adaptor("UrlAdaptor"))
...
.Columns(col =>
{
...
.ClientSideEvents(eve => eve.ActionBegin("onActionBegin"))
)
<script>
function onActionBegin(args) {
if (args.requestType == "filterchoicerequest") {
//selects only the filtering column
//which prevents the serialization errror.
args.query.select(args.filterModel.fName);
}
}
</script>
public ActionResult DataSource(DataManager dm)
{
IEnumerable Data = new NorthwindDataContext().OrdersViews.ToList();
DataOperations operation = new DataOperations();
DataResult result = new DataResult();
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
Data = operation.PerformSorting(Data, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0)
{
Data = operation.PerformWhereFilter(Data, dm.Where, dm.Where[0].Operator);
}
result.count = Data.AsQueryable().Count();
if (dm.Select != null)
{
Data = operation.PerformSelect(Data, dm.Select);
Data = Data.Cast<dynamic>().Distinct().AsEnumerable();
}
if (dm.Skip != 0)
{
Data = operation.PerformSkip(Data, dm.Skip);
}
if (dm.Take != 0)
{
Data = operation.PerformTake(Data, dm.Take);
}
result.result = Data;
return Json(result, JsonRequestBehavior.AllowGet);
} |