Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/grid/filter/searchbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ The `GridSearchBox` component offers the following settings to customize its beh

* `DebounceDelay` - the time in `ms` with which the typing is debounced. This provides a performance optimization when using the `OnRead` event - filtering does not happen on every keystroke anymore. The default value is `300`.

* `Fields` - a list of `string` that denotes the fields names that the grid should search in. By default, the grid looks in all string fields in its currently visible columns, and you can define a subset of that.
* `Fields` - a `List<string>` that includes the fields names that the Grid should search in. By default, the Grid searches in all string fields, which are bound to visible columns. You can only define a subset of those fields. It is also possible to programmatically [search in string fields, which are not displayed in the Grid]({%slug grid-kb-search-in-hidden-fields%}).

* `Class` - a CSS class rendered on the wrapper of the searchbox so you can customize its appearance.

Expand Down
115 changes: 115 additions & 0 deletions knowledge-base/grid-search-in-hidden-fields.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
title: Search in hidden fields of the Grid
description: How to search in hidden fields of the Grid?
type: how-to
page_title: Search in hidden fields of the Grid
slug: grid-kb-search-in-hidden-fields
position:
tags: telerik,blazor,grid,search,searchbox,hidden,field,column,not,visible
ticketid: 1540910
res_type: kb
---

## Environment
<table>
<tbody>
<tr>
<td>Product</td>
<td>Grid for Blazor</td>
</tr>
</tbody>
</table>


## Description

I want to customize the Grid Toolbar Searchbox, so it also searches in the hidden fields of the Grid and not only in the visible ones. How to achieve that?

## Solution

By default, the Grid looks in all string fields in its currently visible columns. You can [customize the SearchBox]({%slug grid-searchbox%}#customize-the-searchbox), so the Grid will search only in certain columns. However, the search will still be based on the **visible** fields provided to the `Fields` parameter of the `GridSearchBox`.

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

* Use the Grid with an [OnRead handler]({%slug components/grid/manual-operations%}).
* In the OnRead handler, [check if there is a filter applied]({%slug components/grid/manual-operations%}#get-information-from-the-datasourcerequest).
* 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.
* Obtain the search string from the `Value` property of any of the descriptors in the above collection.
* Add one additional `FilterDescriptor` to the above collection that targets the hidden column.

Here is an example:

````CSHTML
@using Telerik.DataSource.Extensions
@using Telerik.DataSource

<TelerikGrid Data="@CurrentGridData" TotalCount="@CurrentGridTotalCount"
OnRead="@GridReadHandler"
FilterMode="@GridFilterMode.FilterRow">
<GridToolBar>
<strong style="color:#900">Search for "secret#", where # is the ID number:</strong>
<span class="k-toolbar-spacer"></span>
<GridSearchBox DebounceDelay="200"></GridSearchBox>
</GridToolBar>
<GridColumns>
<GridColumn Field="@nameof(GridItem.ID)" />
<GridColumn Field="@nameof(GridItem.Name)" />
<GridColumn Field="@nameof(GridItem.Secret)" Visible="false" />
</GridColumns>
</TelerikGrid>

@code {

private int CurrentGridTotalCount { get; set; }

public List<GridItem> GridData { get; set; } = new List<GridItem>();
public List<GridItem> CurrentGridData { get; set; } = new List<GridItem>();

protected override Task OnInitializedAsync()
{
for (int j = 1; j <= 9; j++)
{
GridData.Add(new GridItem() { ID = j, Name = "Name " + j, Secret = "Secret" + j });
}

return base.OnInitializedAsync();
}

private async Task GridReadHandler(GridReadEventArgs args)
{
// check if we have any filtering at all
if (args.Request.Filters.Count > 0)
{
foreach (var rootFilterDescriptor in args.Request.Filters)
{
// row filters are FilterDescriptors; search and menu filters are CompositeFilterDescriptors
if (rootFilterDescriptor.GetType() == typeof(CompositeFilterDescriptor))
{
var searchDescriptor = rootFilterDescriptor as CompositeFilterDescriptor;
var searchString = (searchDescriptor.FilterDescriptors[0] as FilterDescriptor).Value.ToString();

// add a descriptor for each hidden column that you want to search in
searchDescriptor.FilterDescriptors.Add(new FilterDescriptor()
{
Member = "Secret",
MemberType = typeof(string),
Value = searchString,
Operator = FilterOperator.Contains
});
}
}
}

var result = GridData.ToDataSourceResult(args.Request);
CurrentGridData = (result.Data as IEnumerable<GridItem>).ToList();
CurrentGridTotalCount = result.Total;
}

public class GridItem
{
public int ID { get; set; }
public string Name { get; set; }
public string Secret { get; set; }
}
}
````