Installation:
composer require customscripts/datagrid-bundle:dev-master
Register the bundle in app/AppKernel.php:
new CS\DataGridBundle\CSDataGridBundle(),
First Use Case: Define a basic grid configuration in a controller or service:
use CS\DataGridBundle\Grid\Grid;
$grid = new Grid();
$grid->setDataSource('doctrine', ['entity' => 'AppBundle:User']);
$grid->setColumns(['id', 'username', 'email']);
$grid->setPageSize(10);
Render the Grid: Pass the grid to a Twig template:
{{ render(controller('CSDataGridBundle:Grid:render', {'grid': grid})) }}
src/CS/DataGridBundle/ directory for core classes (Grid, Column, DataSource).Resources/views/ in the bundle (if any) or test cases for usage patterns.Resources/config/services.yml for bundle services and dependencies.Grid Definition:
Grid class to define structure:
$grid = new Grid();
$grid->setDataSource('doctrine', ['entity' => 'AppBundle:User']);
$grid->setColumns([
'id' => ['label' => 'ID', 'type' => 'number'],
'username' => ['label' => 'Username', 'type' => 'text'],
]);
Data Sources:
$grid->setDataSource('doctrine', [
'entity' => 'AppBundle:User',
'query' => function($qb) {
$qb->where('u.active = 1');
}
]);
$grid->setDataSource('array', ['data' => $arrayData]);
Pagination:
setPageSize() and handle pagination in the template:
{% for row in grid.rows %}
{{ row.id }} - {{ row.username }}
{% endfor %}
{{ grid.pagination }}
Filters and Search:
$grid->addFilter('username', 'text', 'Contains');
$grid->setSearchField('username');
Actions:
$grid->addAction('edit', ['route' => 'user_edit', 'params' => ['id' => 'id']]);
Twig Integration:
Use the render Twig function or pass the grid object to a custom template:
{% for column in grid.columns %}
<th>{{ column.label }}</th>
{% endfor %}
Event Listeners: Extend functionality via events (if supported):
$grid->addListener('preRender', function($event) {
$event->getGrid()->addColumn('custom', ['label' => 'Custom Field']);
});
Dependency Injection:
Register grid services in services.yml for reuse:
services:
app.user_grid:
class: CS\DataGridBundle\Grid\Grid
calls:
- [setDataSource, ['doctrine', { entity: 'AppBundle:User' }]]
Symfony 2.1 Dependency:
Limited Documentation:
README is incomplete (e.g., "Creating your first Grid" is marked "Coming Soon").src/ directory for undocumented features or check the Grid class for available methods.Data Source Quirks:
QueryBuilder setup. Test with simple queries first.Pagination Conflicts:
page, pageSize) align with your template.Action Routing:
edit) assume standard Symfony routing. Custom route names may require manual parameter passing:
$grid->addAction('edit', ['route' => 'app.user_edit', 'params' => ['id' => 'id']]);
Check Grid Configuration:
dump($grid->getColumns(), $grid->getDataSource());
Enable Debug Mode:
Log Data Sources:
$grid->setDataSource('doctrine', [
'entity' => 'AppBundle:User',
'query' => function($qb) {
$qb->where('u.active = 1');
return $qb->getQuery()->getSQL(); // Log this for debugging
}
]);
Custom Columns:
Column class to add custom types (e.g., date, boolean):
class CustomColumn extends \CS\DataGridBundle\Grid\Column {
public function render($value) {
return $value ? 'Yes' : 'No';
}
}
Data Source Plugins:
AbstractDataSource:
class ApiDataSource extends AbstractDataSource {
public function fetch() {
return $this->httpClient->request('GET', '/api/users')->toArray();
}
}
Twig Extensions:
$twig->addFunction(new \Twig_SimpleFunction('custom_grid', function($grid) {
// Preprocess grid data
return $grid->render();
}));
Event-Driven Extensions:
preRender, postRender), hook into them for dynamic modifications:
$grid->addListener('preRender', function($event) {
$event->getGrid()->addColumn('status', ['label' => 'Status']);
});
Bundle Prefix:
The bundle uses CSDataGridBundle as the namespace prefix. Ensure your templates and services reflect this (e.g., CS\DataGridBundle\Grid\Grid).
Template Overrides:
Override default templates by copying them from vendor/customscripts/datagrid-bundle/Resources/views/ to app/Resources/CSDataGridBundle/views/.
Caching: If performance is critical, cache grid configurations or data sources manually, as the bundle may not include built-in caching.
How can I help you explore Laravel packages today?