Blazor | ComponentOne
Controls / DataFilter / Conditional Filter
In This Topic
    Conditional Filter
    In This Topic

    DataFilter supports conditional filtering operations which can be used to apply filters based on specific conditions. This feature enables you to highlight critical information, and identify the records that fit a specific condition, when working with a large dataset. You can use the conditional filter to filter the data that meets two or more complex criteria such as extracting matches and differences between data. You can also narrow down your search by combining conditions by using AND/OR operators. With the help of this feature, you can apply conditional filtering on different data types like text, numeric, and date.

    Conditional filtering using DataFilter

    In DataFilter, ConditionalFilter class represents conditional filters. In addition, some more conditional filters are also available in DataFilter that you can use to implement conditional filtering based on the data type of columns. The description of these conditional filters is as follows:

    These classes provide various properties to customize conditional filtering operations for specified data column. All these classes have different default value for the initial filter operation. The ConditionalFilter class provides the DefaultFilterOperation property, which offers the ability to set the initial filter operation for a conditional filter expression.

    In the following example, the dataset is filtered  by model name, production line date and price based on the text, date and numeric type conditional filters, respectively. For creating the manual conditional filters, first, the automatically generated filters are disabled by setting AutoGenerateFilters property of the C1DataFilter class to false. Then, the TextFilter, DateFilter and NumericFilter classes are used to define the filters, and the name of the property of the data item is specified on which the filter gets applied by using the PropertyName property. Additionally, the value of initial filter operation is set for the text filter using the DefaultFilterOperation property and the header text is set for the date filter by using the HeaderText property along with other filter specific properties for each filter.

    The following code example shows the implementation of different types of conditional filters by using the TextFilter, NumericFilter, and DateFilter classes. This example uses the data from the CarVirtualCollection class available in BlazorExplorer product sample.

    Index.razor
    Copy Code
    @using C1.Blazor.DataFilter
    @using C1.Blazor.Grid
    @using C1.Blazor.Accordion
    @using C1.DataCollection
    @using DataFilterBlazor.Model
    
    
    <div class="filtersSection">
        <C1DataFilter ItemsSource="@_data" AutoGenerateFilters="false" AutoApply="true" ExpandMode="@ExpandMode.One">
            <DataFilters>
                <TextFilter PropertyName="Model" DefaultFilterOperation="FilterOperation.StartsWith" />
                <DateFilter PropertyName="DateProductionLine" HeaderText="Date Production Line" />
                <NumericFilter PropertyName="Price" Increment="1000" Format="F2" />
            </DataFilters>
        </C1DataFilter>
    </div>
    <div class="dataSection">
        <FlexGrid AutoGeneratingColumn="OnAutoGeneratingColumn" ColumnFilterLoading="OnColumnFilterLoading" ItemsSource="@_data" HeadersVisibility="GridHeadersVisibility.All" Style=@("max-height: inherit;")>
         </FlexGrid>
    </div>
    
    @code {
    
        CarVirtualCollection _data;
    
        protected override void OnInitialized()
        {
            _data = new CarVirtualCollection();
             _data.PropertyChanged += DataOnPropertyChanged;
        }
        void OnAutoGeneratingColumn(object sender, GridAutoGeneratingColumnEventArgs a)
        {
            if (a.Property.Name == nameof(Car.Picture))
            {
                a.Column.CellTemplate = target => builder =>
                {
                    builder.OpenElement(0, "img");
                    builder.AddAttribute(1, "style", "max-height: 35px");
                    builder.AddAttribute(2, "src", $"data:image/bmp;base64, {((Lazy<string>)target).Value}");
                    builder.CloseElement();
                };
            }
        }
    
        private void DataOnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs args)
        {
            if (args.PropertyName == nameof(_data.IsLoading))
            {
                 InvokeAsync(StateHasChanged);
            }
        }
    
        async void OnColumnFilterLoading(object sender, GridColumnFilterLoadingEventArgs a)
        {
            var dataFilter = await a.DataFilter;
    
            // Set the initial filter operation for column's conditional filters.
            foreach (var filter in dataFilter.Filters)
            {
                if (filter is TextFilter textFilter)
                {
                    textFilter.DefaultFilterOperation = FilterOperation.StartsWith;
                }
            }
    
        }
    
        public void Dispose()
        {
            if (_data == null)
            {
                return;
            }
    
            _data.PropertyChanged -= DataOnPropertyChanged;
        }
    }