blazor-405616-components-filter-builder-operators.md
The DevExpress Blazor Filter Builder ships with the following operators:
And, Or, NotAnd, and NotOr).Run Demo: Filter Builder - Operator Customization
This article contains information about built-in filter operators and describes how to access, manage, and customize operator collections or individual items.
The DxFilterBuilder component stores all available filter operators in the FilterBuilderOperatorType enumeration:
Equals, Is between, Contains, Is Today)Exists, Count, Avg, Sum, Min, Max)When a user creates or changes a filter condition, the DxFilterBuilder component populates operator lists with items based on associated data types. The table below lists data types and supported criteria operators.
|
Equals | Does not equalContains | Does not containBegins with | Ends withIs like | Is not likeIs any of | Is none ofIs blank | Is not blank
Equals | Does not equalIs greater than | Is greater than or equal toIs less than | Is less than or equal toIs between | Is not betweenIs any of | Is none ofIs blank | Is not blank
Equals | Does not equalIs any of | Is none ofIs blank | Is not blank
Equals | Does not equalIs blank | Is not blank |
Equals | Does not equalIs the same date asIs greater than | Is greater than or equal toIs less than | Is less than or equal toIs in date range | Is out of date rangeIs any of | Is none ofIs blank | Is not blankIs today | Is yesterday | Is tomorrowIs this week | Is last week | Is next weekIs this month | Is last month | Is next monthIs this year | Is last year | Is next yearIs in Year-to-Date periodIs prior to this year | Is beyond this yearIs January | Is February | Is MarchIs April | Is May | Is JuneIs July | Is August | Is SeptemberIs October | Is November | Is December
Equals | Does not equalIs greater than | Is greater than or equal toIs less than | Is less than or equal toIs in time range | Is out of time rangeIs any of | Is none ofIs blank | Is not blank
|
The Filter Builder component displays aggregate operators for fields that store object collections (the DxFilterBuilderField.IsCollection property is true). Aggregate functions calculate collection summaries and allow users to create filter conditions based on the aggregated results. For example, users can filter out invoices containing fewer than 5 items in the Products * field.
Run Demo: Filter Builder - Collection Fields and Aggregate Operators
The following aggregate functions are available:
ExistsReturns true if the collection contains at least one field (object).CountCalculates the number of fields (objects) in the collection.AverageCalculates the average of all values in the collection.SumCalculates the total sum of all values in the collection.MinimumReturns the minimum value in the collection.MaximumReturns the maximum value in the collection.
The following code snippet defines an Orders collection field:
<DxFilterBuilder @bind-FilterCriteria="FilterCriteria">
<Fields>
<DxFilterBuilderField FieldName="ProductName" Caption="Product" Type="@typeof(string)" />
<DxFilterBuilderField FieldName="UnitPrice" Caption="Unit Price" Type="@typeof(decimal)" />
<DxFilterBuilderField FieldName="UnitsInStock" Caption="Units In Stock" Type="@typeof(int)" />
<DxFilterBuilderField FieldName="Orders" Caption="Orders" IsCollection="true">
<Fields>
<DxFilterBuilderField FieldName="OrderDate" Caption="Order Date" Type="@typeof(DateTime?)" />
<DxFilterBuilderField FieldName="CustomerName" Caption="Customer" Type="@typeof(string)" />
<DxFilterBuilderField FieldName="Quantity" Caption="Quantity" Type="@typeof(int)" />
</Fields>
</DxFilterBuilderField>
</Fields>
</DxFilterBuilder>
The DxFilterBuilder.CustomizeOperators event allows you to change operator lists or access/customize individual filter operators. Handle the event and use its Operators argument to access operator collections. You can obtain an item by its type.
You can also use the FieldName or FieldType argument property to access collections associated with a specific data field or type.
<DxFilterBuilder CustomizeOperators="CustomizeOperators">
<Fields>
<DxFilterBuilderField FieldName="Subject" Caption="Name" Type="@typeof(string)" />
<DxFilterBuilderField FieldName="CreatedDate" Caption="Created Date" Type="@typeof(DateTime)" />
<DxFilterBuilderField FieldName="Amount" Caption="Amount" Type="@typeof(int)" />
</Fields>
</DxFilterBuilder>
@code {
void CustomizeOperators(FilterBuilderCustomizeOperatorsEventArgs args) {
if(args.FieldType == typeof(DateTime)) {
// Obtains a predefined operator item by its type
FilterBuilderOperatorItem equalOperator = args.Operators[FilterBuilderOperatorType.Equals];
}
}
}
Call the Operators.Add(item) or Operators.Insert(position, item) method in a CustomizeOperators event handler to add items to operator collections. The FilterBuilderOperatorType enumerator contains all built-in filter operators.
<DxFilterBuilder CustomizeOperators="CustomizeOperators">
<Fields>
<DxFilterBuilderField FieldName="Subject" Caption="Name" Type="@typeof(string)" />
<DxFilterBuilderField FieldName="CreatedDate" Caption="Created Date" Type="@typeof(DateTime)" />
<DxFilterBuilderField FieldName="Amount" Caption="Amount" Type="@typeof(int)" />
</Fields>
</DxFilterBuilder>
@code {
void CustomizeOperators(FilterBuilderCustomizeOperatorsEventArgs args) {
if(args.FieldType == typeof(DateTime)){
FilterBuilderOperatorItem equalOperator = args.Operators[FilterBuilderOperatorType.Equals];
FilterBuilderOperatorItem anyOperator = args.Operators[FilterBuilderOperatorType.AnyOf];
args.Operators.Clear();
// Inserts the Equal operator item at the end of the collection
args.Operators.Add(equalOperator);
// Inserts the DoesNotEqual operator item at the end of the collection using the constructor
args.Operators.Add(new FilterBuilderOperatorItem(FilterBuilderOperatorType.DoesNotEqual));
// Inserts the AnyOf operator item at the first position in the collection
args.Operators.Insert(0, anyOperator);
// Inserts the NoneOf operator item at the second position in the collection using the constructor
args.Operators.Insert(1, new FilterBuilderOperatorItem(FilterBuilderOperatorType.NoneOf));
}
}
}
Note
The Filter Builder validates item collections for each field type and hides unsupported operator items (even if you add these items in a CustomizeOperators event handler).
The DevExpress Blazor Filter Builder component supports custom criteria functions. Follow the steps below to use a custom operator item in the Filter Builder:
<DxFilterBuilder CustomizeOperators="CustomizeOperators">
<Fields>
<DxFilterBuilderField FieldName="Subject" Caption="Name" Type="@typeof(string)" />
<DxFilterBuilderField FieldName="CreatedDate" Caption="Created Date" Type="@typeof(DateTime)" />
<DxFilterBuilderField FieldName="Amount" Caption="Amount" Type="@typeof(int)" />
</Fields>
</DxFilterBuilder>
@code {
void CustomizeOperators(FilterBuilderCustomizeOperatorsEventArgs args) {
if(args.FieldType == typeof(DateTime)) {
FilterBuilderOperatorItem customItem =
new FilterBuilderOperatorItem(IsEarlierThisMonth.Instance.Name) {
GroupName = "Custom Functions"
};
args.Operators.Add(customItem);
}
}
}
using DevExpress.Data.Filtering;
class IsEarlierThisMonth : ICustomFunctionOperator, ICustomFunctionDisplayAttributes {
public static IsEarlierThisMonth Instance { get; } = new IsEarlierThisMonth();
public static void Register() => CriteriaOperator.RegisterCustomFunction(Instance);
public static bool Unregister() => CriteriaOperator.UnregisterCustomFunction(Instance);
private IsEarlierThisMonth() { }
public string Name => nameof(IsEarlierThisMonth);
public string DisplayName => "Is earlier this month";
public object Image => null;
public int MinOperandCount => 1;
public int MaxOperandCount => -1;
public string Description => "Function description";
public FunctionCategory Category => FunctionCategory.DateTime;
public object Evaluate(params object[] operands) {
DateTime dateTime;
if(operands[0] is DateOnly dateOnly)
dateTime = dateOnly.ToDateTime(TimeOnly.MinValue);
else
dateTime = Convert.ToDateTime(operands[0]);
DateTime today = DateTime.Today;
DateTime startOfMonth = new DateTime(today.Year, today.Month, 1);
return dateTime >= startOfMonth && dateTime < today;
}
public bool IsValidOperandCount(int count) => count == 1;
public bool IsValidOperandType(int operandIndex, int operandCount, Type type) => type == typeof(DateTime) || type == typeof(DateOnly);
public Type ResultType(params Type[] operands) => typeof(bool);
}
// ...
IsEarlierThisMonth.Register();
// ...
app.Run();
Once you create a custom function, you can obtain its name using the FilterBuilderOperatorItem.Name property. The FilterBuilderOperatorItem.OperatorType property returns the CustomFunction enumeration value.
Call one of the following methods to remove operators from collections:
Operators.Remove(item)Removes the specified operator.Operators.Remove(operator type)Removes the operator with the specified type.Operators.RemoveAt(index)Removes the operator with the specified index.Operators.ClearRemoves all operators.
<DxFilterBuilder CustomizeOperators="CustomizeOperators">
<Fields>
<DxFilterBuilderField FieldName="Subject" Caption="Name" Type="@typeof(string)" />
<DxFilterBuilderField FieldName="CreatedDate" Caption="Created Date" Type="@typeof(DateTime)" />
<DxFilterBuilderField FieldName="Amount" Caption="Amount" Type="@typeof(int)" />
</Fields>
</DxFilterBuilder>
@code {
void CustomizeOperators(FilterBuilderCustomizeOperatorsEventArgs args) {
if(args.FieldType == typeof(DateTime)){
FilterBuilderOperatorItem sameDateItem = args.Operators[FilterBuilderOperatorType.IsSameDay];
// Removes the specified item
args.Operators.Remove(sameDateItem);
// Removes the item with the specified type
args.Operators.Remove(FilterBuilderOperatorType.IsBeyondThisYear);
// Removes the first item
args.Operators.RemoveAt(0);
// Removes all items
args.Operators.Clear();
}
}
}
You can also use the Filter Builder component’s CustomizeOperators event to modify operator groups, change default operators, and customize captions/icons.
The Filter Builder allows you to arrange filter operators into groups. Built-in operators automatically belong to Basic Comparison and Date Ranges groups. For DateTime and DateOnly data types, groups are visible. For other data types, groups are hidden.
Use the ShowGroups event argument to control group visibility. If enabled, custom functions appear in separate groups with empty captions (names).
Use the FilterBuilderOperatorItem.GroupName property to change group names for individual operators.
The following example hides group names for the DateType field type, displays groups for the Amount field, and changes the group caption for Is blank and Is not blank operators:
<DxFilterBuilder CustomizeOperators="CustomizeOperators">
<Fields>
<DxFilterBuilderField FieldName="Subject" Caption="Name" Type="@typeof(string)" />
<DxFilterBuilderField FieldName="CreatedDate" Caption="Created Date" Type="@typeof(DateTime)" />
<DxFilterBuilderField FieldName="Amount" Caption="Amount" Type="@typeof(int)" />
</Fields>
</DxFilterBuilder>
@code {
void CustomizeOperators(FilterBuilderCustomizeOperatorsEventArgs args) {
if(args.FieldType == typeof(DateTime)) {
// Hides groups for the DateTime field type
args.ShowGroups = false;
}
if(args.FieldName == "Amount") {
// Displays groups for the Amount field
args.ShowGroups = true;
// Changes the group name for IsNull and IsNotNull operator items
foreach(var item in args.Operators) {
if(item.OperatorType == FilterBuilderOperatorType.IsNull ||
item.OperatorType == FilterBuilderOperatorType.IsNotNull) {
item.GroupName = "Unary Operators";
}
}
}
}
}
Note that custom functions appear in separate groups with empty captions unless you specify the GroupName property.
When a user creates a filter condition, the Filter Builder populates the operator list with items, and then selects the default item. Use the DefaultOperator event argument to change the default operator:
<DxFilterBuilder CustomizeOperators="CustomizeOperators" >
<Fields>
<DxFilterBuilderField FieldName="Subject" Caption="Name" Type="@typeof(string)" />
<DxFilterBuilderField FieldName="CreatedDate" Caption="Created Date" Type="@typeof(DateTime)" />
<DxFilterBuilderField FieldName="Amount" Caption="Amount" Type="@typeof(int)" />
<DxFilterBuilderField FieldName="Discontinued" Type="@typeof(bool)" />
</Fields>
</DxFilterBuilder>
@code {
void CustomizeOperators(FilterBuilderCustomizeOperatorsEventArgs args) {
if(args.FieldType == typeof(int)) {
args.Operators.Clear();
args.Operators.Add(new FilterBuilderOperatorItem(FilterBuilderOperatorType.Equals));
args.Operators.Add(new FilterBuilderOperatorItem(FilterBuilderOperatorType.Greater));
args.Operators.Add(new FilterBuilderOperatorItem(FilterBuilderOperatorType.Less));
args.DefaultOperator = args.Operators[FilterBuilderOperatorType.Less];
args.ShowGroups = true;
}
}
}
Note
If a user selects a non-default operator and then changes a field for this condition, the Filter Builder preserves the selected operator (instead of selecting the default operator). If you remove the default item from the collection, the component automatically selects the first item in the list.
The Filter Builder uses operator captions to display corresponding filter operators in drop-down lists and resulting filter criteria. The FilterBuilderOperatorType enumeration lists all built-in operators and their captions. Custom functions inherit their captions from the ICustomFunctionDisplayAttributes.DisplayName property. Use an item’s Caption property to customize the operator caption.
<DxFilterBuilder CustomizeOperators="CustomizeOperators" >
<Fields>
<DxFilterBuilderField FieldName="Subject" Caption="Name" Type="@typeof(string)" />
<DxFilterBuilderField FieldName="CreatedDate" Caption="Created Date" Type="@typeof(DateTime)" />
<DxFilterBuilderField FieldName="Amount" Caption="Amount" Type="@typeof(int)" />
</Fields>
</DxFilterBuilder>
@code {
void CustomizeOperators(FilterBuilderCustomizeOperatorsEventArgs args) {
if(args.FieldType == typeof(DateTime)) {
FilterBuilderOperatorItem thisMonthItem = args.Operators[FilterBuilderOperatorType.IsThisMonth];
thisMonthItem.Caption = "Falls in this month";
}
}
}
The Filter Builder allows you to add or customize icons for individual operator items. You can use the following properties:
IconUrlSpecifies the URL of the operator item icon.IconCssClassSpecifies the name of the CSS class applied to the operator item icon.
DevExpress Blazor components support pre-defined icon sets (such as Iconic or Bootstrap-recommended libraries) and custom icon libraries. Refer to the following topic for additional information: Icons.
The following code snippet modifies the operator collection for the DateTime data type and uses icons to customize item appearance:
<DxFilterBuilder CustomizeOperators="CustomizeOperators">
<Fields>
<DxFilterBuilderField FieldName="Subject" Caption="Name" Type="@typeof(string)" />
<DxFilterBuilderField FieldName="CreatedDate" Caption="Created Date" Type="@typeof(DateTime)" />
<DxFilterBuilderField FieldName="Amount" Caption="Amount" Type="@typeof(int)" />
</Fields>
</DxFilterBuilder>
@code {
void CustomizeOperators(FilterBuilderCustomizeOperatorsEventArgs args) {
if(args.FieldType == typeof(DateTime)) {
args.Operators.Clear();
args.Operators.Add(new FilterBuilderOperatorItem(FilterBuilderOperatorType.IsOctober) {
IconCssClass = "october-icon"
});
args.Operators.Add(new FilterBuilderOperatorItem(FilterBuilderOperatorType.IsNovember) {
IconUrl = "../images/november.png"
});
args.Operators.Add(new FilterBuilderOperatorItem(FilterBuilderOperatorType.IsDecember) {
IconUrl = "https://cdn-icons-png.freepik.com/16/6606/6606304.png"
});
args.Operators.Add(new FilterBuilderOperatorItem(FilterBuilderOperatorType.IsThisYear) {
IconCssClass = "oi oi-calendar"
});
}
}
}
.october-icon {
width: 16px;
height: 16px;
background-image: url("../images/october.png");
}