Installation
Add the bundle to your composer.json:
composer require bbit/data-grid-bundle
Enable the bundle in config/bundles.php:
return [
// ...
BranchBit\DataGridBundle\BranchBitDataGridBundle::class => ['all' => true],
];
Basic Configuration Define a grid in a controller or service:
use BranchBit\DataGridBundle\Grid\GridFactory;
class MyController extends AbstractController
{
public function index(GridFactory $gridFactory)
{
$grid = $gridFactory->create([
'data_source' => $this->getDoctrine()->getRepository(MyEntity::class)->findAll(),
'columns' => [
'id' => 'ID',
'name' => 'Name',
],
]);
return $this->render('my_template.html.twig', ['grid' => $grid]);
}
}
First Use Case Render a simple grid in Twig:
{{ grid.render() }}
This outputs a basic HTML table with pagination (if configured).
Dynamic Data Sources Use closures or services for dynamic data fetching:
$grid = $gridFactory->create([
'data_source' => function() {
return $this->getDoctrine()->getRepository(MyEntity::class)
->createQueryBuilder('e')
->where('e.active = :active')
->setParameter('active', true)
->getQuery()
->getResult();
},
'columns' => [...],
]);
Column Customization
Define column types (e.g., text, date, actions):
'columns' => [
'id' => ['type' => 'text', 'label' => 'ID'],
'created_at' => ['type' => 'date', 'label' => 'Created At'],
'actions' => [
'type' => 'actions',
'route' => 'app_edit',
'route_parameters' => ['id' => 'id'],
'buttons' => [
['label' => 'Edit', 'icon' => 'edit'],
['label' => 'Delete', 'icon' => 'delete'],
],
],
],
Pagination and Sorting Enable via configuration:
$grid = $gridFactory->create([
'data_source' => [...],
'columns' => [...],
'pagination' => ['page_size' => 20],
'sortable_columns' => ['name', 'created_at'],
]);
Integration with Forms
Use the form column type for inline editing:
'columns' => [
'name' => [
'type' => 'form',
'form_type' => 'text',
'options' => ['attr' => ['class' => 'form-control']],
],
],
Event Listeners Attach listeners for pre/post-grid rendering:
$grid->addPreRenderListener(function($grid) {
$grid->addColumn('custom', 'Custom Column');
});
Outdated Codebase
Limited Documentation
GridFactory, Grid, and Column classes).DataGridBundle (this package is a fork; compare behaviors).Doctrine Dependency
Repository interface or adapt the data source.Twig Integration
templates/BranchBitDataGrid/ or extend the base template.Pagination Quirks
pagination_template or override the Pagination class.Enable Debug Mode
Add this to config/packages/branch_bit_data_grid.yaml (if supported):
debug: true
Check logs for grid events or data source issues.
Inspect the Grid Object Dump the grid object to verify configuration:
dump($grid->getDataSource(), $grid->getColumns());
Override Templates
Copy vendor/branchbit/data-grid-bundle/Resources/views/ to templates/BranchBitDataGrid/ to customize rendering without modifying the bundle.
Custom Column Types
Extend BranchBit\DataGridBundle\Grid\Column\ColumnInterface to add new column types (e.g., select, checkbox).
Data Source Adapters
Implement BranchBit\DataGridBundle\Grid\DataSourceInterface for non-Doctrine data sources (e.g., API clients, CSV files).
Event System
Use GridEvents (e.g., PRE_RENDER, POST_RENDER) to modify grid behavior dynamically:
$grid->addPostRenderListener(function($grid, $response) {
$response->setContent($response->getContent() . '<div>Custom footer</div>');
});
Configuration Overrides
Override default settings in config/packages/branch_bit_data_grid.yaml:
branch_bit_data_grid:
default_page_size: 15
default_sort_column: created_at
default_sort_direction: desc
GridFactory manually in a service provider:
$this->app->bind('branchbit.data_grid.grid_factory', function($app) {
return new \BranchBit\DataGridBundle\Grid\GridFactory();
});
actions column type to generate Laravel URLs:
'actions' => [
'type' => 'actions',
'buttons' => [
['label' => 'View', 'route' => route('posts.show', ['post' => 'id'])],
],
],
How can I help you explore Laravel packages today?