Skip to content

Commit 22c767a

Browse files
svdimitryordan-mitevdimodi
authored
Filtermenu buttons template (#1532)
* docs(grid): docs on filter menu buttons template * docs(grid): docs on filter menu buttons template * Update components/grid/templates/filter.md Co-authored-by: Yordan <[email protected]> * Update components/grid/templates/filter.md Co-authored-by: Yordan <[email protected]> * Update components/grid/templates/filter.md Co-authored-by: Yordan <[email protected]> * Update components/grid/templates/filter.md Co-authored-by: Yordan <[email protected]> * Update components/grid/templates/filter.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/grid/templates/filter.md Co-authored-by: Dimo Dimov <[email protected]> --------- Co-authored-by: Yordan <[email protected]> Co-authored-by: Dimo Dimov <[email protected]>
1 parent 1365130 commit 22c767a

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

components/grid/templates/filter.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ There are two different templates you can use depending on the [Filter Mode]({%s
1717

1818
* [Filter Row Template](#filter-row-template)
1919
* [Filter Menu Template](#filter-menu-template)
20+
* [Filter Menu Buttons Template](#filter-menu-buttons-template)
2021

2122

2223
## Filter Row Template
@@ -165,6 +166,8 @@ To customize the filter menu, use the `<FilterMenuTemplate>` tag of the `<GridCo
165166
The template receives a `context` of type `FilterMenuTemplateContext` that provides the following members:
166167

167168
* `FilterDescriptor` - the object that describes the column filter. By default it has two filters with the type and name of the field, and you can add more to its `FilterDescriptors` collection, or change its `LogicalOperator` from the default `AND`.
169+
* `FilterAsync` - applies the defined filters in the Filter Menu to the Grid component.
170+
* `ClearFilterAsync` - clears the applied filters.
168171

169172
You can store a reference to each column's context in a field in the view-model, so you can reference it from event handlers in the standard C# code, instead of passing it as a nargument to lambdas in the markup only. You can also pass the context as a Parameter to your own separate filter component to reduce clutter in the main grid markup and code.
170173

@@ -305,6 +308,100 @@ For an example with the CheckboxList Filter, see the [Custom Data]({%slug grid-c
305308
306309
![Custom Filter Menu Template with Checkboxes](images/custom-filter-menu-checkboxes.png)
307310

311+
## Filter Menu Buttons Template
312+
313+
By default, the Filter Menu renders `Filter` and `Clear` buttons. You can customize or remove them entirely by using the `FilterMenuButtonsTemplate` tag.
314+
315+
The template receives a `context` of type `FilterMenuTemplateContext` that provides the following members:
316+
317+
* `FilterDescriptor`—the object that describes the column filter. By default, the column filter has two filters: one for the type and another for the name of the field. You can modify the column filter by:
318+
* Adding more filters to the `FilterDescriptors` collection.
319+
* Changing the `LogicalOperator` (`AND` by default).
320+
* Using the `FilterDescriptor` to create a custom button that applies a predefined filter.
321+
* `FilterAsync`—applies the filters defined in the Filter Menu to the Grid component.
322+
* `ClearFilterAsync`—clears the applied filters.
323+
324+
>caption Using custom filter menu buttons
325+
326+
````CSHTML
327+
@* Customize the buttons in the Filter Menu
328+
329+
@using Telerik.DataSource
330+
331+
<TelerikGrid Data="@GridData"
332+
Pageable="true"
333+
Sortable="true"
334+
FilterMode="@GridFilterMode.FilterMenu">
335+
<GridColumns>
336+
<GridColumn Field="Name" Title="Product Name" />
337+
<GridColumn Field="Price">
338+
<FilterMenuButtonsTemplate Context="filterContext">
339+
<TelerikButton OnClick="@(async _ => await filterContext.FilterAsync())">Filter </TelerikButton>
340+
<TelerikButton OnClick="@(() => SetPredefinedFilterAsync(filterContext))">Select >= 55</TelerikButton>
341+
<TelerikButton OnClick="@(() => ClearFilterAsync(filterContext))">Clear</TelerikButton>
342+
</FilterMenuButtonsTemplate>
343+
</GridColumn>
344+
<GridColumn Field="@nameof(Product.Released)" />
345+
<GridColumn Field="@nameof(Product.Discontinued)" />
346+
</GridColumns>
347+
</TelerikGrid>
348+
349+
@code {
350+
private async Task SetPredefinedFilterAsync(FilterMenuTemplateContext filterContext)
351+
{
352+
var compositeFilterDescriptor = filterContext.FilterDescriptor;
353+
compositeFilterDescriptor.FilterDescriptors.Clear();
354+
compositeFilterDescriptor.LogicalOperator = FilterCompositionLogicalOperator.Or;
355+
356+
compositeFilterDescriptor.FilterDescriptors.Add(new FilterDescriptor()
357+
{
358+
Member = nameof(Product.Price),
359+
MemberType = typeof(int),
360+
Operator = FilterOperator.IsGreaterThanOrEqualTo,
361+
Value = 55
362+
});
363+
364+
await filterContext.FilterAsync();
365+
}
366+
367+
private async Task ClearFilterAsync(FilterMenuTemplateContext filterContext)
368+
{
369+
await filterContext.ClearFilterAsync();
370+
}
371+
372+
private List<Product> GridData { get; set; }
373+
374+
protected override void OnInitialized()
375+
{
376+
GridData = new List<Product>();
377+
378+
var rnd = new Random();
379+
380+
for (int i = 1; i <= 30; i++)
381+
{
382+
GridData.Add(new Product
383+
{
384+
Id = i,
385+
Name = "Product name " + i,
386+
Price = (decimal)(rnd.Next(1, 50) * 3.14),
387+
Released = DateTime.Now.AddDays(-rnd.Next(1, 365)).AddYears(-rnd.Next(1, 10)).Date,
388+
Discontinued = i % 5 == 0
389+
});
390+
}
391+
392+
base.OnInitialized();
393+
}
394+
395+
public class Product
396+
{
397+
public int Id { get; set; }
398+
public string Name { get; set; }
399+
public decimal Price { get; set; }
400+
public DateTime Released { get; set; }
401+
public bool Discontinued { get; set; }
402+
}
403+
}
404+
````
308405

309406
## See Also
310407

0 commit comments

Comments
 (0)