Skip to content

Commit cb315f1

Browse files
Checklist filter (#204)
* chore(grid): split filtering article * docs(grid): checklist basics (wip) * chore: filter folder; searchbxo title; gitignore sync * docs(grid): checklist filter first sample * docs(grid): all distinct options for OnRead * docs(grid): better custom data explanations and example * chore: links, typo * chore(grid): small improvement on sample * chore(treelist): split filtering article * chore(treelist): filter folder ToC settings * doce(treelist): checlist filtering * chore(treelist): optimize checlist gif size * docs(treelist): custom data example for treelist * chore(treelist): crossling menu template with checkbox filter
1 parent 0ce8178 commit cb315f1

26 files changed

+1376
-741
lines changed

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2+
.DS_Store
23
.asset-cache/
34
.dockerignore
4-
.DS_Store
55
.editorconfig
66
.jekyll-metadata
77
.ruby-version
@@ -21,14 +21,14 @@
2121
/favicon.ico
2222
/fonts/
2323
/styles/
24+
Dockerfile
25+
Gemfile
26+
Gemfile.lock
2427
_tempconfig.yml
2528
bs-config.js
2629
build-docs.sh
2730
copy_content.sh
2831
copy_local.sh
29-
Dockerfile
30-
Gemfile
31-
Gemfile.lock
3232
install-npm.sh
3333
knowledge-base.html
3434
modify-config.sh

_config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ navigation:
7171
"*components/grid/editing":
7272
title: "Editing"
7373
position: 10
74+
"*components/grid/filter":
75+
title: "Filtering"
76+
position: 22
7477
"*components/grid/grouping":
7578
title: "Grouping"
7679
position: 24
@@ -92,6 +95,9 @@ navigation:
9295
"*components/treelist/editing":
9396
title: "Editing"
9497
position: 10
98+
"*components/treelist/filter":
99+
title: "Filtering"
100+
position: 22
95101
"*components/treelist/templates":
96102
title: "Templates"
97103
position: 25
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
---
2+
title: CheckBoxList
3+
page_title: Grid - Filtering CheckBoxList
4+
description: Enable and configure filtering CheckBoxList in Grid for Blazor.
5+
slug: grid-checklist-filter
6+
tags: telerik,blazor,grid,filtering,filter,CheckBoxList
7+
published: True
8+
position: 15
9+
---
10+
11+
# Grid CheckBoxList Filtering
12+
13+
You can change the [filter menu]({%slug grid-filter-menu%}) to show a list of checkboxes with the distinct values from the data source. This lets your users filter records by a commonly found value quickly, and select multiple values with ease. The behavior is similar to Excel filtering.
14+
15+
To enable the checkbox list filtering in the grid:
16+
17+
1. Set the `FilterMode` parameter of the grid to `Telerik.Blazor.GridFilterMode.FilterMenu`
18+
1. Set the `FilterMenuType` parameter of the grid to `Telerik.Blazor.FilterMenuType.CheckBoxList`. It defaults to `Menu` for the default behavior.
19+
20+
You can also change the filter menu behavior for a particular column - its own `FilterMenuType` parameter can be either `Menu` or `CheckBoxList` regardless of the main grid parameter. This lets you mix both modes as necessary for your application - you can either have all grid columns use the same mode with a single setting, or override it for a few columns that need the less common mode.
21+
22+
>caption CheckList filter in the grid
23+
24+
````CSHTML
25+
@* Checkbox List Filter for the Name, Team and Vacation columns, the ID column overrides it to Menu *@
26+
27+
<TelerikGrid Data=@GridData Pageable="true" Height="400px"
28+
FilterMode="@GridFilterMode.FilterMenu" FilterMenuType="@FilterMenuType.CheckBoxList">
29+
<GridColumns>
30+
<GridColumn Field="@(nameof(Employee.EmployeeId))" FilterMenuType="@FilterMenuType.Menu" />
31+
<GridColumn Field=@nameof(Employee.Name) />
32+
<GridColumn Field=@nameof(Employee.Team) Title="Team" />
33+
<GridColumn Field=@nameof(Employee.IsOnLeave) Title="On Vacation" />
34+
</GridColumns>
35+
</TelerikGrid>
36+
37+
@code {
38+
public List<Employee> GridData { get; set; }
39+
40+
protected override void OnInitialized()
41+
{
42+
GridData = new List<Employee>();
43+
var rand = new Random();
44+
for (int i = 0; i < 15; i++)
45+
{
46+
GridData.Add(new Employee()
47+
{
48+
EmployeeId = i,
49+
Name = "Employee " + i.ToString(),
50+
Team = "Team " + i % 3,
51+
IsOnLeave = i % 2 == 0
52+
});
53+
}
54+
}
55+
56+
public class Employee
57+
{
58+
public int EmployeeId { get; set; }
59+
public string Name { get; set; }
60+
public string Team { get; set; }
61+
public bool IsOnLeave { get; set; }
62+
}
63+
}
64+
````
65+
66+
>caption The result from the snippet above
67+
68+
![checbox list filter in action](images/checklist-filter-overview.gif)
69+
70+
71+
## Custom Data
72+
73+
By default, the grid takes the `Distinct` values from its `Data` to populate the chekcbox list filter for each field.
74+
75+
When using the [`OnRead` event]({%slug components/grid/manual-operations%}) to customize the data operations and/or perform them on the server/service, the grid will only have the current page of data. This will limit the options the user will see so you may want to provide the full list.
76+
77+
To customize the checkbox list behavior, you should use the [filter menu template]({%slug grid-templates-filter%}#filter-menu-template). To help you with that, we have exposed the `TelerikCheckBoxListFilter` component that you can place inside the `FilterMenuTemplate` to get the default grid UI. It provides the following settings:
78+
79+
* `FilterDescriptor` - the filter descriptor where filters will be populated when checkboxes are selected. The component creates the necessary descriptors for you and reads existing ones. This makes it easy to plug into the grid without any additional code through two-way binding (`@bind-FilterDescriptor="@context.FilterDescriptor"`)..
80+
81+
* `Data` - the data that will be rendered in the checkbox list. This is where you can supply the desired options to change what the grid displays.
82+
83+
* `Field` - the field from the data that will be used to take the `Distinct` options. It must match the name and type of the column field for which this filter is defined. This lets you use the same models that the grid uses, or to define smaller models to reduce the data you fetch for the filter lists.
84+
85+
>caption Provide all filtering options when using OnRead
86+
87+
````CSHTML
88+
@using Telerik.DataSource
89+
@using Telerik.DataSource.Extensions
90+
91+
Filter by selecting a few names. Then filter by the Teams field (the fields that use application-provided data).<br />
92+
You will see you have all the options for the teams and all the options for the names.<br />
93+
Now try to filter by the On Vacation column - it will use only the current grid data and you may have only a single option,
94+
depending on how you filter the data so you may never be able to get back all values.
95+
96+
97+
<TelerikGrid Data=@CurrentGridData Pageable="true" Height="400px"
98+
TotalCount="Total" OnRead="@OnReadHandler"
99+
FilterMode="@GridFilterMode.FilterMenu" FilterMenuType="@FilterMenuType.CheckBoxList">
100+
<GridColumns>
101+
<GridColumn Field="@(nameof(Employee.EmployeeId))" Filterable="false" />
102+
<GridColumn Field="@nameof(Employee.Name)">
103+
<FilterMenuTemplate Context="context">
104+
<TelerikCheckBoxListFilter Data="@NameOptions"
105+
Field="@(nameof(NameFilterOption.Name))"
106+
@bind-FilterDescriptor="@context.FilterDescriptor">
107+
</TelerikCheckBoxListFilter>
108+
</FilterMenuTemplate>
109+
</GridColumn>
110+
<GridColumn Field="@nameof(Employee.Team)" Title="Team">
111+
<FilterMenuTemplate Context="context">
112+
<TelerikCheckBoxListFilter Data="@TeamsList"
113+
Field="@(nameof(TeamNameFilterOption.Team))"
114+
@bind-FilterDescriptor="@context.FilterDescriptor">
115+
</TelerikCheckBoxListFilter>
116+
</FilterMenuTemplate>
117+
</GridColumn>
118+
<GridColumn Field="@nameof(Employee.IsOnLeave)" Title="On Vacation" />
119+
</GridColumns>
120+
</TelerikGrid>
121+
122+
@code {
123+
List<Employee> AllGridData { get; set; }
124+
125+
IEnumerable<Employee> CurrentGridData { get; set; }
126+
int Total { get; set; }
127+
128+
#region custom-filter-data
129+
List<TeamNameFilterOption> TeamsList { get; set; }
130+
List<NameFilterOption> NameOptions { get; set; }
131+
132+
//obtain filter lists data from the data source to show all options
133+
async Task GetTeamOptions()
134+
{
135+
if (TeamsList == null) // sample of caching since we always want all distinct options,
136+
//but we don't want to make unnecessary requests
137+
{
138+
TeamsList = await GetNamesFromService();
139+
}
140+
}
141+
142+
async Task<List<TeamNameFilterOption>> GetNamesFromService()
143+
{
144+
await Task.Delay(500);// simulate a real service delay
145+
146+
// this is just one example of getting distinct values from the full data source
147+
// in a real case you'd probably call your data service here instead
148+
// or apply further logic (such as tie the returned data to the data the grid will have according to your business logic)
149+
List<TeamNameFilterOption> data = AllGridData.OrderBy(z => z.Team).Select(z => z.Team).
150+
Distinct().Select(t => new TeamNameFilterOption { Team = t }).ToList();
151+
152+
return await Task.FromResult(data);
153+
}
154+
155+
async Task GetNameOptions()
156+
{
157+
if (NameOptions == null)
158+
{
159+
NameOptions = await GetNameOptionsFromService();
160+
}
161+
}
162+
163+
async Task<List<NameFilterOption>> GetNameOptionsFromService()
164+
{
165+
await Task.Delay(500);// simulate a real service delay
166+
167+
List<NameFilterOption> data = AllGridData.OrderBy(z => z.Name).Select(z => z.Name).
168+
Distinct().Select(n => new NameFilterOption { Name = n }).ToList();
169+
170+
return await Task.FromResult(data);
171+
}
172+
#endregion custom-filter-data
173+
174+
async Task OnReadHandler(GridReadEventArgs e)
175+
{
176+
//typical data retrieval for the grid
177+
var filteredData = await AllGridData.ToDataSourceResultAsync(e.Request);
178+
CurrentGridData = filteredData.Data as IEnumerable<Employee>;
179+
Total = filteredData.Total;
180+
}
181+
182+
protected override async Task OnInitializedAsync()
183+
{
184+
// generate data that simulates the database for this example
185+
// the actual grid data is retrieve in its OnRead handler
186+
AllGridData = new List<Employee>();
187+
var rand = new Random();
188+
for (int i = 0; i < 15; i++)
189+
{
190+
AllGridData.Add(new Employee()
191+
{
192+
EmployeeId = i,
193+
Name = "Employee " + i.ToString(),
194+
Team = "Team " + i % 3,
195+
IsOnLeave = i % 2 == 0
196+
});
197+
}
198+
199+
// get custom filters data. In a future version you will be able to call these methods
200+
// from the template initialization instead of here (or in OnRead) so that they fetch data
201+
// only when the user actually needs filter values, instead of always - that could improve server performance
202+
await GetTeamOptions();
203+
await GetNameOptions();
204+
}
205+
206+
public class Employee
207+
{
208+
public int EmployeeId { get; set; }
209+
public string Name { get; set; }
210+
public string Team { get; set; }
211+
public bool IsOnLeave { get; set; }
212+
}
213+
214+
// in this sample we use simplified models to fetch less data from the service
215+
// instead of using the full Employee model that has many fields we do not need for the filters
216+
217+
public class TeamNameFilterOption
218+
{
219+
public string Team { get; set; }
220+
}
221+
222+
public class NameFilterOption
223+
{
224+
public string Name { get; set; }
225+
}
226+
}
227+
````
228+
229+
230+
231+
## See Also
232+
233+
* [Grid Filtering Overview]({%slug components/grid/filtering%})
234+
* [Live Demo: Grid CheckBox List Filter](https://demos.telerik.com/blazor-ui/grid/filter-checkboxlist)
235+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: Filter Menu
3+
page_title: Grid - Filter Menu
4+
description: Enable and configure Filter Menu in Grid for Blazor.
5+
slug: grid-filter-menu
6+
tags: telerik,blazor,grid,filtering,filter,menu
7+
published: True
8+
position: 10
9+
---
10+
11+
# Grid Filter Menu
12+
13+
One of the [filter modes of the grid]({%slug components/grid/filtering%}) is a popup menu with filter options that you can open from the column headers.
14+
15+
To enable the filter menu, set the `FilterMode` property of the grid to `Telerik.Blazor.GridFilterMode.FilterMenu`.
16+
17+
The grid will render a button in the column header that you click to get a popup with filtering options. The popup lets you choose filter operator, filter criteria, to apply and clear the filter.
18+
19+
The filter is applied only upon a button click, not upon input change. This may improve performance if you use [manual CRUD operations]({%slug components/grid/manual-operations%}) by reducing the number of requests compared to using the [Filter Row]({%slug grid-filter-row%}).
20+
21+
>caption Filter Menu in Telerik Grid
22+
23+
````CSHTML
24+
@* Filter menu in the column header *@
25+
26+
<TelerikGrid Data=@GridData FilterMode="Telerik.Blazor.GridFilterMode.FilterMenu"
27+
Pageable="true" Height="400px">
28+
<GridColumns>
29+
<GridColumn Field=@nameof(Employee.Name) />
30+
<GridColumn Field=@nameof(Employee.AgeInYears) Title="Age" />
31+
<GridColumn Field=@nameof(Employee.HireDate) Title="Hire Date" />
32+
<GridColumn Field=@nameof(Employee.IsOnLeave) Title="On Vacation" />
33+
</GridColumns>
34+
</TelerikGrid>
35+
36+
@code {
37+
public List<Employee> GridData { get; set; }
38+
39+
protected override void OnInitialized()
40+
{
41+
GridData = new List<Employee>();
42+
var rand = new Random();
43+
for (int i = 0; i < 100; i++)
44+
{
45+
GridData.Add(new Employee()
46+
{
47+
EmployeeId = i,
48+
Name = "Employee " + i.ToString(),
49+
AgeInYears = rand.Next(10, 80),
50+
HireDate = DateTime.Now.Date.AddDays(rand.Next(-20, 20)),
51+
IsOnLeave = i % 3 == 0
52+
});
53+
}
54+
}
55+
56+
public class Employee
57+
{
58+
public int? EmployeeId { get; set; }
59+
public string Name { get; set; }
60+
public int? AgeInYears { get; set; }
61+
public DateTime HireDate { get; set; }
62+
public bool IsOnLeave { get; set; }
63+
}
64+
}
65+
````
66+
67+
>caption The result from the code snippet above, after the "Age" column has been filtered with <= 30 operator.
68+
69+
![](images/filter-menu-1.png)
70+
71+
72+
## Filter From Code
73+
74+
You can set the grid filters from your code through the grid [state]({%slug grid-state%}).
75+
76+
@[template](/_contentTemplates/grid/state.md#initial-state)
77+
78+
>caption Set filtering programmatically
79+
80+
````CSHTML
81+
@[template](/_contentTemplates/grid/state.md#filter-menu-from-code)
82+
````
83+
84+
85+
86+
## See Also
87+
88+
* [Grid Filtering Overview]({%slug components/grid/filtering%})
89+
* [Live Demo: Grid Filter Menu](https://demos.telerik.com/blazor-ui/grid/filter-menu)

0 commit comments

Comments
 (0)