Skip to content

Commit 3c4c7c3

Browse files
docs(grid): Update Grid search in hidden fields KB (#1303)
Co-authored-by: Dimo Dimov <[email protected]>
1 parent b46c25e commit 3c4c7c3

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

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

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ type: how-to
55
page_title: Search in hidden fields of the Grid
66
slug: grid-kb-search-in-hidden-fields
77
position:
8-
tags: telerik,blazor,grid,search,searchbox,hidden,field,column,not,visible
8+
tags: telerik, blazor, grid, search, searchbox
99
ticketid: 1540910
1010
res_type: kb
1111
---
1212

1313
## Environment
14+
1415
<table>
15-
<tbody>
16-
<tr>
17-
<td>Product</td>
18-
<td>Grid for Blazor</td>
19-
</tr>
20-
</tbody>
16+
<tbody>
17+
<tr>
18+
<td>Product</td>
19+
<td>Grid for Blazor</td>
20+
</tr>
21+
</tbody>
2122
</table>
2223

2324

@@ -31,24 +32,24 @@ By default, the Grid looks in all string fields in its currently visible columns
3132

3233
If you want to search in the hidden fields of the Grid, do the following:
3334

34-
* Use the Grid with an [OnRead handler]({%slug components/grid/manual-operations%}).
35-
* In the OnRead handler, [check if there is a filter applied]({%slug components/grid/manual-operations%}#get-information-from-the-datasourcerequest).
36-
* The applied filter must be of type [CompositeFilterDescriptor](https://docs.telerik.com/blazor-ui/api/Telerik.DataSource.CompositeFilterDescriptor). Plain `FilterDescriptors` at root level (`args.Request.Filters`) are generated by the filter row. The composite filter descriptor has a `FilterDescriptors` property, which holds a collection plain [single-field FilterDescriptors](https://docs.telerik.com/blazor-ui/api/Telerik.DataSource.FilterDescriptor). Each of them targets one of the visible columns.
37-
* Obtain the search string from the `Value` property of any of the descriptors in the above collection.
38-
* Add one additional `FilterDescriptor` to the above collection that targets the hidden column.
35+
* Bind the Grid with an [`OnRead` event handler]({%slug common-features-data-binding-onread%}).
36+
* In the `OnRead` handler, [check if there is a filter applied]({%slug components/grid/manual-operations%}#get-information-from-the-datasourcerequest) in `args.Request.Filters`.
37+
* The applied filters are of type [`CompositeFilterDescriptor`](https://docs.telerik.com/blazor-ui/api/Telerik.DataSource.CompositeFilterDescriptor). The composite filter descriptor has a `FilterDescriptors` property, which holds a collection plain [single-field `FilterDescriptor`s](https://docs.telerik.com/blazor-ui/api/Telerik.DataSource.FilterDescriptor). Each of the `FilterDescriptor` in the search descriptor targets one of the visible columns.
38+
* Obtain the search string from the `SearchFilter` property of the Grid state. It holds a `CompositeFilterDescriptor` too.
39+
* Add one additional `FilterDescriptor` to the search `CompositeFilterDescriptor` for every hidden column.
3940

40-
Here is an example:
41+
>caption Search in hidden Grid columns
4142
4243
````CSHTML
4344
@using Telerik.DataSource.Extensions
4445
@using Telerik.DataSource
4546
46-
<TelerikGrid TItem="@GridItem"
47-
OnRead="@GridReadHandler"
47+
<TelerikGrid @ref="@GridRef"
48+
TItem="@GridItem"
49+
OnRead="@OnGridRead"
4850
FilterMode="@GridFilterMode.FilterRow">
4951
<GridToolBarTemplate>
5052
<strong style="color:#900">Search for "secret#", where # is the ID number:</strong>
51-
<span class="k-toolbar-spacer"></span>
5253
<GridSearchBox DebounceDelay="200"></GridSearchBox>
5354
</GridToolBarTemplate>
5455
<GridColumns>
@@ -59,40 +60,57 @@ Here is an example:
5960
</TelerikGrid>
6061
6162
@code {
62-
public List<GridItem> GridData { get; set; } = new List<GridItem>();
63+
private TelerikGrid<GridItem> GridRef { get; set; }
64+
65+
private List<GridItem> GridData { get; set; }
6366
64-
private async Task GridReadHandler(GridReadEventArgs args)
67+
private async Task OnGridRead(GridReadEventArgs args)
6568
{
6669
// check if we have any filtering at all
6770
if (args.Request.Filters.Count > 0)
6871
{
69-
foreach (var rootFilterDescriptor in args.Request.Filters)
72+
// get the search string from the Grid state
73+
var searchDescriptor = (CompositeFilterDescriptor)GridRef.GetState().SearchFilter;
74+
var searchString = (searchDescriptor?.FilterDescriptors[0] as FilterDescriptor)?.Value.ToString();
75+
76+
if (!string.IsNullOrEmpty(searchString))
7077
{
71-
// row filters are FilterDescriptors; search and menu filters are CompositeFilterDescriptors
72-
if (rootFilterDescriptor.GetType() == typeof(CompositeFilterDescriptor))
78+
// locate the search filter descriptor among all others in the request
79+
// if Grid filtering is disabled, then args.Request.Filters will contain only the search descriptor
80+
foreach (var rootFilterDescriptor in args.Request.Filters)
7381
{
74-
var searchDescriptor = rootFilterDescriptor as CompositeFilterDescriptor;
75-
var searchString = (searchDescriptor.FilterDescriptors[0] as FilterDescriptor).Value.ToString();
82+
// in versions 3.x: row filters are FilterDescriptors; search and menu filters are CompositeFilterDescriptors
83+
// in versions 4.x: all filters are CompositeFilterDescriptors
84+
var filterDescriptor = rootFilterDescriptor as CompositeFilterDescriptor;
85+
var filterString = (searchDescriptor.FilterDescriptors[0] as FilterDescriptor).Value.ToString();
7686
77-
// add a descriptor for each hidden column that you want to search in
78-
searchDescriptor.FilterDescriptors.Add(new FilterDescriptor()
87+
if (searchString == filterString)
7988
{
80-
Member = "Secret",
81-
MemberType = typeof(string),
82-
Value = searchString,
83-
Operator = FilterOperator.Contains
84-
});
89+
// add a descriptor for each hidden column that you want to search in
90+
filterDescriptor.FilterDescriptors.Add(new FilterDescriptor()
91+
{
92+
Member = "Secret",
93+
MemberType = typeof(string),
94+
Value = searchString,
95+
Operator = FilterOperator.Contains
96+
});
97+
98+
break;
99+
}
85100
}
86101
}
87102
}
88103
89104
var result = GridData.ToDataSourceResult(args.Request);
105+
90106
args.Data = result.Data;
91107
args.Total = result.Total;
92108
}
93109
94110
protected override Task OnInitializedAsync()
95111
{
112+
GridData = new List<GridItem>();
113+
96114
for (int j = 1; j <= 10; j++)
97115
{
98116
GridData.Add(new GridItem() { ID = j, Name = "Name " + j, Secret = "Secret" + j });

0 commit comments

Comments
 (0)