Filter types

This section describes the types of filters and how you can add or overwrite conditions for each of them.

In general, filters depend on the column type where the filter will apply. Therefore, the filter types are Text, Numeric, Date, and Binary.

There is another type of filter named Multiple Selection filter. But this type of filter can apply to Text and Numeric filters.

See the column types in the Columns Definitions section.

Live example

Filter types live example.

Filter conditions

The following table shows the existing filter types and the conditions that apply for each of them:

Filter typeConditions

Text

Empty, Equals, Not Equals, Contains, Not Contains, Starts With, and Ends With.

Numeric

Empty, Equals, Not Equals, Less Than, Less Than or Equals, Greater Than, Greater Than Or Equals, and Range.

Date

Empty, Equals, Not Equals, Less Than, Less Than or Equals, Greater Than, Greater Than Or Equals, and Range.

Binary

Empty, Equals, and Not Equals.

Multiple Selection

Empty, Equals, and Not Equals.

Customizing filter conditions

Since filter execution comes down to evaluating every filter condition against the data set, you will see how to implement your filter conditions per column or at the grid scope.

All custom instances do not have any defined condition. You must add or implement every filter condition.

When defining a custom condition, you must implement an object of type Condition<T, R, V>.

Text Filter

You must provide an instance of type CustomTextCaseFilterCondition when defining a column.

<dike-grid id="grid-filter-types" height="600px" #grid="dkgGrid">
    <dike-grid-column
        fieldName="completeNameGroup"
        headerText="Complete Name"
        order="2">

        <dike-grid-column
            fieldName="firstName"
            headerText="Name"
            dataType="Text"
            width="150"
            sortable
            filterable
            [customFilterConditions]="nameConditions">
        </dike-grid-column>

        <dike-grid-column
            fieldName="lastName"
            headerText="Surname"
            dataType="Text"
            width="150"
            filterable
            sortable>
        </dike-grid-column>
    </dike-grid-column>

</dike-grid>

We added only two conditions to the Name column: Contains and Not Contains. We also added our custom implementation of the StartsWith condition.

The customFilterConditions property of the DikeGridColumnComponent is of type CustomFilterConditionInstance. Therefore, be aware of assigning the correct custom instance.

Numeric Filter

You must provide a CustomNumericFilterCondition instance for numeric types when defining a column.

For the Age column, let us define a filter that gives us all the numbers outside a close interval.

<dike-grid id="grid-filter-types" height="600px" #grid="dkgGrid"
    (gridColumnDefInstance)="onColumnDefInstance($event)">
</dike-grid>

Notice how we have added the word range in the condition value (not-in-range-interval). If you omit the range word, the UI will not show the second textbox for typing a numeric value.

Date Filter

You must provide a CustomDateFilterCondition instance for Date types when defining a column.

A Date Filter has the same conditions as a Numeric Filter.

Let us define some custom conditions for the Hire Date column:

<dike-grid id="grid-filter-types" height="600px" #grid="dkgGrid"
    (gridColumnDefInstance)="onColumnDefInstance($event)">
</dike-grid>

We have added two existing conditions in the previous example: Greater Than and Less Than. We also defined a custom condition: Close Interval. Same as before, we added the word range to the value of the custom condition.

As you can see, the DikeGrid uses the native Date type. So do not forget to import the MatNativeDateModule or a custom implementation instead.

Binary Filter

With the Binary Filter, the user must select between two options only. Furthermore, those options are mutually exclusive by definition.

For filtering, the DikeGrid casts Binary Filters values to string values.

If you only define a Binary column as filterable, you will see the filters options as shown in the following screenshot:

Let us define options according to our values in our data set for the Gender column. For Binary columns, we have to provide a CustomBinaryFilterCondition instance to add or overwrite conditions.

<dike-grid id="grid-filter-types" height="600px" #grid="dkgGrid"
    (gridColumnDefInstance)="onColumnDefInstance($event)">
</dike-grid>

