This is a migrated thread and some comments may be shown as answers.

Pre-apply Filter Values

2 Answers 28 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joel
Top achievements
Rank 1
Veteran
Iron
Iron
Joel asked on 07 May 2020, 03:50 PM

My site has a quick-search field on the main nav.  When the user puts a first & last name in the field and hits the search button, they expect to land on my Persons.Index page with a list of filtered people.  Can you tell me how to pre-populate the filter with values?  Then, have the grid refresh using the filters.  I prefer not to do multiple round trips... prefer this to be in place the first time the grid reads.

My Grid

@(Html.Kendo().Grid<Person>()
  .Name("grid")
  .Columns(columns =>
  {
      columns.Command(command => command
          .Custom("Detail")
          .Click("goDetail"))
          .Width(Glossary.Portal.ButtonWidth);
      columns.Bound(p => p.FirstName)
          .Filterable(ftb => ftb.Cell(cell => cell.Operator("contains")
              .ShowOperators(false)
              .SuggestionOperator(FilterType.Contains)));
      columns.Bound(p => p.LastName)
          .Filterable(ftb => ftb.Cell(cell => cell.Operator("contains")
              .ShowOperators(false)
              .SuggestionOperator(FilterType.Contains)));
  })
  .Pageable()
  .Sortable()
  .Scrollable()
  .Filterable(ftb => ftb.Mode(GridFilterMode.Row))
  .HtmlAttributes(new { style = "height:550px;" })
  .DataSource(dataSource => dataSource
      .Ajax()
      .PageSize(20)
      .Read(read => read.Action("IndexJson", "Patients").Data("gridGetData"))
  ))

2 Answers, 1 is accepted

Sort by
0
Joel
Top achievements
Rank 1
Veteran
Iron
Iron
answered on 08 May 2020, 07:30 PM
From you other controls, it seems I am looking to set a filter expression when the page loads.
0
Accepted
Anton Mironov
Telerik team
answered on 12 May 2020, 01:14 PM

Hello, Joel,

Thank you for the provided code snippet!

The Kendo UI Grid sends a Read request to populate its data the first moment it gets initialized. To avoid the first request add the following option to the Grid's declaration:

.AutoBind(false)
Within the document.ready jQuery event, send the Read request with the relevant filters prepopulated. Here is an example:
 $(document).ready(function () {
        var grid = $('#grid').data('kendoGrid');

        grid.dataSource.filter({
            logic: "and",
            filters: [
                { field: "ShipName", operator: "contains", value: "4" },
                { field: "ShipCity", operator: "contains", value: "1" }
            ]
        });
    });

Additional information about the filter operators is presented in the below article:
https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/configuration/filterable.operators.number

I hope this information helps. Please let me know if I can assist you any further.

Regards,
Anton Mironov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Grid
Asked by
Joel
Top achievements
Rank 1
Veteran
Iron
Iron
Answers by
Joel
Top achievements
Rank 1
Veteran
Iron
Iron
Anton Mironov
Telerik team
Share this question
or