Skip to content

Commit 56aa921

Browse files
docs(grid): filter menu template example 2
1 parent 57fd894 commit 56aa921

File tree

2 files changed

+267
-2
lines changed

2 files changed

+267
-2
lines changed

components/grid/templates/filter.md

Lines changed: 267 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,18 @@ There are two different templates you can use depending on the [Filter Mode]({%s
2121

2222
## Filter Menu
2323

24-
By default, the filter menu contains two filter values that are tied with a logical operator - OR or AND. The filter template for it (`<FilterMenuTemplate>` under the corresponding `<GridColumn>`) provides you with the default composite filter in its `context`, and the `Filter` and `Clear` buttons below the template.
24+
By default, the filter menu contains two filter values that are tied with a logical operator - OR or AND. The filter template for it (`<FilterMenuTemplate>` under the corresponding `<GridColumn>`) provides you with the default composite filter in the `FilterDescriptor` field of its `context`, and the `Filter` and `Clear` buttons below the template.
25+
26+
You can get started from the following examples:
27+
28+
<!-- Start Document Outline -->
29+
30+
* [Basic Template - Single Filter Operator](#basic-template---single-filter-operator)
31+
* [Add A Third Filter Operator](#add-a-third-filter-operator)
32+
33+
34+
35+
### Basic Template - Single Filter Operator
2536

2637
In the example below, you can see how to:
2738

@@ -32,7 +43,7 @@ In the example below, you can see how to:
3243

3344
Comments in the code offer more insights into how all the features tie together.
3445

35-
>caption Customize Filter Menu operators and value area
46+
>caption Customize Filter Menu operators and value area to use only one operator
3647
3748
````CSHTML
3849
@using Telerik.DataSource
@@ -135,6 +146,260 @@ Comments in the code offer more insights into how all the features tie together.
135146
136147
![Custom filter menu template](images/filter-menu-template-basic.png)
137148

149+
### Add A Third Filter Operator
150+
151+
In the example below, you can see how to:
152+
153+
* mimic the default behavior (by implementing the default functionality)
154+
* add a third operator (by adding another instance of the filter operator editor)
155+
* provide custom filter operators lists and texts (through the data sources of the custom dropdowns)
156+
157+
Comments in the code offer more insights into how all the features tie together.
158+
159+
>caption Add a third filter operator
160+
161+
````CSHTML
162+
@using Telerik.DataSource
163+
164+
<TelerikGrid Data="@GridData" Pageable="true" Width="300px"
165+
FilterMode="@GridFilterMode.FilterMenu">
166+
<GridColumns>
167+
<GridColumn Field="@nameof(SampleData.UnitsInStock)">
168+
<FilterMenuTemplate>
169+
@{
170+
UnitsInStockFilterMenuTemplateContext = context;
171+
172+
var compositeFilterDescriptor = UnitsInStockFilterMenuTemplateContext.FilterDescriptor as CompositeFilterDescriptor;
173+
174+
var descriptor1 = compositeFilterDescriptor.FilterDescriptors.ElementAtOrDefault(0) as FilterDescriptor;
175+
176+
var descriptor3 = new FilterDescriptor()
177+
{
178+
Member = descriptor1.Member,
179+
MemberType = descriptor1.MemberType,
180+
};
181+
182+
UnitsInStockFilterMenuTemplateContext.FilterDescriptor.FilterDescriptors.Add(descriptor3);
183+
}
184+
185+
@* first filter logic *@
186+
<div>
187+
<TelerikDropDownList Data="@UnitsInStockFilterOperators"
188+
@bind-Value="@UnitsInStockFilterOperator1"
189+
TextField="@(nameof(FilterOperatorDescriptor.Text))"
190+
ValueField="@(nameof(FilterOperatorDescriptor.Operator))"
191+
PopupHeight="auto">
192+
</TelerikDropDownList>
193+
</div>
194+
<div>
195+
<TelerikNumericTextBox @bind-Value="@UnitsInStockFilterValue1"></TelerikNumericTextBox>
196+
</div>
197+
198+
@* logical operator *@
199+
<div>
200+
<TelerikDropDownList @bind-Value="@UnitsInStockFilterLogicalOperator1"
201+
Data="@FilterLogicalOperators"
202+
Width="75px" PopupHeight="auto"
203+
TextField="@nameof(FilterLogicalOperatorDescriptor.Text)"
204+
ValueField="@nameof(FilterLogicalOperatorDescriptor.Operator)">
205+
</TelerikDropDownList>
206+
</div>
207+
208+
@* second filter logic *@
209+
<div>
210+
<TelerikDropDownList Data="@UnitsInStockFilterOperators"
211+
@bind-Value="@UnitsInStockFilterOperator2"
212+
TextField="@(nameof(FilterOperatorDescriptor.Text))"
213+
ValueField="@(nameof(FilterOperatorDescriptor.Operator))"
214+
PopupHeight="auto">
215+
</TelerikDropDownList>
216+
</div>
217+
<div>
218+
<TelerikNumericTextBox @bind-Value="@UnitsInStockFilterValue2"></TelerikNumericTextBox>
219+
</div>
220+
221+
@* third filter logic *@
222+
<div>
223+
<TelerikDropDownList Data="@UnitsInStockFilterOperators"
224+
@bind-Value="@UnitsInStockFilterOperator3"
225+
TextField="@(nameof(FilterOperatorDescriptor.Text))"
226+
ValueField="@(nameof(FilterOperatorDescriptor.Operator))"
227+
PopupHeight="auto">
228+
</TelerikDropDownList>
229+
</div>
230+
<div>
231+
<TelerikNumericTextBox @bind-Value="@UnitsInStockFilterValue3"></TelerikNumericTextBox>
232+
</div>
233+
</FilterMenuTemplate>
234+
</GridColumn>
235+
<GridColumn Field="@nameof(SampleData.ProductId)" Filterable="false" />
236+
</GridColumns>
237+
</TelerikGrid>
238+
239+
@code {
240+
public List<SampleData> GridData { get; set; } = Enumerable.Range(1, 50).Select(x => new SampleData { ProductId = x, UnitsInStock = x % 10 }).ToList();
241+
242+
// data sources for the custom filter dropdowns
243+
public List<FilterLogicalOperatorDescriptor> FilterLogicalOperators { get; set; }
244+
public List<FilterOperatorDescriptor> UnitsInStockFilterOperators { get; set; }
245+
246+
// the filter template context that lets you customize its filter operators list
247+
public FilterMenuTemplateContext UnitsInStockFilterMenuTemplateContext { get; set; }
248+
249+
// get the filter descriptor list from the context
250+
public CompositeFilterDescriptor UnitsInStockCompositeFilterDescriptor
251+
{
252+
get => UnitsInStockFilterMenuTemplateContext.FilterDescriptor;
253+
}
254+
255+
// custom filter operators- they use two-way binding with the customized components in the template
256+
public FilterOperator UnitsInStockFilterOperator1
257+
{
258+
get => UnitsInStockFilterDescriptor1.Operator;
259+
set => UnitsInStockFilterDescriptor1.Operator = value;
260+
}
261+
262+
public FilterOperator UnitsInStockFilterOperator2
263+
{
264+
get => UnitsInStockFilterDescriptor2.Operator;
265+
set => UnitsInStockFilterDescriptor2.Operator = value;
266+
}
267+
268+
public FilterOperator UnitsInStockFilterOperator3
269+
{
270+
get => UnitsInStockFilterDescriptor3.Operator;
271+
set => UnitsInStockFilterDescriptor3.Operator = value;
272+
}
273+
274+
// custom filter values - they use two-way binding with the customized components in the template
275+
public short? UnitsInStockFilterValue1
276+
{
277+
get => (short?)(UnitsInStockFilterDescriptor1.Value);
278+
set => UnitsInStockFilterDescriptor1.Value = (short?)value;
279+
}
280+
281+
public short? UnitsInStockFilterValue2
282+
{
283+
get => (short?)(UnitsInStockFilterDescriptor2.Value);
284+
set => UnitsInStockFilterDescriptor2.Value = (short?)value;
285+
}
286+
287+
public short? UnitsInStockFilterValue3
288+
{
289+
get => (short?)(UnitsInStockFilterDescriptor3.Value);
290+
set => UnitsInStockFilterDescriptor3.Value = (short?)value;
291+
}
292+
293+
// logical operator value
294+
public FilterCompositionLogicalOperator UnitsInStockFilterLogicalOperator1
295+
{
296+
get => UnitsInStockCompositeFilterDescriptor.LogicalOperator;
297+
set => UnitsInStockCompositeFilterDescriptor.LogicalOperator = value;
298+
}
299+
300+
// shortcuts to get the filter descriptors
301+
public FilterDescriptor UnitsInStockFilterDescriptor1 => GetUnitsInStockFilterDescriptor(0);
302+
303+
public FilterDescriptor UnitsInStockFilterDescriptor2 => GetUnitsInStockFilterDescriptor(1);
304+
305+
public FilterDescriptor UnitsInStockFilterDescriptor3 => GetUnitsInStockFilterDescriptor(2);
306+
307+
public FilterDescriptor GetUnitsInStockFilterDescriptor(int index)
308+
{
309+
var unitsInStockFilter = UnitsInStockFilterMenuTemplateContext.FilterDescriptor;
310+
var descriptor = unitsInStockFilter.FilterDescriptors.ElementAtOrDefault(index) as FilterDescriptor;
311+
return descriptor;
312+
}
313+
314+
315+
// initialize data for the filter operators and logical operators lists
316+
protected override async Task OnInitializedAsync()
317+
{
318+
// filter operators
319+
UnitsInStockFilterOperators = GetNumericFilterOperators();
320+
// logical operators
321+
FilterLogicalOperators = GetFilterLogicalOperators();
322+
}
323+
324+
325+
326+
// model class for the custom dropdowns with the filter operators
327+
public class FilterOperatorDescriptor
328+
{
329+
public string Text { get; set; } = "Is equal to";
330+
331+
public FilterOperator Operator { get; set; } = FilterOperator.IsEqualTo;
332+
}
333+
334+
// model class for the dropdowns with logical operators list
335+
public class FilterLogicalOperatorDescriptor
336+
{
337+
public string Text { get; set; }
338+
339+
public FilterCompositionLogicalOperator Operator { get; set; }
340+
}
341+
342+
// provide a list with the custom logical operators to the template
343+
public List<FilterLogicalOperatorDescriptor> GetFilterLogicalOperators()
344+
{
345+
var data = new List<FilterLogicalOperatorDescriptor>();
346+
347+
data.Add(new FilterLogicalOperatorDescriptor()
348+
{
349+
Text = "- OR -",
350+
Operator = FilterCompositionLogicalOperator.Or
351+
});
352+
353+
data.Add(new FilterLogicalOperatorDescriptor()
354+
{
355+
Text = "- AND -",
356+
Operator = FilterCompositionLogicalOperator.And
357+
});
358+
359+
return data;
360+
}
361+
362+
// provide a list with the filter operators to the template
363+
public List<FilterOperatorDescriptor> GetNumericFilterOperators()
364+
{
365+
var data = new List<FilterOperatorDescriptor>();
366+
367+
data.Add(new FilterOperatorDescriptor()
368+
{
369+
Text = "- DIFFERENT FROM -",
370+
Operator = FilterOperator.IsNotEqualTo
371+
});
372+
373+
data.Add(new FilterOperatorDescriptor()
374+
{
375+
Text = "- LESS THAN -",
376+
Operator = FilterOperator.IsLessThan
377+
});
378+
379+
data.Add(new FilterOperatorDescriptor()
380+
{
381+
Text = "- GREATER THAN -",
382+
Operator = FilterOperator.IsGreaterThan
383+
});
384+
385+
return data;
386+
}
387+
388+
// sample model for the grid
389+
public class SampleData
390+
{
391+
public int ProductId { get; set; }
392+
public int UnitsInStock { get; set; }
393+
}
394+
}
395+
````
396+
397+
>caption The result from the code snippet above, after opening the filter menu and applying some filteres
398+
399+
![Three filteres in the filter menu](images/filter-menu-template-three-operators.png)
400+
401+
402+
138403

139404
## See Also
140405

14.1 KB
Loading

0 commit comments

Comments
 (0)