We have defined the following features with the previous code snippet:

  1. We added Equals and Not Equals conditions. If you try adding a non-valid condition, the DikeGrid will only add valid filter conditions.

  2. We have defined our labels and values according to the Gender column values.

  3. Notice how we have added an initial filter for the Gender column. We tell our DikeGrid that filters the data set with a male value setting the selected property to true.

The previous filter definition generates the following output:

Options for Binary custom filters are of type DikeBinarySelectionModel, and you can only provide two options. If you offer more than two options, DikeGrid will ignore them.

If you try to add a condition different from Equals, Not Equal, or Empty, the DikeGrid will throw an error. You can only overwrite the mentioned conditions.

Multiple Selection

In some cases, you want the user to select several options but a fixed number of options.

Consider the following definitions in the evaluation of the conditions:

ConditionEvaluation

Equals

The DikeGrid evaluates the selected options with the OR logical operator.

Not Equals

The DikeGrid evaluates the selected options with the AND logical operator. Then, it takes the complement of the result set.

Empty

If you add the Empty condition, it will appear as a checkbox indicating if the filtering operation includes or excludes the empty values. The latter depends on the Equals or Not Equals selection, respectively.

Multiple Text Filter

For Multiple Text Filter conditions, you must provide a CustomMultipleTextFilterCondition instance to a Text column type.

Let us define a Multiple Text Filter for the Surname column:

<dike-grid id="grid-filter-types" height="600px" #grid="dkgGrid">
    <dike-grid-column
        fieldName="completeNameGroup"
        headerText="Complete Name"
        order="2">

        <dike-grid-column
            fieldName="firstName"
            headerText="Name"
            dataType="Text"
            width="150"
            sortable
            filterable
            [customFilterConditions]="nameConditions">
        </dike-grid-column>

        <dike-grid-column
            fieldName="lastName"
            headerText="Surname"
            dataType="Text"
            width="150"
            filterable
            [customFilterConditions]="surnameConditions"
            sortable>
        </dike-grid-column>
    </dike-grid-column>

</dike-grid>

With the previous code snippet:

  1. We have added the Empty, Equals, and Not Equals filter conditions.

  2. We have defined several options from which the user can select.

  3. We could have provided an initial filter selecting more than one option.

You can provide any number of options for Multiple Text Filter. The array you provide is of type DikeTextSelectionModel.

Multiple Numeric Filter

For Multiple Numeric Filter conditions, you must provide a CustomMultipleNumericFilterCondition instance to a Numeric column type.

Let us define a Multiple Text Filter for the Performance column:

<dike-grid id="grid-filter-types" height="600px" #grid="dkgGrid"
    (gridColumnDefInstance)="onColumnDefInstance($event)">
</dike-grid>

With the previous code snippet:

  1. We have added Equals and Not Equals conditions.

  2. We added five options to the custom filter.

  3. Important. Notice how we define the getter function rounding the performance value to close the options to an integer value.

You can provide any number of options for Multiple Numeric Filter. The array you provide is of type DikeNumericSelectionModel.

Customizing conditions using the API

You can change the column filter conditions at runtime.

MethodDescription

setColumnFilterability()

You can provide an instance of type CustomFilterConditionInstance. Be aware of passing the correct instance depending on the column type. The DikeGrid will take the provided instance if the filterable flag is true.

Customization at the grid level

The previous customizations apply at the column level. In addition, you can change the definition at the grid level.

To change conditions at grid scope, you must provide the corresponding custom instance through an input property named gridCustomFilterConditions.

The gridCustomFilterConditions property is of type DikeGridCustomFilterConditions. You can not change this property at runtime.

Let us provide a custom condition for Text types.

<dike-grid id="grid-filter-types" height="600px" #grid="dkgGrid"
    [gridCustomFilterConditions]="gridCustomConditions">
</dike-grid>

We have defined a new custom condition for Text types named grid-customUpperCaseText. This condition will apply to the Email column only because we have not specified any custom condition for that column. See the following screenshot.

It is essential to notice that when you define a new custom condition at the grid level, the DikeGrid will add this condition to the existing ones.

