Usage
Creating data tables
Data tables are defined using a type classes. Those classes implement DataTableTypeInterface, although, it is recommended to extend from the AbstractDataTableType class:
namespace App\DataTable\Type;
use Kreyu\Bundle\DataTableBundle\Type\AbstractDataTableType;
use Kreyu\Bundle\DataTableBundle\DataTableBuilderInterface;
class ProductDataTableType extends AbstractDataTableType
{
public function buildDataTable(DataTableBuilderInterface $builder, array $options): void
{
// Define column, filters, actions, exporters, etc...
}
}
Recommended namespace for the column type classes is App\DataTable\Type\
.
From here, you can add columns, filters, actions and exporters.
In most cases, the data tables are created in the controller, using the createDataTable()
method from the DataTableFactoryAwareTrait
.
use App\Repository\ProductRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Kreyu\Bundle\DataTableBundle\DataTableFactoryAwareTrait;
class ProductController extends AbstractController
{
use DataTableFactoryAwareTrait;
public function index(ProductRepository $productRepository)
{
$query = $productRepository->createQueryBuilder('product');
$dataTable = $this->createDataTable(ProductDataTableType::class, $query);
}
}
This method accepts three arguments:
- type — with a fully qualified class name;
- data — in most cases, an instance of Doctrine ORM query builder;
- options — defined by the data table type, used to configure the data table;
In above example, we're passing an instance of Doctrine ORM query builder as data, not results. This allows the bundle to paginate the results, apply filtration, and more.
Handling the request
In order to be able to paginate, sort, filter, personalize or export the data table, call the handleRequest()
method of the data table:
use App\Repository\ProductRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Kreyu\Bundle\DataTableBundle\DataTableFactoryAwareTrait;
class ProductController extends AbstractController
{
use DataTableFactoryAwareTrait;
public function index(Request $request, ProductRepository $productRepository)
{
$query = $productRepository->createQueryBuilder('product');
$dataTable = $this->createDataTable(ProductDataTableType::class, $query);
$dataTable->handleRequest($request);
}
}
Rendering the data tables
In order to render the data table, create the data table view and pass it to the template:
use App\Repository\ProductRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Kreyu\Bundle\DataTableBundle\DataTableFactoryAwareTrait;
class ProductController extends AbstractController
{
use DataTableFactoryAwareTrait;
public function index(Request $request, ProductRepository $productRepository)
{
$query = $productRepository->createQueryBuilder('product');
$dataTable = $this->createDataTable(ProductDataTableType::class, $query);
$dataTable->handleRequest($request);
return $this->render('product/index.html.twig', [
'products' => $dataTable->createView(),
]);
}
}
Now, in the template, render the data table using the data_table
function:
{# product/index.html.twig #}
<div class="card">
{{ data_table(products) }}
</div>
By default, the data table will look somewhat ugly, because we haven't configured the theme yet - see theming documentation section.