freezy-bee / datagrid-bundle
Symfony datagrid
Package info
github.com/FreezyBee/DataGridBundle
Type:symfony-bundle
pkg:composer/freezy-bee/datagrid-bundle
Requires
- php: ^8.5
- ext-json: *
- doctrine/orm: ^3.0
- symfony/config: ^8.0
- symfony/dependency-injection: ^8.0
- symfony/event-dispatcher: ^8.0
- symfony/property-access: ^8.0
- symfony/serializer: ^8.0
- symfony/translation: ^8.0
- symfony/yaml: ^8.0
- twig/twig: ^3.0
Requires (Dev)
- doctrine/doctrine-bundle: ^3.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^13.0
- squizlabs/php_codesniffer: ^4.0
- symfony/browser-kit: ^8.0
- symfony/css-selector: ^8.0
- symfony/framework-bundle: ^8.0
- symfony/phpunit-bridge: ^8.0
- symfony/twig-bundle: ^8.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Requirements
FreezyBee/DataGridBundle requires PHP 8.5 or higher and Symfony 8.0 or higher
Installation
composer require freezy-bee/datagrid-bundle
Register the bundle (if you don't use Symfony Flex) in config/bundles.php:
return [ // ... FreezyBee\DataGridBundle\FreezyBeeDataGridBundle::class => ['all' => true], ];
Import the bundle's routes in config/routes.yaml:
freezy_bee_data_grid: resource: '@FreezyBeeDataGridBundle/config/routes.php'
Usage
Create a grid type by implementing DataGridTypeInterface:
use FreezyBee\DataGridBundle\Attribute\AsDataGridType; use FreezyBee\DataGridBundle\DataGridBuilder; use FreezyBee\DataGridBundle\DataGridTypeInterface; use FreezyBee\DataGridBundle\DataSource\DoctrineDataSource; #[AsDataGridType('user-grid')] class UserGridType implements DataGridTypeInterface { public function __construct(private EntityManagerInterface $entityManager) { } public function buildGrid(DataGridBuilder $builder): void { $qb = $this->entityManager->createQueryBuilder() ->select('u') ->from(User::class, 'u'); $builder->setDataSource(new DoctrineDataSource($qb, 'u')); $builder->addText('name', 'Name') ->setFilterable() ->setSortable(); $builder->addDateTime('createdAt', 'Created') ->setSortable(); } }
The #[AsDataGridType] attribute gives the grid a stable, URL-safe name used for the AJAX and
export routes. It is optional – without it, the name is derived from the class name
(e.g. UserGridType becomes user-grid-type). The name must only contain letters, digits, - and
_, and must be unique across all registered grid types.
The grid type class is a regular autowired service, so you can inject any dependency (entity manager, security, …) through the constructor.
Render the grid in a controller and template:
public function list(DataGridFactory $dataGridFactory): Response { return $this->render('user/list.html.twig', [ 'grid' => $dataGridFactory->create(UserGridType::class), ]); }
{{ datagrid(grid) }}
Translations
All grid chrome (pagination, search, filter placeholders, the boolean column labels, …) is
translated through the datagrid domain (translations/datagrid.*.yaml). Czech (cs) and
English (en) are bundled.
To add another locale or override a message, drop a file for the same datagrid domain into your
own translations/ directory, e.g. translations/datagrid.de.yaml:
grid.search: 'Suchen:' grid.zeroRecords: 'Keine passenden Einträge gefunden'
You only need to provide the keys you want to override – Symfony merges your file with the
bundle's for the rest. See translations/datagrid.en.yaml in this repository for the full list of
keys.