Installation:
composer require arne-groskurth/symgrid
Register the bundle in AppKernel.php:
new ArneGroskurth\Symgrid\ArneGroskurthSymgridBundle(),
new Bmatzner\FontAwesomeBundle\BmatznerFontAwesomeBundle()
Run assets install:
php app/console assets:install --symlink
Basic Usage in Controller:
$grid = $this->get('arnegroskurth_symgrid.grid')
->from(User::class) // Entity data source
->useDefaultStyle();
return $grid->getResponse('template.html.twig', ['grid' => $grid]);
Render in Twig:
{{ symgrid(grid) }}
For a fast-start grid with Doctrine entities:
$grid = $this->get('arnegroskurth_symgrid.grid')
->from(User::class)
->useDefaultStyle()
->addColumn('id', 'ID', 'Numeric')
->addColumn('username', 'Username', 'String');
$grid->from(User::class);
$qb = $this->getDoctrine()->getRepository(User::class)->createQueryBuilder('u');
$grid->from($qb);
$grid->from([['id' => 1, 'name' => 'John'], ['id' => 2, 'name' => 'Jane']]);
$grid->addColumn('id', 'ID', 'Numeric');
$grid->addColumn('createdAt', 'Created At', 'DateTime');
$grid->addColumn('fullName', 'Full Name', 'String', [
'callback' => function ($user) {
return $user->getFirstName() . ' ' . $user->getLastName();
}
]);
$grid->enableAutoFilters();
$grid->addAggregation('total', 'Total Users', 'Count');
$grid->addRowAction('edit', 'Edit', 'fa-pencil', '/user/edit/{id}');
$grid->addMassAction('delete', 'Delete Selected', 'fa-trash', '/user/delete');
$grid->enableExport(['csv', 'excel', 'pdf']);
$grid->useDefaultStyle();
<link rel="stylesheet" href="{{ asset('bundles/arnegroskurthsymgrid/layout.css') }}" />
<link rel="stylesheet" href="{{ asset('bundles/arnegroskurthsymgrid/style.css') }}" />
Missing FontAwesomeBundle:
Ensure Bmatzner\FontAwesomeBundle is registered; otherwise, icons (e.g., for actions) will break.
new Bmatzner\FontAwesomeBundle\BmatznerFontAwesomeBundle()
Asset Installation:
Forgetting assets:install will result in broken CSS/JS. Run:
php app/console assets:install --symlink
Column Type Mismatches:
Symgrid auto-detects column types, but explicit types (e.g., DateTime vs. Date) prevent rendering issues.
$grid->addColumn('birthday', 'Birthday', 'Date'); // Explicit type
Pagination Conflicts:
If using custom pagination, ensure the DataSource is configured to respect it:
$grid->setPagination(10); // Default per-page items
Callback Performance: Avoid heavy logic in column callbacks; lazy-load data or use DQL expressions instead.
Check Grid Configuration:
Use dump($grid->getConfiguration()) to verify settings before rendering.
Enable Debug Mode:
Symgrid logs errors to Symfony’s logger. Check var/log/dev.log for issues.
JavaScript Errors:
Clear cache (cache:clear) if JS events (e.g., filters) fail silently.
Custom Column Types:
Extend ArneGroskurth\Symgrid\Grid\Column\Type\AbstractType to add new types (e.g., Email).
Override Templates:
Copy Resources/views/ from the bundle to your project and modify (e.g., grid.html.twig).
Event Listeners: Register JS listeners for dynamic updates:
$(document).on('symgrid:filter:change', function(e, grid, filters) {
console.log('Filters changed:', filters);
});
Translation Overrides:
Extend translations in Resources/translations/ to customize labels/actions.
Lazy-Load Data:
Use QueryBuilder with pagination/fetch joins to avoid N+1 queries.
Disable Auto-Filters: If filters are rarely used:
$grid->disableAutoFilters();
Cache Grid Config: For static grids, cache the configuration object to avoid re-parsing.
How can I help you explore Laravel packages today?