为Blazor的Telerik UI?下载30天免费试用

过滤器模板

过滤器模板允许您自定义内置过滤器的外观和逻辑。它允许您使用网格的内置过滤逻辑,并实现您自己的设计和逻辑来设置它们的值。

可以使用两个不同的模板,具体取决于过滤模式你选择了:

过滤器行模板

默认情况下,筛选器行放置一个适当的编辑器(如用于数字的数字文本框)及其ValueChanged处理器在每次击键时触发网格过滤。还有一个按钮供用户选择筛选操作符,还有一个清除筛选按钮(当编辑器中有值时)。

要自定义筛选单元格,请使用< FilterCellTemplate >的标签< GridColumn >.它接收一个上下文类型的FilterCellTemplateContext它提供了以下成员:

  • FilterDescriptor—描述列过滤器的对象。默认情况下,它有一个带有字段类型和名称的第一个筛选器,您可以向其添加更多筛选器FilterDescriptors集合,或更改其LogicalOperator从默认的

  • FilterAsync ()——一个异步方法,该方法调用内置网格筛选逻辑(包括用于OnRead如果你使用一个),那么你可以很容易地从你的模板调用它(例如,当一个值改变或一个按钮被点击时)。

  • ClearFilterAsync ()——一个异步方法,该方法调用内置网格清除过滤逻辑(包括用于OnRead如果你使用一个),那么你可以很容易地从你的模板调用它(例如,当一个值被清除或一个按钮被点击)。

可以在视图模型的字段中存储对每个列上下文的引用,这样就可以在标准c#代码中编写处理程序,而不是在标记中使用lambdas。您还可以将上下文作为参数传递给您自己的单独过滤器组件,以减少主网格标记和代码中的混乱。

例子

下面的例子展示了一个自定义过滤器:

  • 通过两个数字文本框在筛选单元格中实现最小-最大筛选器。
  • 的过滤器OnChange事件(仅当用户按Enter键或模糊输入时)以减少数据库调用。
  • 演示如何存储对上下文的引用或在模板中内联使用它。
  • 展示了如何使用两个过滤器和示例逻辑构建一个过滤器描述符,该描述符总是过滤数据,即使其中一个输入为空。

你可以在。找到更多的例子现场演示:自定义筛选器行的本地安装中提供演示文件夹中。

自定义过滤器行模板- OnChange上的最小和最大过滤器

