Table control
Table Control is a PHUI class used to create tables that list data. It is useful when you want to list a body of entities (fewer than 100) with multiple properties, or when a data set is too large to display all at once.
Table Control offers several helpful features:
- Actions on a single entity
- Actions on multiple entities at the same time
- Filtering on a specific field or fields
- Sorting on a specific field or fields
Create a table control
The render of a Table Control is handled by the browser’s JavaScript engine. The page can be as simple as the following:
The table appends to the devices-table div. Obtain the context of the table using JavaScript:
Table Control creates a table in the context once you provide additional data about how you want the table to behave. You define this in an object called tableSeed.
For example, to create a table representing a list of horses — where each horse is an entity and you want to show its name, color, and age — create your tableSeed object as follows:
This is the simplest valid tableSeed object. A tableSeed must have a fields property where you define the properties to display as columns. At minimum, specify a displayName — a human-readable description of the field. The key for each field maps to the properties of the horse objects.
Now construct the table:
You should see a table with columns but no rows. Keep the horseTable instance — you will need it later.
Add entities
Add the first entity
Add static data to your table by creating your first horse as a plain JavaScript object:
Add the horse to the table with the pushEntity method:
Once pushed, the table immediately creates the row, mapping name, color, and age to the fields defined in tableSeed.
Table methods
There are three basic methods for adding entities to a table:
Additional methods exposed by Table Control:
Table Control automatically handles sorting and filtering as you add entities. Even when concatenating horses, they appear in the appropriate row based on the current sorter and are hidden if filtered. You can concatenate an array of entities at once:
Note that the object properties do not need to match the fields exactly — Rebecca has no age, and Sally has a breed and gender that are not defined as table fields.
Update entities
When an object is added to a table, it inherits a Table Control super class. Table Control stores objects by reference and handles view updates behind the scenes. This means you can modify the original object and Table Control will update the table accordingly.
You can also use table.getEntities() to retrieve all entity objects.
To remove John from the table:
The object still exists; the table simply no longer tracks it. The removeFromTable method is added to every object passed through pushEntity, concatEntities, or setEntities.
You can also update an entity property by reassignment, and it takes effect in the table immediately:
Sort and filter
Enable sorting and filtering by setting canSort and canFilter to true on specific fields in your tableSeed:
If no field is marked as defaultSort, the first field marked canSort is used as the default sorter.
To configure a specific field as the default sorter:
The defaultSort property accepts 1 (ascending) or -1 (descending).
Define row actions
The rowActions function operates at the entity level and allows users to perform an action on a single entity. For example, to allow editing and selling horses:
The class property is a CSS class set on the button for the action, which appears in a new cell on the right-hand side of the row. Each action’s action property is a function that receives the entity itself.
The second parameter is the DOM event from the button click, as you would normally expect from onclick actions in JavaScript. The primary property specifies which action triggers on row click. At most, one primary action may be specified. If primary is not explicitly set, the first action is primary by default.
Define batch actions
To allow users to perform the same action on multiple rows simultaneously, define batchActions in your tableSeed:
Table Control creates the interface in the DOM. Users can then check multiple rows and select the Brand Horses button at the bottom of their viewport.
Select entities
To programmatically select certain entities — for example, pre-checking rows for horses the user already owns — set the selected flag on the entity:
This causes the horse to appear with its checkbox already checked when pushed to the table.
The selected flag behaves like other entity properties and can be reset in real time:
After five seconds, the first horse becomes selected instantly.
Update action properties and override actions
Row actions or batch actions often need to change at runtime — for example, disabling an action for specific entities or setting a processing class during an API call. Each entity object has an actions property, which you can set in two ways:
- Define it yourself when adding entities via
pushEntities,concatEntities, orsetEntities. - Let Table Control set it automatically.
If you allow Table Control to set the actions property, all horses will have it assigned with disabled: false by default (for example, horses[i].actions.edit.disabled = false).
Regardless of how actions is set, you can modify it at runtime and changes take effect immediately:
You can also replace entire actions:
Advanced techniques
Parcel data
Table Control supports chunking data via AJAX calls for lists too large to store in browser memory. To enable this, add the fetch function to your tableSeed:
A fetch call is made whenever Table Control needs more entities. The table uses lazy loading (infinite scrolling) to display large amounts of data without making calls with huge payloads or overwhelming browser memory. fetch is called repeatedly as the user scrolls. If the user changes the sorter or filter, fetch is called again to reset the table data with the new parameters.
If a table uses parcelling, it must sort and filter by making calls to the server. If it does not use parcelling, Table Control handles sorting and filtering on the client. Never sort or filter data client-side that you know to be a subset of a larger server-side list.
There are two reasons fetch is called:
- To append to the list on scroll.
- To reset entities due to a filter or sort change.
The isAppend flag indicates which case applies. When not appending, start from 0 to ensure the list resets on a filter or sort change.
The callback requires two arguments:
When Argument 2 is true, infinite scrolling stops calling fetch.
Style and size columns
Styling can be done with CSS class modifiers. Table Control adds a class to all <td> and <th> tags corresponding to the field name. For example, a <td> element for the Name field has the class name. This makes it possible to set the width of a specific column using CSS.