Exporters
A data table can be exported to various formats, using exporters, each of which are built with the help of an exporter type.
Prerequisites
If you plan to use the built-in PhpSpreadsheet exporter types, make sure you have the PhpSpreadsheet installed:
composer require phpoffice/phpspreadsheet
Configuring the exporting feature
By default, the exporting is enabled for every data table type.
Every part of the exporting feature can be configured using the data table options:
exporting_enabled- to enable/disable feature completely;
Built-in exporter types
The following exporter types are natively available in the bundle:
Creating custom exporter type
TODO
Downloading the file
To download an export file, use the export() method on the data table.
If you're using data tables in controllers, use it in combination with isExporting() method:
// src/Controller/ProductController.php
namespace App\Controller;
use App\DataTable\Type\ProductType;
use App\Repository\ProductRepository;
use Kreyu\Bundle\DataTableBundle\DataTableControllerTrait;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ProductController extends AbstractController
{
use DataTableControllerTrait;
public function index(Request $request, ProductRepository $repository): Response
{
// ...
$dataTable = $this->createDataTable(ProductType::class, $query);
$dataTable->handleRequest($request);
if ($dataTable->isExporting()) {
return $this->file($dataTable->export());
}
// ...
}
}