@using Telerik。自定义过滤器文本框通过OnChange事件在Enter或模糊时调用过滤     @{ // we store a reference to the filter context to use in the business logic // you can also use it inline in the template, like with the Clear button below theFilterContext = context; }              @code { FilterCellTemplateContext theFilterContext { get; set; } public decimal? MinValue { get; set; } public decimal? MaxValue { get; set; } async Task SetupFilterRule() { // set up min value filter - there is one default filter descriptor // that alredy has the field set up, so we use that for the MIN filter // and set up a value and operator var filter1 = theFilterContext.FilterDescriptor.FilterDescriptors[0] as FilterDescriptor; filter1.Value = MinValue == null ? int.MinValue : MinValue; filter1.Operator = FilterOperator.IsGreaterThan; // set up max value filter - we may have to crete a new filter descriptor // if there wasn't one already so we prepare it first and check whether we have the second filter var filter2Val = MaxValue == null ? int.MaxValue : MaxValue; var filter2 = new FilterDescriptor("Price", FilterOperator.IsLessThan, filter2Val); filter2.MemberType = typeof(decimal); if (theFilterContext.FilterDescriptor.FilterDescriptors.Count > 1) { theFilterContext.FilterDescriptor.FilterDescriptors[1] = filter2; } else { theFilterContext.FilterDescriptor.FilterDescriptors.Add(filter2); } // ensure logical operator between the two filters is AND (it is the default, but we showcase the option) theFilterContext.FilterDescriptor.LogicalOperator = FilterCompositionLogicalOperator.And; // invoke filtering through the method the context provides await theFilterContext.FilterAsync(); } // sample grid data public List GridData { get; set; } = Enumerable.Range(1, 50).Select(x => new SampleData { Id = x, Price = x * 0.5m, ProductName = $"Product {x}" }).ToList(); public class SampleData { public int Id { get; set; } public decimal Price { get; set; } public string ProductName { get; set; } } } @* sample CSS rule to align the custom label elements in the filter cell *@ 

上面的代码片段经过筛选后的结果

自定义过滤单元格模板-最小和最大

过滤器菜单模板

默认情况下,过滤器菜单包含两个与逻辑运算符—OR或AND绑定的过滤器值,过滤器通过专用的filter按钮触发,Clear按钮删除过滤器。

要自定义筛选器菜单,请使用< FilterMenuTemplate >的标签< GridColumn >.的过滤器而且清晰的模板下面仍有按钮可用。

模板接收一个上下文类型的FilterMenuTemplateContext它提供了以下成员:

  • FilterDescriptor—描述列过滤器的对象。默认情况下,它有两个带有字段类型和名称的过滤器,您可以向其添加更多过滤器FilterDescriptors集合,或更改其LogicalOperator从默认的

您可以在视图模型的字段中存储对每个列上下文的引用,这样您就可以从标准c#代码中的事件处理程序引用它,而不是只将它作为一个nargument传递给标记中的lambdas。您还可以将上下文作为参数传递给您自己的单独过滤器组件,以减少主网格标记和代码中的混乱。

例子

下面的例子展示了一个自定义过滤器:

  • 实现一个多复选框筛选器,允许用户从数据源中选择多个值。
    • 展示你如何处理筛选,因为用户不能自行选择筛选操作符。
  • 演示如何存储对上下文的引用或在模板中内联使用它。
  • 演示如何为用户选择的每个值构建多个筛选器描述符。

你可以在。找到更多的例子现场演示:自定义过滤器菜单的本地安装中提供演示文件夹中。

有关CheckboxList过滤器的示例,请参见自定义数据章节。

自定义筛选菜单模板-多个复选框

网格可以为您创建复选框过滤器,请参阅CheckBoxList过滤篇文章。

@using Telerik。DataSource      @foreach (var size in Sizes) { 
}
@code { private bool IsCheckboxInCurrentFilter(CompositeFilterDescriptor filterDescriptor, string size) { // get all current filter descriptors and evaluate whether to select the current checkbox // the default value for string filter descriptors is null so it would select the null checkbox always // so we will add a check to ensure it matches the desired operator - IsNull (see the UpdateCheckedSizes method below) if (size == null) { foreach (FilterDescriptor item in filterDescriptor.FilterDescriptors) { if (item.Operator == FilterOperator.IsNull) { return true; } } return false; } return filterDescriptor.FilterDescriptors.Select(f => (f as FilterDescriptor).Value?.ToString()).ToList().Contains(size); } private void UpdateCheckedSizes(bool isChecked, string itemValue, FilterMenuTemplateContext context) { var compositeFilterDescriptor = context.FilterDescriptor; compositeFilterDescriptor.LogicalOperator = FilterCompositionLogicalOperator.Or; if (!isChecked) { // find and remove the filter descriptor for this checkbox compositeFilterDescriptor.FilterDescriptors.Remove(compositeFilterDescriptor.FilterDescriptors.First(x => { var fd = x as FilterDescriptor; if ((fd.Operator == FilterOperator.IsNull && itemValue == null) || (fd.Operator == FilterOperator.IsEqualTo && fd.Value?.ToString() == itemValue)) { return true; } else { return false; } })); } else { // add a filter descriptor for this checkbox compositeFilterDescriptor.FilterDescriptors.Add(new FilterDescriptor() { Member = nameof(Product.Size), MemberType = typeof(string), Operator = itemValue == null ? FilterOperator.IsNull : FilterOperator.IsEqualTo, Value = itemValue }); } } private List GridData { get; set; } private string[] Sizes = new string[] { "XS", "S", "M", "L", "XL", null }; protected override void OnInitialized() { GridData = Enumerable.Range(1, 70).Select(x => new Product { Id = x, Size = Sizes[x % Sizes.Length], Name = $"Product {x}" }).ToList(); base.OnInitialized(); } public class Product { public int Id { get; set; } public string Name { get; set; } public string Size { get; set; } } }

上面的代码片段经过筛选后的结果

带有复选框的自定义筛选菜单模板

另请参阅

在本文中
Baidu
map