Custom Predicates

Summary

  • In addition to the many Predicates shipped by AdapTable, developers can provide their own Predicate Definition
  • This is done via the customPredicateDefs property in Predicate Options
  • Once created, Custom Predicates are used in exactly the same way as System Predicates
  • Each Predicate Definition must:
    • include a handler function to be invoked by AdapTable
    • configure which Modules can use the Predicate
    • set which Columns (or DataTypes) can access the Predicate
  • Custom Predicates can override existing System Predicates with bespoke behaviour

The Predicates shipped by AdapTable can be extended by Custom Predicates provided by developers.

Custom Predicate Definitions

Custom Predicates are provided in the customPredicateDefs property in Predicate Options.

Each is based on a Predicate Definition object.

Caution

customPredicateDefs

Definitions for Custom-provided Predicates

Mandatory Properties

The Predicate Definition object contains many properties which can be set, of which 5 are mandatory:

id

The id defines how the Predicate is referenced in other objects (e.g. Column Filters)

label

The label is the name given to the Predicate and how it will be referenced in the AdapTable UI.

Hint

  • The id is the unique identifier for the Predicate
  • The label is a more friendly way of identifying the Predicate in the UI

handler

The handler property is a function which is invoked by AdapTable each time the predicate is evaluated.

The function receives information about the current evaluation context and returns a boolean (true / false).

Deep Dive

Understanding the handler property

moduleScope

This array property specifies which AdapTable Modules can use the Predicate.

The moduleScope property is of type PredicateModuleScope which can be one, some, or all of:

columnScope

The columnScope property - of type Column Scope - defines where the Predicate can be applied.

Note

  • For System Predicates the Scope is always of type DataType
  • For Custom Predicates the Scope can be a DataType, but can also be limited to particular ColumnId(s)

Find Out More

See the Guide to Scope in AdapTable for more information

Creating Custom Predicates

Now lets put this all together see how to create a Custom Predicate Definition.

Developer Guide

Creating a Custom Predicate Definition

Custom Predicates
Fork
  • This demo creates 3 Custom Predicate Definitions, with a variety of different Module and Column Scope:
    • Popular (Module Scope of columnFilter; Column Scope of name) - where the Row has many Stars and Watchers
    • Vanilla (Module Scope of columnFilter & formatColumn; Column Scope of language) - where language is JavaScript or HTML
    • Popular (Module Scope of columnFilter & alert; Column Scope of date Columns) - where Date > 19 Dec 2021
  • We have demonstrated some of these Predicates in Action by:
    • Setting a Column Filter to the Name Column using the Popular Predicate
    • Provided a Conditional Style for the Language Column that uses the Vanilla Predicate
    • Created a Warning Alert for Date Columns that use the Recent Predicate
Try It Out
  • Edit a date to be after 19 Dec 2021 and see the Alert appear

Adding Inputs

Some Custom Predicates might require an additional input value in order to perform the evaluation.

Hint

  • Many System Predicates require inputs from users
  • For instance GreaterThan compares the input value against base value and returns true if the latter is larger

The inputs property is an array of any type.

The reason it is an arrray is because while most inputs are a single value, some require 2 inputs (e.g. Between)

Note

2 System Predicates - In and NotIn - can take an array of unlimited length

AdapTable will display an appropriate UI input for each value provided.

Custom Predicates with Inputs
Fork
  • This demo creates a Custom Predicate Definition Long String which also receive an input
  • It has moduleScope of formatColumn and columnFilter and a columnScope of String columns.
  • It receives an input (of type number) which is used to verify whether the string is "big" (or not)
  • We have created a Format Column on the Description with an input of 50 - so entries more than 50 characters are coloured
Try It Out
  • Create a new Column Filter on the Lanague Column using the Long String Custom Predicate and an input of 5
  • Note that the 2 rows with HTML will be filtered out

Extending System Predicates

In addition to providing entire new Custom Predicates, developers can provide Custom Predicates which extend the System Predicates shipped by AdapTable.

This is done by using the extends property in the Predicate Definition which takes the PredicateId to override.

Extending System Predicates can be done in 2 ways depending on the behaviour required:

  • Replacing the System Predicate - to do this provide the same Id as the Predicate being extended
  • Adding alongside System Predicate - to do this provide a different Id to the Predicate being extended
Custom Predicates overriding System Predicates Behaviour
Fork
  • This example overrides 5 System Date Predicates with Custom Predicates that have Timezone support: Today, Yesterday, Tomorrow, InPast, InFuture
  • We only override the handler for each of the System Predicates, and do not change any other properties
  • However for Today we provide a different Id of Now - which means that both Today and Now appear in the dropdown (the latter at the bottom)
  • The demo displays a list of tasks with due dates, which are all UTC dates, filtered according to the local time of the selected Timezone
  • Note: The Timezone support (implemented using the Luxon library) is provided only for demonstration purposes and is not production ready!
  • We use a local ZonedDateTimeService which is provided in Adaptable Context, making it available in all AdapTable's callbacks
  • We also leverage the systemFilterPredicates property to remove other Date Predicates whose default implementation is not timezone compatible
Try It Out
  • Change the Timezone via the TimezoneSelect component in the custom toolbar and/or the Due Date filter and observe the results

In the example above, we overrode the handler property in order to provide a new implementation for some System Predicates.

But any property in a System Predicate can be overridden; the behaviour is as follows:

  • Properties provided in the Custom Predicate will replace their equivalents in the System Predicate
  • Properties not expressly provided in the Custom Predicate will use that provided by the System Predicate

Hint

This allows you just to provide a new label, or icon for the Predicate and leave the implementation untouched

Custom Predicates overriding System Predicates Props
Fork
  • In this example we extend 4 System Predicates by providing properties other than handler:
    • LessThan and GreaterThan have new labels of "Fewer" and "More" respectively
    • Contains and NotContains have been given different icons