Skip to content

Commit 9a8ddd3

Browse files
kb(grid): own filtering sample for throttling operations KB
1 parent 12dc9db commit 9a8ddd3

File tree

1 file changed

+79
-1
lines changed

1 file changed

+79
-1
lines changed

knowledge-base/grid-kb-throttle-operations.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ I want to specify a debounce time for filtering. This way I can (for example) se
2727
This can be useful for filtering with remote data when using the FilterRow mode - it invokes a filter on every keystroke.
2828

2929
## Solution
30-
There are two solutions to this:
30+
There are three ideas on the basic approach how to do this:
3131

3232
* Use the [FilterMenu](https://demos.telerik.com/blazor-ui/grid/filter-menu) filtering mode because it fires filtering requests only when the user presses a button.
3333

@@ -37,6 +37,8 @@ There are two solutions to this:
3737

3838
* Implement the desired throttling/debouncing in the [OnRead event](https://docs.telerik.com/blazor-ui/components/grid/manual-operations). Below is an example of this.
3939

40+
* Implement your own filtering (a second example is available below).
41+
4042

4143
>caption Throttle grid data source requests
4244
@@ -143,3 +145,79 @@ for example, whether the filters changed or something else, so you can throttle
143145
}
144146
````
145147

148+
>caption Own filtering in the grid (header template is used until a [filter template](https://feedback.telerik.com/blazor/1407773-custom-filter-components-filter-template) becomes available)
149+
150+
````CSHTML
151+
<style>
152+
.block-headers-with-sorting th.k-header .k-icon.k-i-sort-asc-sm,
153+
.block-headers-with-sorting th.k-header .k-icon.k-i-sort-desc-sm {
154+
position: absolute;
155+
right: 0;
156+
top: 8px;
157+
}
158+
</style>
159+
160+
<TelerikGrid Data="@MyData" Height="300px" Pageable="true" Sortable="true" FilterMode="@GridFilterMode.None" Class="block-headers-with-sorting">
161+
<GridColumns>
162+
<GridColumn Field="@(nameof(SampleData.ID))">
163+
<HeaderTemplate>
164+
<div>
165+
<div style="margin-bottom: .5rem">Id</div>
166+
<input style="width: 100%;" class="k-textbox"
167+
@onclick:stopPropagation
168+
@oninput="@( (ChangeEventArgs e) => UserFilters((string)e.Value, "ID") )" />
169+
</div>
170+
</HeaderTemplate>
171+
</GridColumn>
172+
<GridColumn Field="@(nameof(SampleData.Name))">
173+
<HeaderTemplate>
174+
<div>
175+
<div style="margin-bottom: .5rem">Name</div>
176+
<input style="width: 100%;" class="k-textbox"
177+
@onclick:stopPropagation
178+
@oninput="@( (ChangeEventArgs e) => UserFilters((string)e.Value, "Name") )" />
179+
</div>
180+
</HeaderTemplate>
181+
</GridColumn>
182+
<GridColumn Field="HireDate" Width="350px">
183+
<HeaderTemplate>
184+
<div>
185+
<div style="margin-bottom: .5rem">Hire Date</div>
186+
<input style="width: 100%;" class="k-textbox"
187+
@onclick:stopPropagation
188+
@oninput="@( (ChangeEventArgs e) => UserFilters((string)e.Value, "HireDate") )" />
189+
</div>
190+
</HeaderTemplate>
191+
</GridColumn>
192+
</GridColumns>
193+
</TelerikGrid>
194+
195+
@operationsList
196+
197+
@code {
198+
MarkupString operationsList { get; set; }
199+
200+
void UserFilters(string input, string field)
201+
{
202+
// do debouncing and filtering of the grid data (MyData in this sample) here
203+
// see the previous snippet on a way to implement throttling
204+
operationsList = new MarkupString($"{operationsList}<br />filter string: {input}, field: {field}");
205+
StateHasChanged();
206+
}
207+
208+
public class SampleData
209+
{
210+
public int ID { get; set; }
211+
public string Name { get; set; }
212+
public DateTime HireDate { get; set; }
213+
}
214+
215+
public IEnumerable<SampleData> MyData = Enumerable.Range(1, 50).Select(x => new SampleData
216+
{
217+
ID = x,
218+
Name = "name " + x,
219+
HireDate = DateTime.Now.AddDays(-x)
220+
});
221+
}
222+
````
223+

0 commit comments

Comments
 (0)