Filtering from controller

3 posts, 0 answers
  1. Raymond
    Raymond avatar
    28 posts
    Member since:
    Jul 2014

    Posted 14 Mar 2016 Link to this post

    I have a situation where I want to allow a grid to be "pre-filtered" based on an optional value passed into the query string.  I found an example, but it is only partially working.  The code that applies the filter is below.  It works in that the grid only shows the appropriate records.  However, it works different than a user applied filter.  The filter icon in the column header is not highlighted.  And when I click the filter icon, neither the filter operator or filter text appears in filter popup.  Is there a way to make it work like a user applied filter so the user can change or clear it?

     

     

    var f = new Kendo.Mvc.FilterDescriptor("OfficeDesc", Kendo.Mvc.FilterOperator.IsEqualTo, pOfficeDesc);

    request.Filters.Add(f);

     

    Thanks,

     

    Ray

  2. Raymond
    Raymond avatar
    28 posts
    Member since:
    Jul 2014

    Posted 14 Mar 2016 Link to this post

    Perhaps a better way to ask this is...I have the following grid definition.  I need to preset the filter based on query string parameters.  But I want the filter to appear as if the user set the filter, so that they can clear it.  I tried applying the filter in the controller Read action, and the data was filtered correctly, but the filter controls were not set as they would be if the user had applied the filter.

     

    I assume there is some way for the controller to pass an initial filter into the view, which would then appear as if the user had set the filter.

     

    @(Html.Kendo().Grid<Staff.Models.Employee>()
            .Name("grid")
            .Columns(columns =>
            {
                columns.Bound(p => p.FirstName).Filterable(false).Width("9%");
                columns.Bound(p => p.LastName).Width("9%");
                columns.Bound(p => p.OfficeDesc).Width("25%");
                columns.Bound(p => p.WorkUnitDesc).Width("15%");
                columns.Bound(p => p.EMail).Width("18%");
                columns.Bound(p => p.Phone).Width("8%");
                columns.Bound(p => p.Title).Width("16%");
            })
                    .ClientRowTemplate(
                "<tr >" +
                    "<td >#: FirstName# </td>" +
                    "<td >#: LastName# </td>" +
                    "<td >#: OfficeDesc #</td>" +
                    "<td >#: WorkUnitDesc #</td>" +
                    "<td ><a href='mailto:#: EMail #'>#: EMail #</a></td>" +
                    "<td >#: Phone #</td>" +
                    "<td >#: Title #</td>" +
                    "</tr>"
                )
                .ClientAltRowTemplate(
                    @"<tr class=""alt-row"" >" +
                        "<td >#: FirstName# </td>" +
                        "<td >#: LastName# </td>" +
                        "<td >#: OfficeDesc #</td>" +
                        "<td >#: WorkUnitDesc #</td>" +
                        "<td ><a href='mailto:#: EMail #'>#: EMail #</a></td>" +
                        "<td >#: Phone #</td>" +
                        "<td >#: Title #</td>" +
                        "</tr>"
                )
            .Pageable()
            .Sortable()
            .Scrollable()
            .Filterable()
            .HtmlAttributes(new { style = "height:550px;" })
            .Filterable()
            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(20)
                .Read(read => read.Action("Staff_Read", "Staff"))
            )
    )

     

  3. Raymond
    Raymond avatar
    28 posts
    Member since:
    Jul 2014

    Posted 15 Mar 2016 Link to this post

    I figured it out.

     

    In the controller, I create an empty list of filter descriptors, and add as many as I need.  That list gets passed into the view in the ViewBag.

                List<Kendo.Mvc.FilterDescriptor> filters = new List<Kendo.Mvc.FilterDescriptor>();
                if (unit != "")
                {
                    filters.Add(new Kendo.Mvc.FilterDescriptor("WorkUnitDesc", Kendo.Mvc.FilterOperator.IsEqualTo, unit));
                }
                if (office != "")
                {
                    filters.Add(new Kendo.Mvc.FilterDescriptor("OfficeDesc", Kendo.Mvc.FilterOperator.IsEqualTo, office));
                }
                ViewBag.filters = filters;

     

    Then inside the view, the grid datasource uses the AddRange function to add however many filters descriptors are in the list.  

     

            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(20)
                .Read(read => read.Action("Staff_Read", "Staff"))
                .Filter(f => f.AddRange(ViewBag.filters))

     

     

Back to Top