|
| 1 | +--- |
| 2 | +title: Filtering |
| 3 | +page_title: Grid for Blazor | Filtering |
| 4 | +description: Enable and configure filtering in Grid for Blazor |
| 5 | +slug: components/grid/filtering |
| 6 | +tags: telerik,blazor,grid,filtering,filter |
| 7 | +published: True |
| 8 | +position: 22 |
| 9 | +--- |
| 10 | + |
| 11 | +# Grid Filtering |
| 12 | + |
| 13 | +The Grid component offers support for filtering. |
| 14 | + |
| 15 | +To enable sorting, set the grid's `Filterable` property to `true`. |
| 16 | + |
| 17 | +The grid will render a row below the column headers with UI that you can use to fill in filter criteria and then you can click a button to execute the filter. Once a filter is a applied to a column, a button will appear that lets you clear that filter. |
| 18 | + |
| 19 | +The behavior of the filter header will depend on the column data type: |
| 20 | + |
| 21 | +* `string` - the filter is `Contains`. A [Telerik TextBox]({%slug components/textbox/overview%}) component is used. |
| 22 | +* `number` - the filter is `EqualsTo`. A [Telerik Numeric TextBox]({%slug components/numerictextbox/overview%}) component is used. |
| 23 | +* `DateTime` - the filter is `EqualsTo`. A [Telerik Date Input]({%slug components/dateinput/overview%}) component is used. |
| 24 | + |
| 25 | +You can filter more than one column at a time, and all filter rules will be applied together with an `AND` logic. |
| 26 | + |
| 27 | +>caption Enable Filtering in Telerik Grid |
| 28 | +
|
| 29 | +````CSHTML |
| 30 | +@using Telerik.Blazor.Components.Grid |
| 31 | +
|
| 32 | +<TelerikGrid Data=@GridData Filterable=true Pageable=true> |
| 33 | + <TelerikGridColumns> |
| 34 | + <TelerikGridColumn Field=@nameof(Employee.Name) /> |
| 35 | + <TelerikGridColumn Field=@nameof(Employee.AgeInYears) Title="Age" /> |
| 36 | + <TelerikGridColumn Field=@nameof(Employee.HireDate) Title="Hire Date" /> |
| 37 | + </TelerikGridColumns> |
| 38 | +</TelerikGrid> |
| 39 | +
|
| 40 | +@functions { |
| 41 | + public List<Employee> GridData { get; set; } |
| 42 | +
|
| 43 | + protected override void OnInit() |
| 44 | + { |
| 45 | + GridData = new List<Employee>(); |
| 46 | + var rand = new Random(); |
| 47 | + for (int i = 0; i < 100; i++) |
| 48 | + { |
| 49 | + GridData.Add(new Employee() |
| 50 | + { |
| 51 | + EmployeeId = i, |
| 52 | + Name = "Employee " + i.ToString(), |
| 53 | + AgeInYears = rand.Next(10, 80), |
| 54 | + HireDate = DateTime.Now.Date.AddDays(rand.Next(-20, 20)) |
| 55 | + }); |
| 56 | + } |
| 57 | + } |
| 58 | +
|
| 59 | + public class Employee |
| 60 | + { |
| 61 | + public int? EmployeeId { get; set; } |
| 62 | + public string Name { get; set; } |
| 63 | + public int? AgeInYears { get; set; } |
| 64 | + public DateTime HireDate { get; set; } |
| 65 | + } |
| 66 | +} |
| 67 | +```` |
| 68 | + |
| 69 | +>caption The result from the code snippet above, before and after the user filled in a filter and clicked on the filter button |
| 70 | +
|
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +## See Also |
| 77 | + |
| 78 | + * [Live Demo: Grid Filtering](https://demos.telerik.com/blazor-ui/grid/filtering) |
| 79 | + |
| 80 | + |
0 commit comments