Buttons Extension

When To Use

Use Buttons when users need export actions (CSV, Excel, PDF, Print) or column visibility controls.

Minimal Example

use Pentiminax\UX\DataTables\Enum\ButtonType;
use Pentiminax\UX\DataTables\Enum\Feature;
use Pentiminax\UX\DataTables\Model\Extensions\ButtonsExtension;

$dataTable
    ->addExtension(new ButtonsExtension([
        ButtonType::COPY,
        ButtonType::CSV,
        ButtonType::EXCEL,
    ]))
    ->layout([
        'topStart' => Feature::BUTTONS,
        'topEnd' => Feature::SEARCH,
    ]);

Buttons is a layout-aware extension: ButtonsExtension configures the buttons, and Feature::BUTTONS tells DataTables where to render the .dt-buttons container. Without Feature::BUTTONS in layout(), no buttons container is generated.

Available Button Types

Button type Purpose
`ButtonType::COPY`Copy table data to clipboard
`ButtonType::CSV`Export CSV file
`ButtonType::EXCEL`Export Excel file
`ButtonType::PDF`Export PDF file
`ButtonType::PRINT`Open print view
`ButtonType::COLUMN_VISIBILITY`Toggle column visibility

Advanced Example

$buttons = (new ButtonsExtension([ButtonType::CSV]))
    ->withExcelButton()
    ->withPrintButton();

$dataTable
    ->addExtension($buttons)
    ->layout([
        'topStart' => Feature::BUTTONS,
    ]);

Customizing Buttons

Use Button objects when you need DataTables button options such as text, className, exportOptions, or export-specific options like filename.

use Pentiminax\UX\DataTables\Enum\Feature;
use Pentiminax\UX\DataTables\Model\Extensions\Button;
use Pentiminax\UX\DataTables\Model\Extensions\ButtonsExtension;

$dataTable
    ->addExtension(new ButtonsExtension([
        Button::csv()
            ->text('Export CSV')
            ->className('btn btn-sm btn-outline-primary')
            ->exportOptions(['columns' => ':visible:not(.not-exportable)']),
        Button::excel()
            ->text('Excel')
            ->option('filename', 'users-export'),
        Button::colVis()->text('Columns'),
    ]))
    ->layout([
        'topStart' => Feature::BUTTONS,
        'topEnd' => Feature::SEARCH,
    ]);

Button options must be JSON-serializable. JavaScript callbacks such as DataTables action or customize functions cannot be serialized from PHP.

For options not covered by the typed API, you can still pass a raw DataTables layout object:

$dataTable->layout([
    'topStart' => [
        'buttons' => [
            [
                'extend' => 'csv',
                'text' => 'Export CSV',
                'className' => 'btn btn-primary',
                'exportOptions' => ['columns' => ':visible'],
            ],
        ],
    ],
]);

Frequent Pitfalls

  • Adding ButtonsExtension without adding Feature::BUTTONS to the layout.
  • Mixing button values as invalid strings.
  • Forgetting that non-column-visibility buttons apply export filtering by default.