Providing Adaptable Context

Summary

  • The adaptableContext property in Adaptable Options allows individual context to be passed into AdapTable
  • This can then be leveraged at runtime as required

The adaptableContext property allows developers to provide application-specific "context".

Note

This object is very similar in intention to the AG Grid Context object

The property is set by developers in the root of Adaptable Options.

It is subsequently passed by AdapTable into the BaseContext object.

Note

This allows developers to provide data or services which they can subsequently leverage elsewhere in AdapTable, e.g. in Custom Predicates or Display Formats.

Hint

This demo illustrates using Adaptable Context by providing a TimeZone Service leveraged by Custom Predicates

adaptableContext

Record<string, any>
Custom Context for the Application

Demo

The demo below shows the typical pattern for using adaptableContext:

  1. Build an application object — here a currentUser plus a tiny userService that answers permission questions
  2. Put that object on adaptableContext once, alongside the rest of AdaptableOptions
  3. Retrieve it from any AdapTable callback that needs it
Providing Adaptable Context
Fork
  • The userService.ts file exports a currentUser object and a tiny userService with a canPerform() permission helper — both are plain TypeScript, with no framework dependencies
  • adaptableOptions.ts then exposes them through adaptableContext and reads them back in three different places:
    • Config-time — the userName option is set from currentUser.name and is displayed at the top-right of AdapTable
    • Runtime — the Edit action button's hidden callback asks the userService whether the current user is allowed to edit
    • Runtime — the Delete action button's hidden callback does the same for deleting
  • The default user has the Manager role, so Edit is visible but Delete is hidden
Try It Out
  • Edit userService.ts and change currentUser.role to 'Trader' (both buttons disappear) or 'Admin' (both buttons appear)
  • Add another consumer — for example, a Custom Predicate handler — and access currentUser / userService exactly the same way via adaptableContext