Conditional Action Rules โ
Use Action Rules to change a row, button, checkbox, radio, toggle switch, or detail toggle when a condition matches. Register them in actionRules(). Rules run on the server while PowerGrid renders the rows.
Defining Action Rules โ
A Rule targets one element: rows, buttons, checkboxes, or radios. Start with Rule::button(), Rule::rows(), Rule::checkbox(), or Rule::radio(), then chain one condition and one or more modifiers.
Each Rule accepts one condition: when(), or on Rule::rows() also loop(), firstOnPage(), lastOnPage(), or alternating(). Combining two conditions on the same Rule throws InvalidArgumentException. A Rule with no condition never applies.
actionRules() may receive the current $row. Closures passed to when() always receive it.
In the example below:
- The Order button text and style change when the product price is below 100.
- The Order button is hidden when the product is out of stock.
- Checkboxes are hidden when the product is out of stock.
- The row background changes when the product is out of stock.
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\Facades\Rule;
class ProductTable extends PowerGridComponent
{
public function actionRules(): array
{
return [
Rule::button('order-product')
->when(fn ($product) => $product->price < 100)
->slot('Order NOW! ๐ฅ')
->setAttribute('class', '!bg-orange-400'),
Rule::button('order-product')
->when(fn ($product) => $product->in_stock === false)
->hide(),
Rule::checkbox()
->when(fn ($product) => $product->in_stock === false)
->hide(),
Rule::rows()
->when(fn ($product) => $product->in_stock === false)
->setAttribute('class', '!bg-red-200'),
];
}
}actionRules() applies to row buttons, not header buttons. Hide a header button with Button::can().
Modifiers โ
Modifiers run when the Rule condition is true.
[[sub-toc]]
disable() โ
Disables the target (buttons, checkboxes, and radios). On a button this sets the HTML disabled attribute. Disabled checkboxes are skipped by "select all".
use PowerComponents\LivewirePowerGrid\Facades\Rule;
Rule::button('order-product')
->when(fn ($product) => $product->in_stock === false)
->disable(),dispatch() โ
Sets the Livewire event dispatched by a button (wire:click="$dispatch(...)"). Parameters are JSON-encoded as you pass them โ use $row for row values.
| Parameter | Description |
|---|---|
| (string) $event | Name of event |
| (array) $params | Parameters |
TIP
See Dispatch in the Livewire documentation.
use PowerComponents\LivewirePowerGrid\Facades\Rule;
public function actionRules($row): array
{
return [
Rule::button('order-product')
->when(fn ($product) => $product->is_spicy === true)
->dispatch('showSpiceAlert', ['id' => $row->id]),
];
}hide() โ
Hides the target (buttons, checkboxes, and radios). Hidden checkboxes are also skipped by "select all".
use PowerComponents\LivewirePowerGrid\Facades\Rule;
Rule::checkbox()
->when(fn ($product) => $product->in_stock === false)
->hide(),hideToggleable() โ
Hides every Toggleable switch on the row and shows the Yes/No labels instead. See also showToggleable() and Toggleable column.
use PowerComponents\LivewirePowerGrid\Facades\Rule;
Rule::rows()
->when(fn ($product) => $product->trashed())
->hideToggleable(),showToggleable() โ
Shows every Toggleable switch on the row. See also hideToggleable().
use PowerComponents\LivewirePowerGrid\Facades\Rule;
Rule::rows()
->when(fn ($product) => ! $product->trashed())
->showToggleable(),loop() โ
A condition (not a modifier) for Rule::rows(): tests the current page's loop (index, first, last, even, odd, โฆ) instead of the row. Same one-condition limit as when().
Shortcuts: firstOnPage(), lastOnPage(), and alternating() (even rows).
use PowerComponents\LivewirePowerGrid\Facades\Rule;
Rule::rows()
->loop(fn ($loop) => $loop->index % 2)
->setAttribute('class', '!bg-gunmetal-100'),
Rule::rows()
->alternating()
->setAttribute('class', '!bg-gunmetal-100'),slot() โ
Replaces the button caption.
| Parameter |
|---|
| (string) $slot |
use PowerComponents\LivewirePowerGrid\Facades\Rule;
Rule::button('order-product')
->when(fn ($product) => $product->on_sale === true)
->slot('Order ๐ฐ ON SALE ๐ฐ'),setAttribute() โ
Sets an HTML attribute on the target.
On buttons, you may chain several setAttribute() calls. On rows, each call replaces the previous one โ use a second Rule::rows() for another attribute.
| Parameter | Description |
|---|---|
| (string) $attribute | HTML attribute (class, id, x-on:click โฆ) |
| (string) $value | Attribute value |
use PowerComponents\LivewirePowerGrid\Facades\Rule;
Rule::rows()
->when(fn ($product) => $product->in_stock === false)
->setAttribute('class', '!bg-red-200'),
Rule::button('order-product')
->when(fn ($product) => $product->in_stock === false)
->setAttribute('class', '!bg-red-200')
->setAttribute('title', 'Out of stock'),bladeComponent() โ
Replaces the button with a Blade component. Pass concrete values (use $row); field-name placeholders are not interpolated.
| Parameter | Description |
|---|---|
| (string) $component | View component path (Blade) |
| (array) $params | Component attributes |
use PowerComponents\LivewirePowerGrid\Facades\Rule;
public function actionRules($row): array
{
return [
Rule::button('order-product')
->when(fn ($product) => $product->in_stock === false)
->bladeComponent('another-custom-button', ['productId' => $row->id]),
];
}hideToggleDetail() / showToggleDetail() โ
Hides or shows the Detail Row collapse icon on the row.
use PowerComponents\LivewirePowerGrid\Facades\Rule;
Rule::rows()
->when(fn ($product) => $product->trashed())
->hideToggleDetail(),disableEditOnClick() / enableEditOnClick() โ
Disables or enables every Edit On Click cell on the row.
use PowerComponents\LivewirePowerGrid\Facades\Rule;
Rule::rows()
->when(fn ($product) => $product->trashed())
->disableEditOnClick(),detailView() โ
Overrides the Detail Row view and options for the matching row.
use PowerComponents\LivewirePowerGrid\Facades\Rule;
Rule::rows()
->when(fn ($product) => $product->has_extras)
->detailView('components.product-detail', ['showCalories' => true]),Toggleable column โ
Hide or show a single Toggleable column. Rule::rows()->hideToggleable() still hides every switch on the row.
use PowerComponents\LivewirePowerGrid\Facades\Rule;
Rule::toggleable('in_stock')
->when(fn ($product) => $product->trashed())
->hide(),show() is the counterpart.
Radio โ
Same modifiers as checkboxes: hide(), disable(), setAttribute().
use PowerComponents\LivewirePowerGrid\Facades\Rule;
Rule::radio()
->when(fn ($product) => $product->in_stock === false)
->disable(),Applying Conditional Data Formatting โ
To change how a value is displayed (currency, labels, HTML), use Custom Fields rather than Action Rules.
Performance โ
Rules run on the server for each row. For heavy per-row markup, prefer Actions From Blade View or Button::can() / Button::view() instead of stacking many rules.
