Documentation: Advanced Datatable with Tree Structures
This document provides a comprehensive guide to implementing hierarchical datatables using theAdminController backend and the TreeDatatable.vue frontend component.
Part 1: Datatable Data Format
The backend generates a standardized JSON object for each row (or “node”) in the datatable. Understanding this structure is key to leveraging all the component’s features.Anatomy of a Row Object
Property Breakdown
Properties starting with an underscore_ are metadata used to control the UI.
| Property | Type | Source | Description |
|---|---|---|---|
| Core Data | mixed | Model | All the original fields from your Eloquent model (e.g., id, accountCode, accountName). |
children | Array | Backend | An array containing child row objects. In backend lazy-load mode, this is populated by the server. In frontend lazy-load mode, it’s initially empty. The component renames this to _children internally. |
_expanded | Boolean | Backend | Server’s Suggestion. If true, the frontend will render this node as expanded on initial load or when table_tree_expand_all is enabled. The user’s interaction can override this. |
_selectable | Boolean | Backend | If false, the checkbox or radio button for this row will be disabled, and the row will be styled as non-selectable. Controlled by table_tree_leaf_only_select or table_tree_selectable_checker. |
_selected | Boolean | Backend | If true, the frontend component will automatically add this row to its selection state upon loading data. Controlled by table_pre_selected_ids. |
_hasChildren | Boolean | Backend | Crucial for Frontend Lazy Loading. Indicates if a node has children, even if the children array is empty. The UI uses this to show/hide the expander icon. |
_type | String | Backend | (Optional) A type identifier (e.g., 'group', 'employee') used for custom styling or logic on the frontend. |
Part 2: Backend - AdminController Setup
To serve tree data, configure these protected properties in your controller that extends AdminController.
Configuration Properties
| Property | Type | Description |
|---|---|---|
table_structure_mode | string | (Required) Set to 'simple_tree' or 'compound_tree'. |
table_lazy_load | string | Set to 'frontend' for the most scalable performance. The frontend will fetch children on demand. Set to 'backend' for the API to build the tree (simpler for frontend). |
table_tree_parent_field | string | For 'simple_tree' mode. The column in your model that holds the parent’s ID. |
table_pre_expanded_ids | array | Populated automatically from the frontend request (expandedIds payload). Contains IDs of rows the user has open. This takes priority over table_tree_expand_all. |
table_tree_expand_all | bool | If true and expandedIds is not sent from the frontend, all nodes will be sent as expanded. Useful for initial view. |
table_tree_leaf_only_select | bool | If true, only rows with no children will be selectable. |
table_tree_selectable_checker | Closure | A function that receives the row data (as an array) and returns true or false to determine selectability. e.g., fn($row) => $row['status'] === 'active' |
table_pre_selected_ids | array | An array of IDs you provide on the backend to force certain rows to be pre-selected on load. |
table_compound_tree_config | array | For 'compound_tree' mode. A detailed configuration array defining the multi-model hierarchy. |
Example Controller (ChartOfAccountController.php)
This example sets up a lazy-loaded, single-model tree where only “active” accounts can be selected.
Part 3: Frontend - TreeDatatable.vue Usage
The TreeDatatable component is highly configurable via its props.
Key Props
| Prop | Type | Description |
|---|---|---|
rows | Array | The array of data from the backend. |
columns | Array | The column definitions. One column should have { isTreeColumn: true } to render the indented tree view. |
selectOptions | Object | Configures selection behavior: { enabled: Boolean, mode: String }. mode can be 'single' or 'multi'. |
selectedRows | Array | An array of the currently selected row objects. Use with .sync modifier or manage with events. |
expandedRows | Array | (For State Preservation) An array of the IDs of currently expanded rows. Use with .sync modifier to enable state preservation across data refreshes. |
Key Events
| Event | Payload | Description |
|---|---|---|
@update:sortBy | Array | Emitted when the user changes the sort order. The parent should re-fetch data with the new sort parameters. |
@update:expandedRows | Array | Emitted whenever the user expands or collapses a row. The parent should store this array of IDs and send it back on the next data fetch. |
@select-toggle | Object (the row) | Emitted when a single row’s selection state is toggled. |
@select-bulk | Array (of rows) | Emitted on initial load if the server sent _selected: true flags, allowing the parent to add these to its selection without removing existing ones. |
@load-children | Object (the parent row) | Emitted in frontend lazy-load mode when an un-loaded node is expanded. The parent must fetch its children and update the rows prop. |
Example Parent Component Implementation
This shows how a page would useTreeDatatable to manage all its state.
