kendo ui grid filter

kendo ui grid filter, sort and paging in the server


Kendo UI Grid code-sample

@(Html.Kendo().Grid<Entities.Model.Proc_GetOrders_Result>()
.Name("GridOrders")
.Columns(columns =>
{
    columns.Bound(p => p.Order).Title("Order Detail").Filterable(true);
    columns.Bound(p => p.Customer).Title("Customer Detail").Filterable(true);
    columns.Bound(p => p.ID).Title("").Filterable(false).Title("Actions").Sortable(false);
})
.ClientRowTemplate(Html.Partial("_tabs").ToHtmlString())                                                                            
.DataSource(ds => ds
    .Ajax()
    .Sort(srt => srt.Add(e => e.ID).Descending())
    .Read(read => read.Action("GetOrders", "Orders"))
    .PageSize(PageSize)
)
.Events(e => e.DataBound("onDataBoundOrders"))
.Pageable()
.Sortable()
.Resizable(e => e.Columns(true))
    .Pageable(pager => pager
        .Refresh(true)
    )
.Filterable(filterable => filterable
    .Extra(false)
    .Operators(operators => operators
        .ForString(str => str.Clear()
            .StartsWith("Starts with")
            .EndsWith("Ends with")
            .Contains("Contains")
            .DoesNotContain("Not contains")
            )
        )
    )
)

Controller Action code-sample

        /// <summary>
        /// BELOW METHOD IS USED TO GET ORDERS
        /// </summary>
        public ActionResult GetOrders([DataSourceRequest] DataSourceRequest request, KendoCustomFilterCollection kendoCustomFilterCollection)
        {
            // We are not fetching the complete DataSet from DB as kendo grid do, but now we are requesting records based on PageIndex and PageSize.
            Tuple<int, List<Proc_GetOrders_Result>> orders = _repoOrder.GetOrders(request, kendoCustomFilterCollection);

            // Overriding request.Page property to 1.
            // Because now we are fetching records for single page only which contains records equals to page size which is by default 10.
            request.Page = 1;
            request.Filters = null;// filter Client Row Template.
            var result = orders.Item2.ToDataSourceResult(request);
            if (orders.Item1 > 0)
            {
                result.Total = orders.Item1;
            }
            return Json(result);
        }


        /// <summary>
        /// BELOW METHOD IS USED TO GET ORDERS FOR KENDO UI GRID
        /// </summary>
        public Tuple<int, List<Proc_GetOrders_Result>> GetOrders(Kendo.Mvc.UI.DataSourceRequest request, KendoCustomFilterCollection kendoCustomFilterCollection)
        {
            #region FILTERATION RELATED CODE

            List<Kendo.Mvc.FilterDescriptor> lFilters = new List<Kendo.Mvc.FilterDescriptor>();
            Expression<Func<Proc_GetOrders_Result, bool>> lambda = null;
            List<Expression<Func<Proc_GetOrders_Result, bool>>> lExpressions = new List<Expression<Func<Proc_GetOrders_Result, bool>>>();

            if (request.Filters != null && request.Filters.Count > 0)
            {
                //GET "FILTERDESCRIPTOR" AT RUNTIME
                lFilters = new Helper().GetFilterDescriptors(request.Filters);

                if (kendoCustomFilterCollection.Values != null)
                {
                    // FOR MULTIPLE FILTERS PER COLUMN
                    lExpressions = new Helper().CreateExpressions<Proc_GetOrders_Result, bool>(lExpressions, lFilters, kendoCustomFilterCollection);
                }
                else
                {
                    // FOR SINGLE FILTER PER COLUMN
                    lExpressions.Add(ExpressionBuilder.GetExpression<Proc_GetOrders_Result>(lFilters, "AND"));
                }

                //MERGE ALL EXPRESSIONS
                lambda = new Helper().MergeAllExpressions<Proc_GetOrders_Result, bool>(lExpressions);
            }

            #endregion

            //GET LIST
            Tuple<int, List<Proc_GetOrders_Result>> mylist = new Tuple<int, List<Proc_GetOrders_Result>>
                (base.Context.Proc_GetOrders().AsQueryable()
                .Where(lambda == null ? e => e.OrderID == e.OrderID : lambda)
                .Count(),
                base.Context.Proc_GetOrders().AsQueryable()
                .Where(lambda == null ? e => e.OrderID == e.OrderID : lambda)
                .Skip((request.Page - 1) * request.PageSize)
                .Take(request.PageSize)
                .ToList());

            return mylist;
        }

For the helpers class click on link

Not : if you want full code then contact me.

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.