-
Notifications
You must be signed in to change notification settings - Fork 81
chore(Grid): add kb for selectall with onRead #2697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9415017
chore(Grid): add kb for selectall with onRead
ntacheva 0c48219
Update knowledge-base/grid-select-all-onread.md
ntacheva 5c61956
Update knowledge-base/grid-select-all-onread.md
ntacheva 888ecab
Update knowledge-base/grid-select-all-onread.md
ntacheva c5ee158
chore(grid): address feedback and update example
ntacheva 96f9805
Update knowledge-base/grid-select-all-onread.md
ntacheva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| --- | ||
| title: Select All Grid Items With CheckBox when Using OnRead | ||
| description: How to select all Grid rows with checkBox when using the OnRead event in the Telerik Grid for Blazor. | ||
| type: how-to | ||
| page_title: How to Select All Grid Items With CheckBox when Using OnRead | ||
| slug: grid-kb-select-all-onread | ||
| position: | ||
| tags: grid, selection, select all, checkbox, onread | ||
| ticketid: 1562945 | ||
| res_type: kb | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| <table> | ||
| <tbody> | ||
| <tr> | ||
| <td>Product</td> | ||
| <td>Grid for Blazor</td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
|
|
||
| ## Description | ||
|
|
||
| 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. | ||
|
|
||
| ## Cause | ||
|
|
||
| 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). | ||
ntacheva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Solution | ||
|
|
||
| To ensure all items will be selected upon clicking the SelectAll CheckBox when using the `OnRead` event, you can implement a custom approach: | ||
|
|
||
| 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. | ||
| 1. Handle the [`ValueChanged` event](slug:checkbox-events#valuechanged) of the CheckBox to track when the user checks/unchecks it to manage the selected items. | ||
| - When the CheckBox is checked, add the newly coming items to the `SelectedItems` collection, so you have all items selected. This requires custom logic in the Grid `OnRead` and `SelectedItemsChanged` events. | ||
| - When the user deselects the CheckBox, clear the `SelectedItems` collection. | ||
| 1. Manage the [`Indeterminate` state](slug:checkbox-indeterminate-state) of the CheckBox based on the selected items' count. | ||
| 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. | ||
|
|
||
| >caption Select all items with CheckBox when using OnRead | ||
|
|
||
| ````RAZOR | ||
| @using Telerik.DataSource | ||
| @using Telerik.DataSource.Extensions | ||
|
|
||
| <TelerikGrid OnRead="@OnGridRead" | ||
| TItem="@SampleModel" | ||
| Pageable="true" | ||
| SelectedItems="@GridSelectedItems" | ||
| SelectedItemsChanged="@GridSelectedItemsChanged" | ||
| SelectionMode="@GridSelectionMode.Multiple"> | ||
| <GridToolBarTemplate> | ||
| All Items Selected: @(SelectAllCheckBoxValue.HasValue && SelectAllCheckBoxValue.Value) | ||
| <span class="k-separator"></span> | ||
| GridSelectedItems Count: @GridSelectedItems.Count() | ||
| </GridToolBarTemplate> | ||
| <GridColumns> | ||
| <GridCheckboxColumn> | ||
| <HeaderTemplate> | ||
| @{ | ||
| <TelerikCheckBox Value="@SelectAllCheckBoxValue" | ||
| ValueChanged="@( (bool? newValue) => SelectAllCheckBoxValueChanged(newValue) )" | ||
| TabIndex="-1" | ||
| Indeterminate="@(SelectAllCheckBoxValue == null)" /> | ||
| } | ||
| </HeaderTemplate> | ||
| </GridCheckboxColumn> | ||
| <GridColumn Field="@nameof(SampleModel.Name)" /> | ||
| <GridColumn Field="@nameof(SampleModel.Price)" /> | ||
| <GridColumn Field="@nameof(SampleModel.Quantity)" /> | ||
| </GridColumns> | ||
| </TelerikGrid> | ||
|
|
||
| @code { | ||
| private List<SampleModel> GridData { get; set; } = new(); | ||
| private IEnumerable<SampleModel> GridSelectedItems { get; set; } = new List<SampleModel>(); | ||
| private bool? SelectAllCheckBoxValue { get; set; } = false; | ||
| private IEnumerable<SampleModel> GridCurrentData { get; set; } = Enumerable.Empty<SampleModel>(); | ||
| private int GridCurrentTotal { get; set; } | ||
|
|
||
| private void SelectAllCheckBoxValueChanged(bool? newValue) | ||
| { | ||
| SelectAllCheckBoxValue = newValue; | ||
|
|
||
| if (SelectAllCheckBoxValue.HasValue && !SelectAllCheckBoxValue.Value) | ||
| { | ||
| GridSelectedItems = new List<SampleModel>(); | ||
| } | ||
| else | ||
| { | ||
| AddItemsToSelection(); | ||
| } | ||
| } | ||
|
|
||
| private void GridSelectedItemsChanged(IEnumerable<SampleModel> newSelectedItems) | ||
| { | ||
| if (SelectAllCheckBoxValue.HasValue) | ||
| { | ||
| SelectAllCheckBoxValue = null; | ||
| } | ||
|
|
||
| GridSelectedItems = newSelectedItems; | ||
| } | ||
|
|
||
| private void AddItemsToSelection() | ||
| { | ||
| GridCurrentData.Each(x => | ||
| { | ||
| var selectedItemsList = (List<SampleModel>)GridSelectedItems; | ||
| if (!selectedItemsList.Contains(x)) | ||
| { | ||
| selectedItemsList.Add(x); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private async Task OnGridRead(GridReadEventArgs args) | ||
| { | ||
| DataSourceResult result = await GridData.ToDataSourceResultAsync(args.Request); | ||
|
|
||
| args.Data = result.Data; | ||
| args.Total = result.Total; | ||
| args.AggregateResults = result.AggregateResults; | ||
|
|
||
| GridCurrentData = result.Data.Cast<SampleModel>(); | ||
| GridCurrentTotal = result.Total; | ||
|
|
||
| if (SelectAllCheckBoxValue.HasValue && SelectAllCheckBoxValue.Value) | ||
| { | ||
| AddItemsToSelection(); | ||
| } | ||
| } | ||
|
|
||
| protected override void OnInitialized() | ||
| { | ||
| for (int i = 1; i <= 1000; i++) | ||
| { | ||
| GridData.Add(new SampleModel() | ||
| { | ||
| Id = i, | ||
| Name = $"Name {i}", | ||
| GroupName = $"Group {i % 3 + 1}", | ||
| Price = Random.Shared.Next(1, 100) * 1.23m, | ||
| Quantity = Random.Shared.Next(0, 1000), | ||
| StartDate = DateTime.Now.AddDays(-Random.Shared.Next(60, 1000)), | ||
| IsActive = i % 4 > 0 | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| public class SampleModel | ||
| { | ||
| public int Id { get; set; } | ||
| public string Name { get; set; } = string.Empty; | ||
| public string GroupName { get; set; } = string.Empty; | ||
| public decimal Price { get; set; } | ||
| public int Quantity { get; set; } | ||
| public DateTime StartDate { get; set; } | ||
| public bool IsActive { get; set; } | ||
|
|
||
| public override bool Equals(object? obj) | ||
| { | ||
| return obj is SampleModel && ((SampleModel)obj).Id == Id; | ||
| } | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| return base.GetHashCode(); | ||
| } | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
| ## See Also | ||
|
|
||
| * [Grid Row Selection](slug:grid-selection-row) | ||
| * [Grid CheckBox Column](slug:components/grid/columns/checkbox) | ||
| * [CheckBox component](slug:checkbox-overview) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.