Installation Add the bundle to your project via Composer:
composer require cwd/bootgrid-bundle
Enable it in config/bundles.php:
return [
// ...
CWD\BootgridBundle\CWDBootgridBundle::class => ['all' => true],
];
Basic Configuration Define a route and template for your first grid:
# config/packages/cwd_bootgrid.yaml
cwd_bootgrid:
templates:
default: 'BootgridBundle::default.html.twig'
First Use Case Create a controller to render a grid:
// src/Controller/ExampleController.php
use CWD\BootgridBundle\Grid\Grid;
use CWD\BootgridBundle\Grid\Source\ArraySource;
class ExampleController extends AbstractController
{
public function gridAction()
{
$grid = new Grid();
$grid->setSource(new ArraySource([
['id' => 1, 'name' => 'Item 1'],
['id' => 2, 'name' => 'Item 2'],
]));
$grid->setColumns([
'id', 'name'
]);
return $this->render('example/grid.html.twig', [
'grid' => $grid,
]);
}
}
Template Integration Use the provided Twig template:
{% extends 'BootgridBundle::default.html.twig' %}
{% block grid %}
{{ grid|bootgrid }}
{% endblock %}
doctrine/orm):
$grid->setSource(new DoctrineSource(
$entityManager->getRepository(Entity::class)->findAll()
));
CWD\BootgridBundle\Grid\Source\SourceInterface.Define columns with options:
$grid->setColumns([
'id' => [
'title' => 'ID',
'sortable' => true,
'formatter' => 'html',
],
'name' => [
'title' => 'Name',
'width' => '200px',
],
]);
Enable via configuration:
$grid->setDefaultSort('id');
$grid->setPaging(true);
$grid->setPagingOptions(['pageSize' => 10, 'pageSizes' => [5, 10, 20]]);
Leverage Bootgrid’s JS features (e.g., filtering, row selection) by extending the template:
{% block grid_options %}
{{ parent() }}
{{
grid|bootgrid_options({
'selection': true,
'multiSelect': true,
'rowSelect': true
})
}}
{% endblock %}
Bind grid selections to Symfony forms:
$form = $this->createFormBuilder()
->add('selectedItems', HiddenType::class)
->getForm();
// In Twig:
{{ form_widget(form) }}
<script>
$('#grid').on('loaded.rs.jquery.bootgrid', function() {
$('#grid').bootgrid('getSelectedRows').then(function(rows) {
$('#example_controller_selectedItems').val(JSON.stringify(rows));
});
});
</script>
Twig Template Overrides
{% block grid %} and {% block grid_options %} are preserved.php bin/console cache:clear
DoctrineSource Caching
DoctrineSource queries if data changes frequently. Use setCacheLifetime(0) if needed.JavaScript Conflicts
{{ parent() }} {# Loads jQuery if missing #}
{{ cwd_bootgrid_js() }}
CSRF in AJAX Requests
{% block grid_toolbar %}
{{ form_start(form, {'attr': {'id': 'delete-form'}}) }}
{{ form_widget(form.delete) }}
{{ form_end(form) }}
{% endblock %}
Uncaught TypeError or 404 requests.{
"rows": [...],
"total": 100
}
?nocache=1 to URLs during development.Custom Formatters
Add a Twig formatter (e.g., src/Twig/Extension/BootgridExtension.php):
public function getFormatter($value, $column)
{
if ($column['formatter'] === 'custom') {
return '<span class="badge">'.$value.'</span>';
}
return $value;
}
Event Listeners Subscribe to grid events (e.g., row selection):
$grid->on('rowSelected', function($event) {
$row = $event->getRow();
// Handle selection
});
Override Default Templates
Copy vendor/cwd/bootgrid-bundle/Resources/views/ to templates/cwd_bootgrid/ and customize.
setLazyLoading(true) for large datasets.$grid->setSource(new DoctrineSource(
$entityManager->getRepository(Entity::class)->createQueryBuilder('e')
->getQuery()
));
How can I help you explore Laravel packages today?