Documentation: Advanced Use Cases for Compound Tree Datatables
This document provides practical examples of how to implement complex, multi-level hierarchical datatables using the'compound_tree' mode. These use cases demonstrate how to handle hierarchies from both a single, multi-purpose model and multiple distinct models.
Use Case 1: Document Storage Hierarchy
Scenario: A physical document storage system is modeled asLocation -> Warehouse -> Rack -> Box -> Document. The first four levels (Location, Warehouse, Rack, Box) are all stored in a single Storage model, distinguished by a type column. The final level, Document, is a separate model.
Objective:
- Display the full hierarchy in a single datatable.
- Only allow the final leaf nodes (
Document) to be selected. - Use frontend lazy loading for performance.
Backend Setup (StorageBrowserController.php)
The key to this setup is using the additionalQuery hook to filter the Storage model by its type for each level of the hierarchy.
Frontend Parent Component
The frontend implementation is standard. The@load-children handler will send the parentType (e.g., 'location'), and the backend’s additionalQuery will use this information to correctly filter for the children (e.g., storage_type = 'warehouse').
Use Case 2: CMS Content Hierarchy
Scenario: A classic Content Management System with a structure ofGroup -> Section -> Category -> Article. Each is a distinct model. Additionally, Category can have sub-categories (a self-referencing hierarchy).
Objective:
- Display the complete content structure.
- Prevent selection of organizational containers (
Group,Section). - Allow selection of
CategoryandArticlenodes. - Categories are only selectable if they have a status of
'published'.
Backend Setup (CmsContentController.php)
This example showcases defining selectable_checker closures at different levels for granular control.
Frontend Parent Component
The frontend implementation is straightforward. It makes an initial call to the endpoint and then uses the@load-children handler to lazy-load the different levels. The backend’s configuration automatically handles the relationships and selectability rules. The frontend just needs to render the _selectable state provided in the data.
Dokumentasi (Bahasa Indonesia)
Studi Kasus Lanjutan untuk Datatable Pohon Gabungan (Compound Tree)
Dokumen ini memberikan contoh praktis tentang cara mengimplementasikan datatable hierarkis yang kompleks dan multi-level menggunakan mode'compound_tree'. Studi kasus ini menunjukkan cara menangani hierarki yang berasal dari satu model serbaguna dan dari beberapa model yang berbeda.
Studi Kasus 1: Hierarki Penyimpanan Dokumen
Skenario: Sebuah sistem penyimpanan dokumen fisik dimodelkan sebagaiLokasi -> Gudang -> Rak -> Boks -> Dokumen. Empat level pertama (Lokasi, Gudang, Rak, Boks) semuanya disimpan dalam satu model Storage, yang dibedakan oleh kolom type. Level terakhir, Dokumen, adalah model terpisah.
Tujuan:
- Menampilkan hierarki lengkap dalam satu datatable.
- Hanya mengizinkan leaf node terakhir (
Dokumen) yang dapat dipilih. - Menggunakan frontend lazy loading untuk performa.
Pengaturan Backend (StorageBrowserController.php)
Kunci dari pengaturan ini adalah menggunakan hook additionalQuery untuk memfilter model Storage berdasarkan kolom type-nya untuk setiap level hierarki.
Komponen Induk Frontend
Implementasi di frontend bersifat standar. Handler@load-children akan mengirim parentType (misalnya, 'location'), dan additionalQuery di backend akan menggunakan informasi ini untuk memfilter anak-anaknya dengan benar (misalnya, storage_type = 'warehouse').
Studi Kasus 2: Hierarki Konten CMS
Skenario: Sebuah Content Management System klasik dengan strukturGrup -> Seksi -> Kategori -> Artikel. Masing-masing adalah model yang berbeda. Selain itu, Kategori dapat memiliki sub-kategori (hierarki self-referencing).
Tujuan:
- Menampilkan struktur konten lengkap.
- Mencegah pemilihan kontainer organisasional (
Grup,Seksi). - Mengizinkan pemilihan
KategoridanArtikel. - Kategori hanya dapat dipilih jika statusnya
'published'.
Pengaturan Backend (CmsContentController.php)
Contoh ini menonjolkan pendefinisian closure selectable_checker di level yang berbeda untuk kontrol yang lebih terperinci.
Komponen Induk Frontend
Implementasi di frontend sangat lugas. Ia melakukan panggilan awal ke endpoint dan kemudian menggunakan handler@load-children untuk memuat level-level yang berbeda secara lazy-load. Konfigurasi di backend secara otomatis menangani relasi dan aturan seleksi. Frontend hanya perlu me-render status _selectable yang disediakan dalam data.