Skip to content

Commit 9a5471b

Browse files
xristianstefanovxristianstefanov
authored andcommitted
chore(Grid): apply last fixes
1 parent 5f0e9de commit 9a5471b

File tree

1 file changed

+32
-48
lines changed

1 file changed

+32
-48
lines changed

knowledge-base/grid-access-gridsearchbox-value.md

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,24 @@ I need to capture the text entered in the [`GridSearchBox`](slug:grid-searchbox)
2727

2828
## Solution
2929

30-
The `GridSearchBox` does not expose a direct `Value` property or provide two-way binding. It is integrated with the Grid's filtering system, automatically converting the search text into filters passed to the Grid. To capture the search text, extract it from the `DataSourceRequest.Filters` during the [`OnRead` event](slug:components/grid/manual-operations) processing. Follow these steps:
30+
The `GridSearchBox` does not expose a direct `Value` property or provide two-way binding. It is integrated with the Grid's filtering system, automatically converting the search text into filters passed to the Grid. To capture the search text, extract it from the Grid [state](slug:grid-state). Follow these steps:
3131

32-
1. Use the `OnRead` event of the Grid to access the current filters.
33-
2. Extract the search text from `DataSourceRequest.Filters`.
32+
1. Use the [`OnStateChanged` event](slug:grid-state#onstatechanged) of the Grid to access the current filters.
33+
2. Extract the search text from the component state.
3434

3535
Here is an example:
3636

3737
````Razor
3838
@using Telerik.DataSource
39-
@using Telerik.DataSource.Extensions
4039
41-
@( new MarkupString(output) )
40+
<TelerikButton OnClick="@GetSearchValue">Get Search Value</TelerikButton>
4241
43-
<br />
44-
<TelerikButton OnClick="@GetFilters">Get Filters</TelerikButton>
45-
46-
<TelerikGrid TItem="@Employee" OnRead="@ReadItems"
42+
<TelerikGrid @ref="GridRef"
43+
Data="@Employees"
4744
FilterMode="@GridFilterMode.FilterRow"
48-
Sortable="true" Pageable="true">
45+
Sortable="true"
46+
Pageable="true"
47+
OnStateChanged="@((GridStateEventArgs<Employee> args) => GridStateChanged(args))">
4948
<GridToolBar>
5049
<GridSearchBox Placeholder="Search by Name" />
5150
</GridToolBar>
@@ -56,59 +55,44 @@ Here is an example:
5655
</GridColumns>
5756
</TelerikGrid>
5857
59-
@code {
60-
private List<Employee> SourceData { get; set; }
61-
private string output { get; set; }
62-
private DataSourceRequest CurrentRequest { get; set; }
58+
<p>Current Search: @CurrentSearchValue</p>
6359
64-
private void GetFilters()
65-
{
66-
output = string.Empty;
60+
@code {
61+
private TelerikGrid<Employee> GridRef { get; set; }
62+
private string CurrentSearchValue { get; set; } = string.Empty;
6763
68-
foreach (var item in CurrentRequest.Filters)
69-
{
70-
if (item is FilterDescriptor) // filter row
71-
{
72-
FilterDescriptor currFilter = item as FilterDescriptor;
73-
output += $"field: {currFilter.Member}, operator {currFilter.Operator}, value: {currFilter.Value}<br />";
74-
}
64+
private List<Employee> Employees { get; set; } = new();
7565
76-
if (item is CompositeFilterDescriptor) // filter menu
77-
{
78-
CompositeFilterDescriptor currFilter = item as CompositeFilterDescriptor;
79-
output += $"START nested filter: logical operator: {currFilter.LogicalOperator}, details:<br />";
80-
81-
foreach (FilterDescriptor nestedFilter in currFilter.FilterDescriptors)
82-
{
83-
84-
output += $"field: {nestedFilter.Member}, operator {nestedFilter.Operator}, value: {nestedFilter.Value}<br />";
85-
}
86-
output += "END nested filter<br />";
87-
}
88-
}
66+
protected override void OnInitialized()
67+
{
68+
Employees = GenerateData();
8969
}
9070
91-
protected async Task ReadItems(GridReadEventArgs args)
71+
private void GridStateChanged(GridStateEventArgs<Employee> args)
9272
{
93-
CurrentRequest = args.Request; //store the current request for later use
94-
95-
await Task.Delay(1000); //simulate network delay from a real async call
96-
97-
var datasourceResult = SourceData.ToDataSourceResult(args.Request);
73+
// Capture search value whenever the grid state changes
74+
CurrentSearchValue = ExtractSearchValue(args.GridState);
75+
}
9876
99-
args.Data = datasourceResult.Data;
100-
args.Total = datasourceResult.Total;
77+
private void GetSearchValue()
78+
{
79+
// Or retrieve the current search value on demand
80+
var gridState = GridRef.GetState();
81+
CurrentSearchValue = ExtractSearchValue(gridState);
10182
}
10283
103-
protected override void OnInitialized()
84+
private string ExtractSearchValue(GridState<Employee> gridState)
10485
{
105-
SourceData = GenerateData();
86+
return (gridState.SearchFilter as CompositeFilterDescriptor)?
87+
.FilterDescriptors.OfType<FilterDescriptor>()
88+
.FirstOrDefault()?.Value?.ToString() ?? string.Empty;
10689
}
10790
10891
private List<Employee> GenerateData()
10992
{
11093
var result = new List<Employee>();
11194
var rand = new Random();
95+
11296
for (int i = 0; i < 100; i++)
11397
{
11498
result.Add(new Employee()
@@ -134,4 +118,4 @@ Here is an example:
134118
## See Also
135119

136120
* [Grid SearchBox](slug:grid-searchbox)
137-
* [Grid OnRead](slug:components/grid/manual-operations)
121+
* [Grid State](slug:grid-state)

0 commit comments

Comments
 (0)