Installation
composer require coryjb/datagrid-bundle
Add to config/bundles.php:
return [
// ...
CoryJB\DatagridBundle\CoryJBDatagridBundle::class => ['all' => true],
];
Basic Usage Create a controller to render a grid:
use CoryJB\DatagridBundle\Datagrid\Datagrid;
use CoryJB\DatagridBundle\Datagrid\Source\DoctrineSource;
class ProductController extends AbstractController
{
public function listAction()
{
$grid = new Datagrid();
$grid->setSource(new DoctrineSource(Product::class));
$grid->addColumn('id', 'ID', 'text');
$grid->addColumn('name', 'Name', 'text');
return $this->render('@CoryJBDatagrid/Grid.html.twig', [
'grid' => $grid,
]);
}
}
First Use Case Display a paginated list of entities with sorting/filtering:
{{ render(controller('App\Controller\ProductController::listAction')) }}
Dynamic Column Configuration
Use addColumn() with custom callbacks for computed fields:
$grid->addColumn('price', 'Price', 'text', function ($entity) {
return '$' . number_format($entity->getPrice(), 2);
});
Doctrine Integration
Leverage DoctrineSource for automatic DQL generation:
$source = new DoctrineSource(Product::class, $entityManager);
$source->addFilter('active', true); // WHERE active = 1
$grid->setSource($source);
Twig Integration
Extend templates via Grid.html.twig overrides:
{% extends '@CoryJBDatagrid/Grid.html.twig' %}
{% block grid_row %}
<tr>
<td>{{ entity.id }}</td>
<td><a href="{{ path('product_edit', {'id': entity.id}) }}">{{ entity.name }}</a></td>
</tr>
{% endblock %}
API Responses Return JSON for SPAs:
$grid->setResponseFormat('json');
return $this->json($grid->getData());
datagrid.build events for pre-processing.SourceInterface for non-Doctrine data (e.g., CSV, API).Doctrine DQL Quirks
DoctrineSource.IN clauses with large datasets (use FIND_IN_SET or native queries).Pagination Edge Cases
setItemsPerPage() is called before getData().Twig Template Overrides
grid_header, grid_row) must match exactly.php bin/console cache:clear).# config/packages/dev/doctrine.yaml
doctrine:
dbal:
logging: true
$grid->on('build', function ($event) {
\CoryJB\DatagridBundle\Event\BuildEvent $event;
dump($event->getGrid()->getSource()->getQuery());
});
Custom Column Types
Extend Column\AbstractColumn for new renderers (e.g., DateColumn):
class DateColumn extends AbstractColumn
{
public function render($entity)
{
return $entity->getCreatedAt()->format('Y-m-d');
}
}
Filter Extensions
Add custom filters via addFilter() with FilterInterface:
$grid->addFilter('search', new SearchFilter('name', 'LIKE', '%{value}%'));
Action Buttons
Use addAction() for row-level actions:
$grid->addAction('edit', 'Edit', function ($entity) {
return $this->generateUrl('product_edit', ['id' => $entity->getId()]);
});
How can I help you explore Laravel packages today?