Exporting
Introduction
Tables can be exported to different formats such as CSV, Excel, and PDF. Before continuing, make sure you have installed the Laravel Excel package. You can install it via composer:
composer require maatwebsite/excel
Defining Exports
The Table
class has an exports()
method that can be used to define all the export options. Simply create an Export
instance with the static make()
method and return it from the exports()
method.
use InertiaUI\Table\Table;
class Users extends Table
{
public function exports(): array
{
return [
Export::make(),
];
}
}
By default, the export is set to the Excel type and has a label of Excel Export. The make()
method accepts five arguments to customize the export:
use Maatwebsite\Excel\Excel;
Export::make(
label: 'CSV Export',
filename: 'users.csv',
type: Excel::CSV,
authorize: true,
events: []
);
The type must be one of the constants from the Maatwebsite\Excel\Excel
class. Note that some types, like PDF, require additional configuration.
Filtering and Selected Rows
By default, the export will include all rows, even if you have applied filters or selected rows in the frontend. You can change this behavior by passing the limitToFilteredRows
or limitToSelectedRows
options to the make()
method.
Export::make(
limitToFilteredRows: true,
limitToSelectedRows: true,
);
Alternatively, you can call the limitToFilteredRows()
or limitToSelectedRows()
methods.
Export::make()
->limitToFilteredRows()
->limitToSelectedRows();
If you want this to be the default for all tables, you may call the static defaultLimitToFilteredRows()
and defaultLimitToSelectedRows()
methods on the Table class, for example, in your AppServiceProvider
:
use InertiaUI\Table\Export;
Export::defaultLimitToFilteredRows();
Export::defaultLimitToSelectedRows();
Authorization
Authorization works the same way as with Row Actions. The authorize
option can be a boolean or a Closure.
Export Events
The events
array enables you to hook into the underlying package events.
use Maatwebsite\Excel\Events\BeforeExport;
Export::make(events: [
BeforeExport::class => function (BeforeExport $event) {
$event->writer->getProperties()->setCreator('John Doe');
},
]);
Check out the documentation for more information.
Customizing Exports
You can customize how each column is exported.
Column Mapping
If you want to change the content of a column when exporting, you can pass a Closure to the exportUsing
argument. Alternatively, you can call the exportUsing()
method.
TextColumn::make('reference', exportAs: function (string $reference) {
return "#REF-{$reference}";
});
The closure accepts a second argument, which is the Model instance.
TextColumn::make('name')->exportAs(function (string $name, User $user) {
return $user->is_active ? $name : "Inactive: {$name}";
});
Column Formatting
You may choose a custom format for each column by passing a Closure or string to the exportFormat
argument. Alternatively, you can call the exportFormat()
method.
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
TextColumn::make('amount', exportFormat: NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
TextColumn::make('amount')->exportFormat(NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
Column Styling
You can customize the column styling by passing a Closure or array to the exportStyle
argument. Alternatively, you can call the exportStyle()
method.
use PhpOffice\PhpSpreadsheet\Style\Style;
TextColumn::make('name', exportStyle: ['font' => ['bold' => true]]);
TextColumn::make('email')->exportStyle(function (Style $style) {
$style->getFont()->setBold(true);
});
Exclude Columns
To exclude a column from being exported, pass false
to the exportAs
argument, or call the dontExport()
method.
TextColumn::make('Name', exportAs: false);
TextColumn::make('Name')->dontExport();
Define a Custom Export
If you need more control over the export, you can write your own logic to export the data. You can do this with the using
argument or the using()
method.
use App\Tables\Users;
use InertiaUI\Table\Export;
use InertiaUI\Table\Http\ExportRequest;
Export::make()->using(function (Users $table, Export $export, ExportRequest $request) {
// return response()->download(...);
});
You may also pass an invokable class:
Export::make(using: CustomUserExport::class);
Data Attributes
You can add Data Attributes to the Export button by using the dataAttributes
argument in the make()
method or by using the dataAttributes
method. You don't have to prefix the keys with data-
.
Export::make()->dataAttributes([
'contents' => 'all-users',
]);
Targetting Exports with CSS
You can target the Export button with specific data attributes using CSS. For example, to style the Export with the data-content="all-users"
attribute, you can use the following CSS:
a[data-content="all-users"] {
/* ... */
}
Metadata
You can add metadata to Exports by using the meta
argument in the make()
or by using the meta()
method.
Export::make()->meta([
'key' => 'value',
]);
In the default Table component, this metadata is unused and not passed to any DOM element. However, this feature can be useful when extending the Table component using slots, or when you build a custom Table component.