-
Notifications
You must be signed in to change notification settings - Fork 81
kb(grid): Add KB for changing the search results on column hide or show #2387
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ca1b250
kb(grid): Add KB for changing the search results on column hide or show
dimodi 3cfa1ec
Update knowledge-base/grid-search-column-menu.md
dimodi 232e2af
Update knowledge-base/grid-search-column-menu.md
dimodi c1cc1a3
Update knowledge-base/grid-search-column-menu.md
dimodi 7641622
Update knowledge-base/grid-search-column-menu.md
dimodi 96b39bd
Update grid-search-column-menu.md
dimodi 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
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,168 @@ | ||
| --- | ||
| title: Change Grid Search Results on Column Hide or Show | ||
| description: Learn how to modify the Grid search results on the fly when the user hides or shows a column. | ||
| type: how-to | ||
| page_title: How to Change Grid Search Results on Column Hide or Show | ||
| slug: grid-kb-search-match-visible-columns | ||
| tags: telerik, blazor, grid, search | ||
| ticketid: 1565592 | ||
| res_type: kb | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| <table> | ||
| <tbody> | ||
| <tr> | ||
| <td>Product</td> | ||
| <td>Grid for Blazor, <br /> TreeList for Blazor</td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
| ## Description | ||
|
|
||
| This KB article answers the following questions: | ||
|
|
||
| * How to change the Grid search results when the user hides or shows a string column? | ||
| * How to refresh and match the Grid search results when the `Visible` attribute of a column changes? | ||
| * How to hide or show search results in the Grid when the visibility of a column changes and a column is no longer displayed? | ||
|
|
||
| ## Solution | ||
|
|
||
| 1. Subscribe to the [Grid `OnStateChanged` event]({%slug grid-state%}#onstatechanged). | ||
| 1. Check if the `PropertyName` event argument is equal to `"ColumnStates"` to verify that the user has modified the column state. | ||
| 1. Check for `FilterDescriptor` instances in `args.GridState.SearchFilter.FilterDescriptors` to verify if a search is active. | ||
| 1. [Get the visible columns from `args.GridState.ColumnStates`.]({%slug grid-kb-column-state%}) Use only the columns with a `Field` that points to a `string` property. | ||
| 1. Compare the `Field` values of the visible string columns with the `Member` values of the search-related filter descriptors. | ||
| 1. Add or remove `FilterDescriptors` in `args.GridState.SearchFilter.FilterDescriptors` to align the search configuration with the currently visible columns. | ||
| 1. [Update the Grid state with `SetStateAsync()`]({%slug grid-state%}#methods). | ||
|
|
||
| >caption Change search results when the user hides or shows a column | ||
|
|
||
| <div class="skip-repl"></div> | ||
|
|
||
| ````CSHTML | ||
| @using Telerik.DataSource | ||
|
|
||
| <TelerikGrid @ref="@GridRef" | ||
| Data="@GridData" | ||
| TItem="@SampleModel" | ||
| Pageable="true" | ||
| Sortable="true" | ||
| ShowColumnMenu="true" | ||
| OnStateChanged="@OnGridStateChanged"> | ||
| <GridToolBarTemplate> | ||
| <GridSearchBox Placeholder="Type two identical letters" Width="200px" /> | ||
| </GridToolBarTemplate> | ||
| <GridColumns> | ||
| <GridColumn Field="@nameof(SampleModel.Id)" Width="100px" VisibleInColumnChooser="false" /> | ||
| <GridColumn Field="@nameof(SampleModel.Name)" /> | ||
| <GridColumn Field="@nameof(SampleModel.Description)" /> | ||
| </GridColumns> | ||
| </TelerikGrid> | ||
|
|
||
| @code { | ||
| private TelerikGrid<SampleModel>? GridRef { get; set; } | ||
|
|
||
| private List<SampleModel> GridData { get; set; } = new(); | ||
|
|
||
| // Non-string columns should not take part in the custom logic. | ||
| private readonly List<string> GridStringFields = new List<string>() { nameof(SampleModel.Name), nameof(SampleModel.Description) }; | ||
|
|
||
| private async Task OnGridStateChanged(GridStateEventArgs<SampleModel> args) | ||
| { | ||
| // This will be true also for column resizing, reordering and locking. | ||
| // Some additional checks exist below. | ||
| if (args.PropertyName == "ColumnStates") | ||
| { | ||
| var searchFilterDescriptors = ((CompositeFilterDescriptor)args.GridState.SearchFilter).FilterDescriptors; | ||
|
|
||
| if (searchFilterDescriptors.Any()) | ||
| { | ||
| var searchValue = ((FilterDescriptor)searchFilterDescriptors.First()).Value; | ||
| bool shouldRebindGrid = false; | ||
|
|
||
| var visibleStringColumnFields = new List<string>(); | ||
| var filterDescriptorsToRemove = new List<IFilterDescriptor>(); | ||
|
|
||
| foreach (GridColumnState colState in args.GridState.ColumnStates) | ||
| { | ||
| if (!string.IsNullOrEmpty(colState.Field) && | ||
| GridStringFields.Contains(colState.Field) && | ||
| (!colState.Visible.HasValue || colState.Visible.Value)) | ||
| { | ||
| visibleStringColumnFields.Add(colState.Field); | ||
| } | ||
| } | ||
|
|
||
| // Find FilterDescriptors for hidden columns. | ||
| foreach (FilterDescriptor fd in searchFilterDescriptors) | ||
| { | ||
| if (!visibleStringColumnFields.Contains(fd.Member)) | ||
| { | ||
| filterDescriptorsToRemove.Add(fd); | ||
| } | ||
| } | ||
| // Remove FilterDescriptors for hidden columns. | ||
| foreach (FilterDescriptor fd in filterDescriptorsToRemove) | ||
| { | ||
| searchFilterDescriptors.Remove(fd); | ||
| shouldRebindGrid = true; | ||
| } | ||
|
|
||
| // Add FilterDescriptors for newly shown columns. | ||
| foreach (string field in visibleStringColumnFields) | ||
| { | ||
| if (!searchFilterDescriptors.Any(x => ((FilterDescriptor)x).Member == field)) | ||
| { | ||
| searchFilterDescriptors.Add(new FilterDescriptor() | ||
| { | ||
| Member = field, | ||
| MemberType = typeof(string), | ||
| Operator = FilterOperator.Contains, | ||
| Value = searchValue | ||
| }); | ||
| shouldRebindGrid = true; | ||
| } | ||
| } | ||
|
|
||
| // Apply the changes in args.GridState.SearchFilter and rebind the Grid. | ||
| if (shouldRebindGrid) | ||
| { | ||
| await GridRef!.SetStateAsync(args.GridState); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected override void OnInitialized() | ||
| { | ||
| for (int i = 1; i <= 50; i++) | ||
| { | ||
| char nameCharCode = (char)(64 + i % 26 + 1); | ||
| char descriptionCharCode = (char)(91 - i % 26 - 1); | ||
|
|
||
| GridData.Add(new SampleModel() | ||
| { | ||
| Id = i, | ||
| Name = $"Name {i} {nameCharCode}{nameCharCode}", | ||
| Description = $"Description {i} {descriptionCharCode}{descriptionCharCode}" | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| public class SampleModel | ||
| { | ||
| public int Id { get; set; } | ||
| public string Name { get; set; } = string.Empty; | ||
| public string Description { get; set; } = string.Empty; | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
| ## See Also | ||
|
|
||
| * [Search in Hidden Grid Columns]({%slug grid-kb-search-in-hidden-fields%}) | ||
| * [Grid State]({%slug grid-state%}) | ||
| * [Grid SearchBox]({%slug grid-searchbox%}) | ||
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
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.