Customization by providing an Injection Token

The lowest precedence to define a new condition is by providing an Injection Token.

Injection TokenCustom instance

CUSTOM_TEXT_FILTER_CONDITIONS

CUSTOM_NUMERIC_FILTER_CONDITIONS

CUSTOM_DATE_FILTER_CONDITIONS

CUSTOM_BINARY_FILTER_CONDITIONS

Let us define a custom condition for Text types.

filtering.module.ts
@NgModule({
   providers: [
    {
      provide: CUSTOM_TEXT_FILTER_CONDITIONS,
      useFactory: (): CustomTextCaseFilterCondition<Employee> =>
        new CustomRowTextCaseFilterCondition()
          .addCondition({
            text: 'Global Custom Lower Case',
            value: 'global-customLowerCaseText',
            eval: (entry: Employee, dataColumnDef: DikeDataColumnDef<Employee, string>, values?: DikeTextFilter): boolean =>
              dataColumnDef.getValue(entry).toLowerCase().includes(values.value)
          })
    }
  ]
})
export class FilteringModule { }

We have defined a new custom condition for Text types named global-customLowerCaseText. This condition will apply to the Email column only because we have not specified any custom condition for that column. See the following screenshot.

It is essential to notice that when you define a new custom condition by providing an Injection Token, the DikeGrid will add this condition to the existing ones.

Empty Values

When the DikeGrid instance filters the rows, it considers the following values as empties:

Filter typeValue

Text and Binary

null, undefined, and empty strings.

Numeric

null, undefined, and NaN values.

Date

null, undefined, and Invalid Dates.

We recommend you define the getter function for the columns you define. For further details, see the Column Definitions section.

Summary

Filters depend on the column type where the filter will apply. Apart from the column type a filter applies, there is one more type: Multiple Selection Filter, an extension of Text and Numeric filter types.

Since filter execution comes down to evaluating every filter condition, you can add or overwrite conditions.

Complete code for this section

<div class="mt-2 flex flex-row flex-wrap items-center justify-around">
    <button mat-raised-button
        class="flex-none w-56 my-2"
        color="primary"
        (click)="onGetFilterables()">Filterables
    </button>

    <button mat-raised-button
        class="flex-none w-56 my-2"
        color="primary"
        (click)="onGetFilteredRows()">Filtered rows
    </button>

    <button mat-raised-button
        class="flex-none w-56 my-2"
        color="primary"
        (click)="onClearFilter()">Clear filter
    </button>
</div>

<dike-grid id="grid-filter-types" height="600px" #grid="dkgGrid"
    [displayRowId]="gridProperties.displayRowId"
    [gridElevation]="gridProperties.matElevation"
    [gridElevationValue]="gridProperties.elevationValue"
    [striped]="gridProperties.stripeRows"
    [verticalRowLines]="gridProperties.verticalRowLines"

    [allowSorting]="gridProperties.allowSorting"
    [allowPagination]="gridProperties.allowPagination"

    (registerFilterableChange)="onRegisterChange($event)"
    (filterChange)="onFilterChange($event)"
    [gridCustomFilterConditions]="gridCustomConditions"

    (gridColumnDefInstance)="onColumnDefInstance($event)"
    [datasource]="dkgDataSource">

    <dike-grid-column
        fieldName="employeeId"
        headerText="Employee Id"
        dataType="Text"
        width="350"
        order="1"
        sortable>
    </dike-grid-column>

    <dike-grid-column
        fieldName="completeNameGroup"
        headerText="Complete Name"
        order="2">

        <dike-grid-column
            fieldName="firstName"
            headerText="Name"
            dataType="Text"
            width="150"
            sortable
            filterable
            [customFilterConditions]="nameConditions">
        </dike-grid-column>

        <dike-grid-column
            fieldName="lastName"
            headerText="Surname"
            dataType="Text"
            width="150"
            filterable
            [customFilterConditions]="surnameConditions"
            sortable>
        </dike-grid-column>
    </dike-grid-column>
</dike-grid>

Last updated