Type to search columns, filters, options, and extensions.

↑↓ to navigate · Enter to open · Esc to close

Documentation

Boolean Column

Render boolean values as toggle switches with optional AJAX update support

BooleanColumn renders a true/false (or 1/0) field as text by default. When combined with renderAsSwitch(), the cell becomes an interactive toggle switch and toggling it sends an AJAX request to update the field server-side.

Switch markup is produced by a frontend column style adapter: a Bootstrap 5 form switch when a Bootstrap DataTables stylesheet is detected, or a Tailwind toggle otherwise (including the default dt theme).

Basic Usage

use Pentiminax\UX\DataTables\Column\BooleanColumn;

BooleanColumn::new('active', 'Status');

Renders the raw boolean value as text. Not interactive. For a read-only check or cross, use IconColumn::boolean().

Interactive Toggle Switch

renderAsSwitch() turns the cell into a live switch. Clicking it sends an AJAX PATCH request to update the field.

BooleanColumn::new('active', 'Status')
    ->renderAsSwitch();

Set the initial state of the switch when the row value is not available:

BooleanColumn::new('active', 'Status')
    ->renderAsSwitch(defaultState: false);

Entity Class

The AJAX request includes the entity class so the backend knows which object to update. When using #[AsDataTable(Entity::class)], it is injected automatically. For manual tables, set it explicitly:

BooleanColumn::new('active', 'Status')
    ->renderAsSwitch()
    ->setEntityClass(User::class);

When the column targets another entity than the table’s, the request also carries the table row it sits in. The server checks that this row is inside the table scope and that its setToggleAjax() id field resolves to the entity being toggled; otherwise it answers 404, as it does for a request without rowId or on a table with a composite identifier.

AJAX Configuration

Customize the identifier field and HTTP method used for the toggle request:

BooleanColumn::new('active', 'Status')
    ->renderAsSwitch()
    ->setToggleAjax(idField: 'uuid', method: 'PUT');

The default is id + PATCH.

Route Import

The toggle endpoint is provided by the bundle. The Symfony Flex recipe already imports its routes; without Flex, import them once in your project:

// config/routes/ux_datatables.php
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

return static function (RoutingConfigurator $routes): void {
    $routes->import('@PentiminaxDataTablesBundle/config/routes.php');
};

API Reference

MethodDescription
BooleanColumn::new(string $name, string $title = '')Creates a new BooleanColumn (type: num)
renderAsSwitch(bool $defaultState = false)Enable switch rendering with AJAX toggle
setEntityClass(string $class)Set the Doctrine entity class for the AJAX request (auto-injected when using #[AsDataTable])
setToggleAjax(string $idField = 'id', string $method = 'PATCH')Customize the identifier field and HTTP method for the toggle request
getDefaultState(): boolReturns the configured default switch state
getToggleField(): ?stringReturns the field name used in the toggle request

Complete Example

use Pentiminax\UX\DataTables\Attribute\AsDataTable;
use Pentiminax\UX\DataTables\Column\BooleanColumn;
use Pentiminax\UX\DataTables\Column\NumberColumn;
use Pentiminax\UX\DataTables\Column\TextColumn;
use Pentiminax\UX\DataTables\Model\AbstractDataTable;

#[AsDataTable(User::class)]
final class UsersDataTable extends AbstractDataTable
{
    public function configureColumns(): iterable
    {
        yield NumberColumn::new('id', 'ID');
        yield TextColumn::new('email', 'Email');
        yield BooleanColumn::new('active', 'Active')
            ->renderAsSwitch();
        // Entity class is injected automatically from #[AsDataTable(User::class)]
    }

    protected function mapRow(mixed $row): array
    {
        return [
            'id'     => $row->getId(),
            'email'  => $row->getEmail(),
            'active' => $row->isActive(),
        ];
    }
}