Predicate

Version 7 of AdapTable introduced a new concept called a Predicate which is used in many functions.

Essentially a Predicate is a type that returns a boolean value.

Predicate Handler

A predicate includes a handler function. This will run each time the predicate is required, and evaluate the contents of the given cell (or alert or data change) against the predicate type and return either true or false.

For instance the 'Blanks' predicate will check all column types to see if the value is empty, while the 'Positive' predicate will check the value of a number cell to see if its greater than 0.

Predicate Inputs

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

For instance 'GreaterThan' compares the input value against base value and returns true if the latter is larger.

The input is passed in as an array. The reason is that while most inputs are a single value, some require 2 inputs (e.g. between) while the 'In' Predicates (also known as 'Values') can take an array of unlimited length.

Column Scope

Each Predicate has a ColumnScope property of type Scope which defines where it can be applied.

note

For System Predicates the Scope is always of type 'DataType' but for Custom Predicates (see below) the Scope can be limited to particular ColumnId(s)

Function Scope

A Predicate also has a FunctionScope which defines in which Adaptable Functions the Predicate is available.

The value will be one, some, or all of the following functions:

// Definition of the GreaterThan Predicate
// Note it has Column Scope of Number and wide Function Scope
// It receives a single input (of type 'number')
id: 'GreaterThan',
label: 'Greater Than',
icon: { path: 'greater-than' },
columnScope: { DataTypes: ['Number'] },
functionScope: ['filter', 'alert', 'validation', 'conditionalstyle'],
inputs: [{ type: 'number' }],
handler: ({ value, inputs }) => Number(value) > Number(inputs[0]),
toString: ({ inputs }) => `> ${inputs[0]}`,
shortcuts: ['>'],

System Predicates

AdapTable ships with a number of Predicates which are available to use across the Grid.

Most of these are available in all functions that use Predicates but there are one or two exceptions. See the list below for more details.

Multiple Variations

warning

Because we wish to ensure that each Predicate is used by one DataType only - we sometimes have more than one predicate for the same type of operation but with a different name.

For example AdapTable offer an 'equality' type valuation via 3 different predicates:

  • Number: Equals
  • String: Is
  • Date: On

Likewse there is a Numeric 'GreaterThan' and a Date 'After'

Values Predicate

Most Predicates are relatively straightforward.

They take either no inputs (e.g. Blanks) or just 1 input (e.g. GreaterThan).

The one exception is the Values Predicate which works like the SQL 'IN' operator.

note

The Values Predicate is only available in Column Filter

It receives an array of values and returns true for each row where the Column contains one of the values.

tip

When selected, AdapTable provides a popup form with a checkbox list of all distinct Column values

System Predicate List

The Predicates shipped by AdapTable are:

PredicateColumn Data TypeInputsWhere Used
ValuesAll0Column Filter
BlanksAll0Column Filter, Alert, Validation, Conditional Style
NonBlanksAll0Column Filter, Alert, Validation, Conditional Style
EqualsNumber1Column Filter, Alert, Validation, Conditional Style
NotEqualsNumber1Column Filter, Alert, Validation, Conditional Style
GreaterThanNumber1Column Filter, Alert, Validation, Conditional Style
LessThanNumber1Column Filter, Alert, Validation, Conditional Style
PositiveNumber0Column Filter, Alert, Validation, Conditional Style
NegativeNumber0Column Filter, Alert, Validation, Conditional Style
ZeroNumber0Column Filter, Alert, Validation, Conditional Style
BetweenNumber2Column Filter, Alert, Validation, Conditional Style
NotBetweenNumber2Column Filter, Alert, Validation, Conditional Style
PercentChangeNumber0Alert, Validation
IsNumericNumber0Validation
IsString1Column Filter, Alert, Validation, Conditional Style
IsNotString1Column Filter, Alert, Validation, Conditional Style
ContainsString1Column Filter, Alert, Validation, Conditional Style
NotContainsString1Column Filter, Alert, Validation, Conditional Style
StartsWithString1Column Filter, Alert, Validation, Conditional Style
EndsWithString1Column Filter, Alert, Validation, Conditional Style
RegexString1Column Filter, Alert, Validation, Conditional Style
TodayDate0Column Filter, Alert, Validation, Conditional Style
YesterdayDate0Column Filter, Alert, Validation, Conditional Style
TomorrowDate0Column Filter, Alert, Validation, Conditional Style
ThisWeekDate0Column Filter, Alert, Validation, Conditional Style
ThisMonthDate0Column Filter, Alert, Validation, Conditional Style
ThisQuarterDate0Column Filter, Alert, Validation, Conditional Style
ThisYearDate0Column Filter, Alert, Validation, Conditional Style
InPastDate0Column Filter, Alert, Validation, Conditional Style
InFutureDate0Column Filter, Alert, Validation, Conditional Style
BeforeDate1Column Filter, Alert, Validation, Conditional Style
AfterDate1Column Filter, Alert, Validation, Conditional Style
OnDate1Column Filter, Alert, Validation, Conditional Style
NotOnDate1Column Filter, Alert, Validation, Conditional Style
NextWorkDayDate0Column Filter, Alert, Validation, Conditional Style
LastWorkDayDate0Column Filter, Alert, Validation, Conditional Style
TrueBoolean0Column Filter, Alert, Validation, Conditional Style
AnyAll0Alert, Validation
ExistingValuesOnlyAll0Alert, Validation
NoDuplicateValuesAll0Alert, Validation
PrimaryKeyDuplicatePK Column0Alert, Validation

Using Predicates

The AdaptablePredicate object has just 2 properties:

PropertyDescription
InputsOptional inputs - needed to evaluate some Predicates (e.g. 'GreaterThan')
PredicateIdId of Predicate (e.g. 'Equals', 'GreaterThan')
note

The Inputs property is required when the Predicate needs additional information for evaluation.

Referencing a Predicate

It is straightforward to reference a Predicate in Predefined Config:

predefinedConfig: {
Filter: {
ColumnFilters: [
{
// Using no inputs
ColumnId: 'Price',
Predicate: {
PredicateId: 'Positive',
},
},
{
// Using 1 input
ColumnId: 'OrderId',
Predicate: {
PredicateId: 'GreaterThan',
Inputs: [15],
},
},
{
// Using 2 inputs
ColumnId: 'Age',
Predicate: {
PredicateId: 'Between',
Inputs: [18, 65],
},
},
{
ColumnId: 'Currency',
Predicate: {
PredicateId: 'Values',
Inputs: ['GBP', 'USD'],
},
],
},
};

Custom Predicates

Developers are encouraged to create their own Predicates at DesignTime via the Custom Predicate Defs section of Adaptable Options.

// Create a Custom Predicate called 'High Invoice'
// and give it column scope of 'OrderId' column
// and function scope of multiple functions
const adaptableOptions: AdaptableOptions = {
customPredicateDefs: [
{
id: 'high_invoice',
label: 'High Invoice',
columnScope: {
ColumnIds: ['OrderId'],
},
functionScope: ['filter', 'conditionalstyle', 'alert', 'cellvalidation'],
handler(params: PredicateDefHandlerParams) {
const invoiced: number = params.node.data.InvoicedCost;
const itemCount: number = params.node.data.ItemCount;
return invoiced > 100 && itemCount > 10 ? true : false;
},
}
],
// Create a Column Filter in Predefined Config
// and use the Custom Predicate defined above
predefinedConfig: {
Filter: {
ColumnFilters: [
{
ColumnId: 'OrderId',
Predicate: { PredicateId: 'high_invoice' },
},
],
},
};

Demos

Visit the AdapTable Demo Site to see a number of demos that use Predicates