Skip to content

Commit 147308e

Browse files
author
KB Bot
committed
Added new kb article grid-dynamically-updating-filtermenutemplate-value
1 parent 8351b9b commit 147308e

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
---
2+
title: FilterMenuTemplate Not Updating After Dynamic Value Change
3+
description: Learn how to refresh and update the displayed value within a FilterMenuTemplate when its value changes dynamically in Grid for Blazor.
4+
type: troubleshooting
5+
page_title: Refreshing FilterMenuTemplate to Reflect Dynamic Value Changes in Blazor Grid
6+
slug: grid-kb-dynamically-updating-filtermenutemplate-value
7+
tags: grid, blazor, filtermenutemplate, dynamic, update, refresh, value
8+
res_type: kb
9+
ticketid: 1677674
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>Grid for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
## Description
24+
25+
I have a custom `FilterMenuTemplate` for a column where a slider is shown. Although filtering works as expected, the span within the `FilterMenuTemplate` that is supposed to show the current selected value does not update when the slider value changes.
26+
27+
## Cause
28+
29+
The [`FilterMenuTemplate`](slug:grid-templates-filter#filter-menu-template) does not refresh automatically when the value bound to a control within it, such as a slider, changes dynamically.
30+
31+
## Solution
32+
33+
To resolve this issue, encapsulate the content of the `FilterMenuTemplate` in a separate Razor component. Then, refresh this component upon any value change in the Slider. This approach ensures that the displayed value updates dynamically to reflect the current selection. Follow the steps below to implement this solution:
34+
35+
1. Create a separate Razor component (for example, `CustomFilterMenu.razor`) that will contain the `FilterMenuTemplate` content, including the slider and the span displaying the selected value.
36+
37+
2. In your `CustomFilterMenu.razor`, define the necessary parameters and logic to handle the value change and update the display.
38+
39+
3. Use this component within the `FilterMenuTemplate` of the desired column in your Grid.
40+
41+
4. Ensure that any value change in the slider triggers the component's refresh to update the displayed value accordingly.
42+
43+
`````RAZOR
44+
@using Telerik.DataSource
45+
46+
<TelerikGrid Data="@GridData"
47+
FilterMode="@GridFilterMode.FilterMenu"
48+
Pageable="true"
49+
Width="600px">
50+
<GridColumns>
51+
<GridColumn Field="@(nameof(Product.Name))" Title="Product" Filterable="false" />
52+
<GridColumn Field="@(nameof(Product.Price))">
53+
<FilterMenuTemplate>
54+
<CustomPriceFilter SelectedPrice="@SelectedPrice"
55+
SelectedPriceChanged="@(value => SelectedPrice = value)"
56+
Context="context" />
57+
</FilterMenuTemplate>
58+
</GridColumn>
59+
60+
<GridColumn Field="@(nameof(Product.Size))">
61+
<FilterMenuTemplate>
62+
@foreach (var size in Sizes)
63+
{
64+
<div>
65+
<TelerikCheckBox Value="@(IsCheckboxInCurrentFilter(context.FilterDescriptor, size))"
66+
TValue="bool"
67+
ValueChanged="@((value) => UpdateCheckedSizes(value, size, context))"
68+
Id="@($"size_{size}")">
69+
</TelerikCheckBox>
70+
<label for="@($"size_{size}")">
71+
@if (size == null) // part of handling nulls - show meaningful text for the end user
72+
{
73+
<text>Empty</text>
74+
}
75+
else
76+
{
77+
@size
78+
}
79+
</label>
80+
</div>
81+
}
82+
</FilterMenuTemplate>
83+
</GridColumn>
84+
</GridColumns>
85+
</TelerikGrid>
86+
87+
@code {
88+
private List<Product> GridData { get; set; }
89+
90+
private string[] Sizes = new string[] { "XS", "S", "M", "L", "XL", null };
91+
92+
private double SelectedPrice { get; set; }
93+
94+
private void OnPriceChanged(double value, FilterMenuTemplateContext context)
95+
{
96+
SelectedPrice = value;
97+
context.FilterDescriptor.FilterDescriptors.Clear();
98+
context.FilterDescriptor.FilterDescriptors.Add(new FilterDescriptor(nameof(Product.Price), FilterOperator.IsGreaterThanOrEqualTo, value));
99+
}
100+
101+
private bool IsCheckboxInCurrentFilter(CompositeFilterDescriptor filterDescriptor, string size)
102+
{
103+
if (size == null)
104+
{
105+
foreach (FilterDescriptor item in filterDescriptor.FilterDescriptors)
106+
{
107+
if (item.Operator == FilterOperator.IsNull)
108+
{
109+
return true;
110+
}
111+
}
112+
return false;
113+
}
114+
return filterDescriptor.FilterDescriptors.Select(f => (f as FilterDescriptor).Value?.ToString()).ToList().Contains(size);
115+
}
116+
117+
private void UpdateCheckedSizes(bool isChecked, string itemValue, FilterMenuTemplateContext context)
118+
{
119+
var compositeFilterDescriptor = context.FilterDescriptor;
120+
compositeFilterDescriptor.LogicalOperator = FilterCompositionLogicalOperator.Or;
121+
122+
if (!isChecked)
123+
{
124+
compositeFilterDescriptor.FilterDescriptors.Remove(compositeFilterDescriptor.FilterDescriptors.First(x =>
125+
{
126+
var fd = x as FilterDescriptor;
127+
if ((fd.Operator == FilterOperator.IsNull && itemValue == null) ||
128+
(fd.Operator == FilterOperator.IsEqualTo && fd.Value?.ToString() == itemValue))
129+
{
130+
return true;
131+
}
132+
else
133+
{
134+
return false;
135+
}
136+
}));
137+
}
138+
else
139+
{
140+
compositeFilterDescriptor.FilterDescriptors.Add(new FilterDescriptor()
141+
{
142+
Member = nameof(Product.Size),
143+
MemberType = typeof(string),
144+
Operator = itemValue == null ? FilterOperator.IsNull : FilterOperator.IsEqualTo,
145+
Value = itemValue
146+
});
147+
}
148+
}
149+
150+
protected override void OnInitialized()
151+
{
152+
GridData = Enumerable.Range(1, 70).Select(x => new Product
153+
{
154+
Id = x,
155+
Size = Sizes[x % Sizes.Length],
156+
Name = $"Product {x}",
157+
Price = x
158+
}).ToList();
159+
160+
base.OnInitialized();
161+
}
162+
}
163+
`````
164+
`````RAZOR CustomPriceFilter.razor
165+
@using Telerik.DataSource
166+
167+
<TelerikStackLayout HorizontalAlign="@StackLayoutHorizontalAlign.Center" Orientation="StackLayoutOrientation.Vertical">
168+
<span>@SelectedPrice</span>
169+
<TelerikSlider ShowButtons="false"
170+
TickPosition="SliderTickPosition.None"
171+
Min="0"
172+
Max="100"
173+
Width="100%"
174+
LargeStep="10"
175+
SmallStep="1"
176+
Value="@SelectedPrice"
177+
ValueChanged="@((double v) => OnPriceChanged(v))">
178+
</TelerikSlider>
179+
</TelerikStackLayout>
180+
181+
@code {
182+
[Parameter] public double SelectedPrice { get; set; }
183+
[Parameter] public EventCallback<double> SelectedPriceChanged { get; set; }
184+
[Parameter] public FilterMenuTemplateContext Context { get; set; } = null!;
185+
186+
private async Task OnPriceChanged(double newValue)
187+
{
188+
SelectedPrice = newValue;
189+
Context.FilterDescriptor.FilterDescriptors.Clear();
190+
Context.FilterDescriptor.FilterDescriptors.Add(new FilterDescriptor(nameof(Product.Price), FilterOperator.IsGreaterThanOrEqualTo, newValue));
191+
await SelectedPriceChanged.InvokeAsync(newValue);
192+
// await Context.FilterAsync();
193+
}
194+
}
195+
`````
196+
`````RAZOR Product.cs
197+
public class Product
198+
{
199+
public int Id { get; set; }
200+
public string? Name { get; set; }
201+
public string Size { get; set; }
202+
public double Price { get; set; }
203+
}
204+
`````
205+
## See Also
206+
207+
- [Grid Overview](slug:grid-overview)
208+
- [Filtering in Grid](slug:components/grid/filtering)

0 commit comments

Comments
 (0)