Skip to content

Commit 9415017

Browse files
committed
chore(Grid): add kb for selectall with onRead
1 parent a3b02d6 commit 9415017

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
title: Select All Items With CheckBox when Using OnRead
3+
description: How Select All Items With CheckBox when Using OnRead in the Grid for Blazor
4+
type: how-to
5+
page_title: How Select All Items With CheckBox when Using OnRead
6+
slug: grid-kb-select-all-onread
7+
position:
8+
tags: grid, selection, select all, checkbox, onread
9+
ticketid: 1562945
10+
res_type: kb
11+
---
12+
13+
## Environment
14+
15+
<table>
16+
<tbody>
17+
<tr>
18+
<td>Product</td>
19+
<td>Grid for Blazor</td>
20+
</tr>
21+
</tbody>
22+
</table>
23+
24+
25+
## Description
26+
27+
I am binding the Grid through the `OnRead` event and I am using CheckBox selection with `SelectAllMode` set to `GridSelectAllMode.All`. However, when the user clicks the SelectAll CheckBox, only the items on the current page are selected, not all items in the dataset. How to ensure all items will be selected when clicking the SelectAll CheckBox.
28+
29+
## Solution
30+
31+
The described behavior is expected as when using the `OnRead` event, the Grid operates only with the current set/page of data. The component does not have information for all the items in your datasource and [thus it cannot select them all](slug:grid-selection-row#selection-and-paging).
32+
33+
To ensure all item will be selected upon clicking the SelectAll CheckBox when using the `OnRead` event, you can implement a custom approach:
34+
35+
1. Use [`HeaderTemplate` for the `CheckboxColumn`](slug:components/grid/columns/checkbox#header-template) and add a custom [CheckBox component](slug:checkbox-overview) so you can have full control over its behavior.
36+
1. Handle the [`OnChange` event](slug:checkbox-events#onchange) of the CheckBox to track when the user checks/unchecks it to manage the selected items.
37+
- When the CheckBox is checked, request all the data from your datasource and assign it to the `SelectedItems` collection, so you have all items selected. You may want to cache all the data for further usage, so you don't need to request it additionally upon clicking the custom SelectAll CheckBox.
38+
- When the user deselects the CheckBox, clear the `SelectedItems` collection.
39+
1. Manage the [`Indeterminate` state](slug:checkbox-indeterminate-state) of the CheckBox based on the selected items' count.
40+
1. Track [when the user changes the `Indeterminate` state](slug:checkbox-events#indeterminatechanged) of the CheckBox (clicks the CheckBox when it is in `Indeterminate` state). In this case, ensure that the CheckBox value will be always set to true if you want to completely mimic the default CheckBox selection behavior.
41+
42+
>caption Select all items with CheckBox when using OnRead
43+
44+
````RAZOR
45+
@using Telerik.DataSource.Extensions
46+
47+
<TelerikGrid TItem="@Employee"
48+
OnRead="@ReadItems"
49+
SelectionMode="@GridSelectionMode.Multiple"
50+
@bind-SelectedItems="@SelectedEmployees"
51+
Pageable="true">
52+
<GridColumns>
53+
<GridCheckboxColumn CheckBoxOnlySelection="true">
54+
<HeaderTemplate>
55+
@{
56+
<TelerikCheckBox @bind-Value="@SelectAllCheckBoxValue"
57+
Indeterminate="@(SelectedEmployees.Count() >= 1 && SelectedEmployees.Count() < Total)"
58+
IndeterminateChanged="@HandleIndeterminateChanged"
59+
OnChange="@HandleSelectAll"/>
60+
}
61+
</HeaderTemplate>
62+
</GridCheckboxColumn>
63+
<GridColumn Field=@nameof(Employee.ID) />
64+
<GridColumn Field=@nameof(Employee.Name)/>
65+
<GridColumn Field=@nameof(Employee.HireDate) Title="Hire Date" />
66+
</GridColumns>
67+
</TelerikGrid>
68+
69+
@code {
70+
private List<Employee> SourceData { get; set; }
71+
72+
private IEnumerable<Employee> SelectedEmployees { get; set; } = Enumerable.Empty<Employee>();
73+
74+
private bool SelectAllCheckBoxValue { get; set; }
75+
76+
private int Total { get; set; } = 0;
77+
78+
//Track when the user checks/unchecks the custom SelectAll CheckBox
79+
private void HandleSelectAll()
80+
{
81+
if (SelectAllCheckBoxValue)
82+
{
83+
// Request all the data and assign it to the SelectedItems collection. Here we use the already available SourceData for brevity.
84+
SelectedEmployees = SourceData;
85+
}
86+
else
87+
{
88+
SelectedEmployees = Enumerable.Empty<Employee>();
89+
}
90+
}
91+
92+
// Ensure that clicking the custom SelectAll CheckBox when it is in its indeterminate state will always select all items.
93+
// This mimics the default SelectAll CheckBox behavior.
94+
private void HandleIndeterminateChanged(bool value)
95+
{
96+
if (!value)
97+
{
98+
SelectAllCheckBoxValue = true;
99+
}
100+
}
101+
102+
protected async Task ReadItems(GridReadEventArgs args)
103+
{
104+
var datasourceResult = SourceData.ToDataSourceResult(args.Request);
105+
106+
args.Data = datasourceResult.Data;
107+
args.Total = datasourceResult.Total;
108+
109+
Total = datasourceResult.Total;
110+
}
111+
112+
protected override void OnInitialized()
113+
{
114+
SourceData = GenerateData();
115+
}
116+
117+
private List<Employee> GenerateData()
118+
{
119+
var result = new List<Employee>();
120+
var rand = new Random();
121+
for (int i = 0; i < 100; i++)
122+
{
123+
result.Add(new Employee()
124+
{
125+
ID = i,
126+
Name = "Name " + i,
127+
HireDate = DateTime.Now.Date.AddDays(rand.Next(-20, 20))
128+
});
129+
}
130+
131+
return result;
132+
}
133+
134+
public class Employee
135+
{
136+
public int ID { get; set; }
137+
public string Name { get; set; }
138+
public DateTime HireDate { get; set; }
139+
}
140+
}
141+
````
142+
143+
## See Also
144+
145+
* [Grid Row Selection](slug:grid-selection-row)
146+
* [Grid CheckBox Column](slug:components/grid/columns/checkbox)
147+
* [CheckBox component](slug:checkbox-overview)

0 commit comments

Comments
 (0)