State
Data displayed in an object-table
is controlled by its state. The objects provided below list all of the properties which can be safely accessed and mutated from JavaScript/TypeScript.
Advanced topic
Managing state is considered an advanced topic. In most cases an object-table
can be used effectively without directly managing state.
on-state-change
on-state-change
The function specified in on-state-change
is called when the state of the table is changed due to a UX/UI interaction such as changing to the next page or applying a column filter. The function should contain a $state
object which contains a simplified JavaScript/TypeScript object of the table's state.
The internal structure of $state
is as follows:
column properties: The state payload will in contain column
properties which refer to the current name of the column. This is provided as convenience data to make working with the state easier, but it is not used when initializing state since the value can change dynamically. Using the column
data to initialize the state is therefore considered unsafe and should not be relied on.
init-state
init-state
A table can initialize its internal state after the table is loaded using a similar structure as on-state-change
.
How init-state
differs from on-state-change
:
init-state
will ignorecolumn
fields (it only uses columnkey
information)
The internal structure of $state
is as follows:
Changing the state from JavaScript/TypeScript
To change the state of the table using JavaScript/TypeScript, make use of the component.objectTable({ id: "my-id" }).setState({ ... })
method. This method requires an id=""
attribute to be present on the object-table
that matches the id
used in above component.objectTable()
.
Note: The setState()
method takes the exact same payload as init-state=""
.
For example:
When to use setState()
vs init-state=""
setState()
vs init-state=""
Since both of these methods do the same thing but in different parts of an application, it is important to understand why and when you should use one over the other. In most JourneyApps applications, init-state=""
is the easiest to use because it is called only once when the view loads. This is particularly useful when users navigate using a table, and when they return to the table they expect it to look exactly the same as before.
setState()
is useful when a developer needs to control or manipulate the table after the view has loaded. Actions such as changing the page and updating the select cell are events that should not fire on every update on a view, but rather when very specific events occur, such as pressing a particular button.
Tip: Implementing setState()
can slow down your view if done frequently. Use it only where init-state=""
is not feasible.
Last updated