Skip to content

Commit 92b6400

Browse files
dimodiikoevska
andauthored
kb(grid): Add KB for changing the search results on column hide or show (#2387)
* kb(grid): Add KB for changing the search results on column hide or show * Update knowledge-base/grid-search-column-menu.md * Update knowledge-base/grid-search-column-menu.md Co-authored-by: Iva Stefanova Koevska-Atanasova <koevska@progress.com> * Update knowledge-base/grid-search-column-menu.md Co-authored-by: Iva Stefanova Koevska-Atanasova <koevska@progress.com> * Update knowledge-base/grid-search-column-menu.md Co-authored-by: Iva Stefanova Koevska-Atanasova <koevska@progress.com> * Update grid-search-column-menu.md --------- Co-authored-by: Iva Stefanova Koevska-Atanasova <koevska@progress.com>
1 parent 9d9a387 commit 92b6400

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

components/grid/filter/searchbox.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ The example below demonstrates all SearchBox settings in action, and also how to
146146
* [Highlight or Bold Search Results in the Grid]({%slug grid-kb-search-highlight-results%})
147147
* [Search the Grid in Numeric and Date Model Fields]({%slug grid-kb-search-numeric-fields%})
148148
* [Search the Grid in Hidden Fields]({%slug grid-kb-search-in-hidden-fields%})
149+
* [Change Grid Search Results on Column Hide or Show]({%slug grid-kb-search-match-visible-columns%})
149150
* [Search the Grid with a `StartsWith` operator]({%slug grid-kb-search-startswith%})
150151
* [Search the Grid on Button Click]({%slug grid-kb-search-button-click%})
151152
* [Grid Filtering Overview]({%slug components/grid/filtering%})
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
---
2+
title: Change Grid Search Results on Column Hide or Show
3+
description: Learn how to modify the Grid search results on the fly when the user hides or shows a column.
4+
type: how-to
5+
page_title: How to Change Grid Search Results on Column Hide or Show
6+
slug: grid-kb-search-match-visible-columns
7+
tags: telerik, blazor, grid, search
8+
ticketid: 1565592
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>Grid for Blazor, <br /> TreeList for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
## Description
24+
25+
This KB article answers the following questions:
26+
27+
* How to change the Grid search results when the user hides or shows a string column?
28+
* How to refresh and match the Grid search results when the `Visible` attribute of a column changes?
29+
* How to hide or show search results in the Grid when the visibility of a column changes and a column is no longer displayed?
30+
31+
## Solution
32+
33+
1. Subscribe to the [Grid `OnStateChanged` event]({%slug grid-state%}#onstatechanged).
34+
1. Check if the `PropertyName` event argument is equal to `"ColumnStates"` to verify that the user has modified the column state.
35+
1. Check for `FilterDescriptor` instances in `args.GridState.SearchFilter.FilterDescriptors` to verify if a search is active.
36+
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.
37+
1. Compare the `Field` values of the visible string columns with the `Member` values of the search-related filter descriptors.
38+
1. Add or remove `FilterDescriptors` in `args.GridState.SearchFilter.FilterDescriptors` to align the search configuration with the currently visible columns.
39+
1. [Update the Grid state with `SetStateAsync()`]({%slug grid-state%}#methods).
40+
41+
>caption Change search results when the user hides or shows a column
42+
43+
<div class="skip-repl"></div>
44+
45+
````CSHTML
46+
@using Telerik.DataSource
47+
48+
<TelerikGrid @ref="@GridRef"
49+
Data="@GridData"
50+
TItem="@SampleModel"
51+
Pageable="true"
52+
Sortable="true"
53+
ShowColumnMenu="true"
54+
OnStateChanged="@OnGridStateChanged">
55+
<GridToolBarTemplate>
56+
<GridSearchBox Placeholder="Type two identical letters" Width="200px" />
57+
</GridToolBarTemplate>
58+
<GridColumns>
59+
<GridColumn Field="@nameof(SampleModel.Id)" Width="100px" VisibleInColumnChooser="false" />
60+
<GridColumn Field="@nameof(SampleModel.Name)" />
61+
<GridColumn Field="@nameof(SampleModel.Description)" />
62+
</GridColumns>
63+
</TelerikGrid>
64+
65+
@code {
66+
private TelerikGrid<SampleModel>? GridRef { get; set; }
67+
68+
private List<SampleModel> GridData { get; set; } = new();
69+
70+
// Non-string columns should not take part in the custom logic.
71+
private readonly List<string> GridStringFields = new List<string>() { nameof(SampleModel.Name), nameof(SampleModel.Description) };
72+
73+
private async Task OnGridStateChanged(GridStateEventArgs<SampleModel> args)
74+
{
75+
// This will be true also for column resizing, reordering and locking.
76+
// Some additional checks exist below.
77+
if (args.PropertyName == "ColumnStates")
78+
{
79+
var searchFilterDescriptors = ((CompositeFilterDescriptor)args.GridState.SearchFilter).FilterDescriptors;
80+
81+
if (searchFilterDescriptors.Any())
82+
{
83+
var searchValue = ((FilterDescriptor)searchFilterDescriptors.First()).Value;
84+
bool shouldRebindGrid = false;
85+
86+
var visibleStringColumnFields = new List<string>();
87+
var filterDescriptorsToRemove = new List<IFilterDescriptor>();
88+
89+
foreach (GridColumnState colState in args.GridState.ColumnStates)
90+
{
91+
if (!string.IsNullOrEmpty(colState.Field) &&
92+
GridStringFields.Contains(colState.Field) &&
93+
(!colState.Visible.HasValue || colState.Visible.Value))
94+
{
95+
visibleStringColumnFields.Add(colState.Field);
96+
}
97+
}
98+
99+
// Find FilterDescriptors for hidden columns.
100+
foreach (FilterDescriptor fd in searchFilterDescriptors)
101+
{
102+
if (!visibleStringColumnFields.Contains(fd.Member))
103+
{
104+
filterDescriptorsToRemove.Add(fd);
105+
}
106+
}
107+
// Remove FilterDescriptors for hidden columns.
108+
foreach (FilterDescriptor fd in filterDescriptorsToRemove)
109+
{
110+
searchFilterDescriptors.Remove(fd);
111+
shouldRebindGrid = true;
112+
}
113+
114+
// Add FilterDescriptors for newly shown columns.
115+
foreach (string field in visibleStringColumnFields)
116+
{
117+
if (!searchFilterDescriptors.Any(x => ((FilterDescriptor)x).Member == field))
118+
{
119+
searchFilterDescriptors.Add(new FilterDescriptor()
120+
{
121+
Member = field,
122+
MemberType = typeof(string),
123+
Operator = FilterOperator.Contains,
124+
Value = searchValue
125+
});
126+
shouldRebindGrid = true;
127+
}
128+
}
129+
130+
// Apply the changes in args.GridState.SearchFilter and rebind the Grid.
131+
if (shouldRebindGrid)
132+
{
133+
await GridRef!.SetStateAsync(args.GridState);
134+
}
135+
}
136+
}
137+
}
138+
139+
protected override void OnInitialized()
140+
{
141+
for (int i = 1; i <= 50; i++)
142+
{
143+
char nameCharCode = (char)(64 + i % 26 + 1);
144+
char descriptionCharCode = (char)(91 - i % 26 - 1);
145+
146+
GridData.Add(new SampleModel()
147+
{
148+
Id = i,
149+
Name = $"Name {i} {nameCharCode}{nameCharCode}",
150+
Description = $"Description {i} {descriptionCharCode}{descriptionCharCode}"
151+
});
152+
}
153+
}
154+
155+
public class SampleModel
156+
{
157+
public int Id { get; set; }
158+
public string Name { get; set; } = string.Empty;
159+
public string Description { get; set; } = string.Empty;
160+
}
161+
}
162+
````
163+
164+
## See Also
165+
166+
* [Search in Hidden Grid Columns]({%slug grid-kb-search-in-hidden-fields%})
167+
* [Grid State]({%slug grid-state%})
168+
* [Grid SearchBox]({%slug grid-searchbox%})

knowledge-base/grid-search-in-hidden-fields.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ If you want to search in the hidden fields of the Grid, do the following:
130130

131131
## See Also
132132

133+
* [Change Grid Search Results on Column Hide or Show]({%slug grid-kb-search-match-visible-columns%})
133134
* [Search the Grid in Numeric and Date Model Fields]({%slug grid-kb-search-numeric-fields%})
134135
* [Search the Grid on Button Click]({%slug grid-kb-search-button-click%})
135136
* [Search the Grid with a `StartsWith` operator]({%slug grid-kb-search-startswith%})

0 commit comments

Comments
 (0)