Documentation: Advanced Datatable Structures
This document explains how to configure and use the advanced hierarchical data structures for the main datatable in your application. It builds upon theAdminController superclass to render flat lists, simple trees, and complex compound trees.
Part 1: Backend - Laravel AdminController Datatable Configuration
To enable tree structures in your datatable, you must extend AdminController and set specific protected properties in your child controller’s __construct() method.
Configuration Properties
These properties control the behavior and structure of the data returned by thepostIndex endpoint.
| Property | Type | Default | Description |
|---|---|---|---|
table_structure_mode | string | 'flat' | Required. Defines the data structure. Options are: - 'flat': A standard paginated list. - 'simple_tree': A hierarchy from a single, self-referencing model. - 'compound_tree': A hierarchy from multiple, related models. |
table_lazy_load | string | 'backend' | For tree modes, defines the loading strategy. - 'backend': The API constructs and returns a paginated tree. Simpler for the frontend. - 'frontend': The API only returns one level at a time. The frontend must make new API calls to load children. More scalable. |
table_tree_parent_field | string | 'parentId' | For 'simple_tree' mode. The database column that stores the parent’s ID. |
table_tree_root_parent_value | mixed | null | For 'simple_tree' mode. The value in the parent field that identifies a root-level node. |
table_compound_tree_config | array | [] | For 'compound_tree' mode. A detailed configuration array defining the multi-model hierarchy. The structure is identical to option_compound_tree_config. |
Routes and API Interaction
- Endpoint: All datatable interactions use the same endpoint:
POST {controller-base}/index. - Lazy Loading (Frontend): When
table_lazy_loadis set to'frontend', your frontend datatable must make subsequent calls to this same endpoint to load children. The required payload depends on thetable_structure_mode: - Simple Tree: Send
{ "parentId": "ID_of_the_expanded_row" }. - Compound Tree: Send
{ "parentType": "type_of_the_row", "parentId": "prefixed_ID_of_the_row" }.
Usage Examples
Scenario 1: Standard Flat Table (Default)
No special configuration is needed. This is the default behavior.Scenario 2: Simple Tree with Backend Lazy Loading
The API does all the work of building the tree. The frontend simply needs to render the nestedchildren array. Best for small to medium-sized trees.
- Controller (
CategoryController.php)
- API Response: The
dataarray will contain only root-level items, but each item that has children will contain achildrenarray with its descendants.
Scenario 3: Simple Tree with Frontend Lazy Loading
The most scalable and performant option for large, deep hierarchies.- Controller (
OrgChartController.php)
- API Interaction:
- Initial Load:
POST /api/org-chart/indexwith pagination params returns paginated root-level employees. Each will have a_hasChildren: trueflag if they manage others. - Expanding a Row: When the user expands an employee with ID
123, the frontend makes a new call:POST /api/org-chart/indexwith the body{"parentId": "123"}. - The API responds with a flat array of all employees who report directly to employee
123.
Scenario 4: Compound Tree (Frontend Lazy Loading Only)
Used for complex hierarchies involving different models. Due to complexity, this mode enforces frontend lazy loading.- Controller (
DocumentBrowserController.php)
- API Interaction:
- Initial Load:
POST /api/doc-browser/indexreturns paginated root nodes (e.g.,_type: 'group'). - Expanding a Row: To expand a group with prefixed ID
group-45, the frontend callsPOST /api/doc-browser/indexwith the body{"parentType": "group", "parentId": "group-45"}. - The API responds with a flat array of all sections belonging to that group.
Dokumentasi (Bahasa Indonesia)
Struktur Datatable Lanjutan
Dokumen ini menjelaskan cara mengkonfigurasi dan menggunakan struktur data hierarkis tingkat lanjut untuk datatable utama dalam aplikasi Anda. Fitur ini dibangun di atas superclassAdminController untuk me-render daftar datar (flat list), pohon sederhana (simple tree), dan pohon gabungan (compound tree) yang kompleks.
Bagian 1: Backend - Konfigurasi Datatable AdminController
Untuk mengaktifkan struktur pohon di datatable Anda, Anda harus extend AdminController dan mengatur properti protected tertentu di dalam metode __construct() pada controller turunan Anda.
Properti Konfigurasi
Properti ini mengontrol perilaku dan struktur data yang dikembalikan oleh endpointpostIndex.
| Properti | Tipe | Default | Deskripsi |
|---|---|---|---|
table_structure_mode | string | 'flat' | Wajib. Mendefinisikan struktur data. Pilihan: - 'flat': Daftar terpaginasi standar. - 'simple_tree': Hierarki dari satu model tunggal yang memiliki relasi ke dirinya sendiri. - 'compound_tree': Hierarki dari beberapa model yang saling berelasi. |
table_lazy_load | string | 'backend' | Untuk mode pohon, mendefinisikan strategi pemuatan data. - 'backend': API akan membangun dan mengembalikan pohon data yang terpaginasi. Lebih sederhana untuk frontend. - 'frontend': API hanya mengembalikan satu level data pada satu waktu. Frontend harus membuat panggilan API baru untuk memuat turunan (children). Lebih skalabel. |
table_tree_parent_field | string | 'parentId' | Untuk mode 'simple_tree'. Kolom database yang menyimpan ID dari induk. |
table_tree_root_parent_value | mixed | null | Untuk mode 'simple_tree'. Nilai di kolom induk yang mengidentifikasi node level akar. |
table_compound_tree_config | array | [] | Untuk mode 'compound_tree'. Array konfigurasi detail yang mendefinisikan hierarki multi-model. Strukturnya identik dengan option_compound_tree_config. |
Routes dan Interaksi API
- Endpoint: Semua interaksi datatable menggunakan endpoint yang sama:
POST {controller-base}/index. - Lazy Loading (Frontend): Ketika
table_lazy_loaddiatur ke'frontend', datatable di frontend Anda harus melakukan panggilan berikutnya ke endpoint yang sama untuk memuat turunan. Payload yang diperlukan tergantung padatable_structure_mode: - Simple Tree: Kirim
{ "parentId": "ID_baris_yang_diekspansi" }. - Compound Tree: Kirim
{ "parentType": "tipe_baris", "parentId": "ID_dengan_prefix" }.
Contoh Penggunaan
Skenario 1: Tabel Datar Standar (Default)
Tidak diperlukan konfigurasi khusus. Ini adalah perilaku default.Skenario 2: Pohon Sederhana dengan Backend Lazy Loading
API akan melakukan semua pekerjaan untuk membangun pohon data. Frontend hanya perlu me-render arraychildren yang bersarang. Paling baik untuk pohon data berukuran kecil hingga sedang.
- Controller (
CategoryController.php)
- Respon API: Array
dataakan berisi item level akar saja, tetapi setiap item yang memiliki turunan akan berisi arraychildrendengan data turunannya.
Skenario 3: Pohon Sederhana dengan Frontend Lazy Loading
Opsi yang paling skalabel dan berkinerja tinggi untuk hierarki yang besar dan dalam.- Controller (
OrgChartController.php)
- Interaksi API:
- Panggilan Awal:
POST /api/org-chart/indexdengan parameter paginasi akan mengembalikan karyawan level akar yang terpaginasi. Masing-masing akan memiliki flag_hasChildren: truejika mereka memiliki bawahan. - Mengekspansi Baris: Saat pengguna mengekspansi seorang karyawan dengan ID
123, frontend membuat panggilan baru:POST /api/org-chart/indexdengan body{"parentId": "123"}. - API akan merespons dengan array datar dari semua karyawan yang melapor langsung ke karyawan
123.
Skenario 4: Pohon Gabungan (Frontend Lazy Loading Only)
Digunakan untuk hierarki kompleks yang melibatkan model berbeda. Karena kompleksitasnya, mode ini memaksakan penggunaan frontend lazy loading.- Controller (
DocumentBrowserController.php)
- Interaksi API:
- Panggilan Awal:
POST /api/doc-browser/indexmengembalikan node akar yang terpaginasi (misal,_type: 'group'). - Mengekspansi Baris: Untuk mengekspansi sebuah grup dengan ID ber-prefix
group-45, frontend memanggilPOST /api/doc-browser/indexdengan body{"parentType": "group", "parentId": "group-45"}. - API merespons dengan array datar dari semua seksi yang dimiliki oleh grup tersebut.
