You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/table-component/component-configuration.md
+108-5Lines changed: 108 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,7 @@ use Illuminate\View\View; // [!code ++]
48
48
class DishTable extends PowerGridComponent
49
49
{
50
50
public function noDataLabel(): string|View // [!code ++:5]
51
-
{
51
+
{
52
52
//return 'We could not find any dish matching your search.';
53
53
return view('dishes.no-data');
54
54
}
@@ -92,7 +92,7 @@ use PowerComponents\LivewirePowerGrid\PowerGridComponent;
92
92
class DishTable extends PowerGridComponent
93
93
{
94
94
public bool $deferLoading = true;// [!code ++:3]
95
-
95
+
96
96
public string $loadingComponent = 'components.my-custom-loading';
97
97
}
98
98
```
@@ -442,7 +442,7 @@ class DishTable extends PowerGridComponent
442
442
public function setUp(): array
443
443
{
444
444
$this->persist(// [!code ++:4]
445
-
tableItems: ['columns', 'filters', 'sort'],
445
+
tableItems: ['columns', 'filters', 'sort'],
446
446
prefix: auth()->id
447
447
);
448
448
}
@@ -496,7 +496,7 @@ use PowerComponents\LivewirePowerGrid\Facades\PowerGrid; // [!code ++]
496
496
class DishTable extends PowerGridComponent
497
497
{
498
498
public function setUp(): array
499
-
{
499
+
{
500
500
return [
501
501
PowerGrid::cache() // [!code ++:3]
502
502
->ttl(60) // [!code ++:3]
@@ -512,7 +512,7 @@ You may also use your own custom tag, as demonstrated below.
512
512
use PowerComponents\LivewirePowerGrid\Cache;
513
513
514
514
public function setUp(): array
515
-
{
515
+
{
516
516
return [
517
517
PowerGrid::cache()// [!code ++:3]
518
518
->ttl(60) // [!code ++:3]
@@ -603,3 +603,106 @@ If you need to add a custom event to your PowerGrid Table, just override the met
603
603
];
604
604
}
605
605
```
606
+
607
+
## Lifecycle Hooks
608
+
609
+
PowerGrid provides three lifecycle hooks that allow you to modify data at key stages of the rendering pipeline. All hooks are defined with no-op defaults, so they are fully opt-in.
610
+
611
+
These hooks are intended for advanced use cases where you need to adjust the query, rows, or resolved actions after PowerGrid has applied filters, search, and sorting, but before rendering or dispatching data to the frontend.
612
+
613
+
### Hook Overview
614
+
615
+
-`transformQuery(mixed $query): mixed`
616
+
- Modify the query after filters, search, and sorting are applied, but before pagination executes it. Useful for eager loading, subqueries, withCount(), or any query-level adjustments that depend on the current filter state.
617
+
618
+
-`transformRows(Collection $rows): Collection`
619
+
- Modify the paginated rows after the query has executed and data has been transformed, but before it is passed to the view. Runs on the current page's rows only. Ideal for enriching rows with data from external APIs or computed values not available at query time.
- Modify the resolved actions array before it is dispatched to the frontend via JavaScript. Receives both the `$actionsByRow` dictionary (keyed by primary key) and the current `$rows` collection. Useful for dynamically changing button labels, visibility, or attributes based on data not available during `actions()` resolution.
623
+
624
+
### Examples
625
+
626
+
#### transformQuery — add counts and a subselect
627
+
628
+
```php
629
+
public function transformQuery(mixed $query): mixed
630
+
{
631
+
return $query
632
+
->withCount('comments')
633
+
->addSelect(DB::raw('(SELECT COUNT(*) FROM reviews WHERE reviews.dish_id = dishes.id) as review_count'));
634
+
}
635
+
```
636
+
637
+
#### transformRows — enrich rows with external API data
638
+
639
+
```php
640
+
use Illuminate\Support\Collection;
641
+
642
+
public function transformRows(Collection $rows): Collection
643
+
{
644
+
$ids = $rows->pluck('id')->toArray();
645
+
$apiData = ExternalApi::getByIds($ids);
646
+
647
+
return $rows->map(function ($row) use ($apiData) {
★ transformQuery() ← modify the builder before it hits the DB
686
+
│
687
+
▼
688
+
Pagination
689
+
│
690
+
▼
691
+
Data Transformation
692
+
│
693
+
▼
694
+
★ transformActions() ← modify resolved actions before JS dispatch
695
+
│
696
+
▼
697
+
★ transformRows() ← enrich/modify rows before the view
698
+
│
699
+
▼
700
+
Render
701
+
```
702
+
703
+
### Notes and Best Practices
704
+
705
+
- Use `transformQuery` for query-level modifications only. Avoid executing heavy per-row logic inside this hook — use `transformRows` for per-row enrichment.
706
+
-`transformRows` runs only on the current page's rows (after pagination). If you need to enrich all rows regardless of pagination, run a separate process or fetch the required data aggregated by IDs.
707
+
-`transformActions` receives the already-resolved actions array. Modify keys such as `slot`, `can`, `attributes`, `icon`, `iconAttributes`, etc., but avoid mutating the structure unexpectedly.
708
+
- Because hooks run on the server side, keep I/O and long-running operations under control; consider caching results when calling external services.
0 commit comments