Custom Predicates
Summary
- In addition to the many Predicates shipped by AdapTable, developers can provide their own Predicate Definition
- This is done via the
customPredicateDefsproperty in Predicate Options - Once created, Custom Predicates are used in exactly the same way as System Predicates
- Each Predicate Definition must:
- include a
handlerfunction to be invoked by AdapTable - configure which Modules can use the Predicate
- set which Columns (or DataTypes) can access the Predicate
- include a
- 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
- Custom Predicates add bespoke items to the Predicates available in AdapTable
- Use Custom Expression Functions to extend Expressions
customPredicateDefs
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
idis the unique identifier for the Predicate - The
labelis 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:
customFilter- used in Column Filtersalert- can trigger an AlertflashingCell- available in Flashing CellsformatColumn- used to define a Format ColumnbadgeStyle- used to define a Badge Style
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
- This demo creates 3 Custom Predicate Definitions, with a variety of different Module and Column Scope:
Popular(Module Scope ofcolumnFilter; Column Scope ofname) - where the Row has many Stars and WatchersVanilla(Module Scope ofcolumnFilter&formatColumn; Column Scope oflanguage) - where language is JavaScript or HTMLPopular(Module Scope ofcolumnFilter&alert; Column Scope ofdateColumns) - where Date > 19 Dec 2021
- We have demonstrated some of these Predicates in Action by:
- Setting a Column Filter to the
NameColumn using thePopularPredicate - Provided a Conditional Style for the
LanguageColumn that uses theVanillaPredicate - Created a Warning Alert for Date Columns that use the
RecentPredicate
- Setting a Column Filter to the
- 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
GreaterThancompares 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.
- This demo creates a Custom Predicate Definition
Long Stringwhich also receive aninput - It has
moduleScopeofformatColumnandcolumnFilterand acolumnScopeof 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
- 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
- This example overrides 5 System Date Predicates with Custom Predicates that have Timezone support:
Today,Yesterday,Tomorrow,InPast,InFuture - We only override the
handlerfor each of the System Predicates, and do not change any other properties - However for
Todaywe provide a different Id ofNow- which means that bothTodayandNowappear 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
ZonedDateTimeServicewhich is provided in Adaptable Context, making it available in all AdapTable's callbacks - We also leverage the
systemFilterPredicatesproperty to remove other Date Predicates whose default implementation is not timezone compatible
- Change the Timezone via the
TimezoneSelectcomponent in the custom toolbar and/or theDue Datefilter 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
- In this example we extend 4 System Predicates by providing properties other than
handler:LessThanandGreaterThanhave new labels of "Fewer" and "More" respectivelyContainsandNotContainshave been given different icons