Installation:
composer require apymakoso/datagrid-bundle
Enable the bundle in config/bundles.php:
return [
// ...
APY\DataGridBundle\APYDataGridBundle::class => ['all' => true],
];
First Use Case: Create a basic grid for an entity in a controller:
use APY\DataGridBundle\Grid\Source\Entity;
public function indexAction()
{
$source = new Entity('App\Entity\Product');
$grid = $this->get('grid');
$grid->setSource($source);
return $grid->getGridResponse('App:product_grid.html.twig');
}
Template:
{{ grid(grid) }}
Clear Cache:
php bin/console cache:clear
Resources/doc/summary.md (Core documentation)Resources/doc/grid_configuration/ (Examples and templates)Resources/views/ (Default Twig templates)// Controller
$source = new Entity('App\Entity\User');
$source->setUseRepositoryMethod(true); // Use custom repository method
$source->setRepositoryMethod('findAllActive'); // e.g., UserRepository::findAllActive()
$grid = $this->get('grid');
$grid->setSource($source);
$grid->setTitle('User Management');
return $grid->getGridResponse('App:user_grid.html.twig');
// In your entity or via YAML/XML
/**
* @GRID\Column(
* title="Full Name",
* type="text",
* width="200px",
* align="left",
* sortable=true,
* filterable=true,
* template="App:Grid/columns/name.html.twig"
* )
*/
protected $fullName;
// Controller (dynamic filters)
$grid->getSource()->addFilter('status', 'eq', 'active');
// Twig (external filter box)
{{ grid_filter_box(grid) }}
// Controller
$grid->addMassAction('delete', 'Delete', 'fa-trash', 'grid.delete', ['confirm' => true]);
$grid->addMassAction('export', 'Export to CSV', 'fa-file-excel-o', 'grid.export_csv');
// Twig
{{ grid_mass_actions(grid) }}
// Controller (auto-export)
$grid->setExport(true);
$grid->setExportFormats(['csv', 'xlsx']);
// Manual export
$export = $grid->getExport();
$export->setFormat('csv');
return $export->getResponse('user_export.csv');
// Controller
$grid->setAjax(true);
$grid->setAjaxUrl($this->generateUrl('app_user_grid_ajax'));
// Twig
{{ grid_ajax(grid) }}
// Controller
$grid->setAccessControl([
'column' => ['id' => 'ROLE_ADMIN'],
'action' => ['delete' => 'ROLE_SUPER_ADMIN'],
]);
Entity source for ORM entities.setRepositoryMethod() for custom queries.@GRID\Column or @GRID\Source annotations.Document source for MongoDB documents.$source = new Document('App\Document\Product');
$source->setGroups(['category' => ['name', 'description']]);
$source = new Vector($yourArrayData);
$grid->setSource($source);
templates/App/Grid/.columns/name.html.twig for custom column rendering.Knp\Component\Pagerfanta\Paginator.$grid->setItemsPerPage(20);
$grid->setMaxItemsPerPage(100);
{% trans_default_domain 'APYDataGridBundle' %}
{{ grid(grid) }}
config/packages/apy_datagrid.yaml:
apy_datagrid:
locale: 'fr_FR'
number_formats:
currency: '€ #,##0.00'
Cache Issues:
php bin/console cache:clear --env=prod in production.Annotation Parsing:
@GRID\Source.use APY\DataGridBundle\Grid\Mapping as GRID; in imports.Doctrine Proxy Classes:
Ajax Conflicts:
public function ajaxAction()
{
$grid = $this->get('grid');
return $grid->getAjaxResponse();
}
Export Quirks:
mikehaertl/php-excel and dompdf/dompdf.Security:
access_control for sensitive actions/columns.ROLE_USER and ROLE_ADMIN to avoid accidental exposure.Performance:
setDefaultColumns():
$grid->setDefaultColumns(['id', 'name', 'createdAt']);
setUseRepositoryMethod(true) with a paginated query.Locale Mismatches:
{{ dump(grid.getSource().getLocale()) }}.Enable Debug Mode:
debug: true in config/packages/apy_datagrid.yaml:
apy_datagrid:
debug: true
var/log/dev.log.Check Grid Events:
$grid->on('pre_build_query', function ($event) {
$query = $event->getQuery();
// Log or inspect $query
});
Inspect Source Data:
$data = $grid->getSource()->getData();
dump($data);
Validate Annotations:
php bin/console debug:container APY\DataGridBundle\Annotation\Driver to check if annotations are loaded.Twig Debugging:
{{ dump(grid) }} to inspect the grid object.Custom Sources:
APY\DataGridBundle\Grid\Source\AbstractSource for new data sources (e.g., API, Elasticsearch).Custom Columns:
APY\DataGridBundle\Grid\Column\AbstractColumn:
class CustomColumn extends AbstractColumn
{
public function getValue($item)
{
return $item->getCustomValue();
}
}
services.yaml:
services:
App\Grid\Column\CustomColumn:
tags:
- { name: apy_datagrid.column_type, type: custom }
Custom Filters:
APY\DataGridBundle\Grid\Filter\AbstractFilter:
class CustomFilter extends AbstractFilter
{
public function apply($queryBuilder, $alias, $direction)
{
// Custom logic
}
}
services.yaml:
tags:
- { name: apy_datagrid.filter_type, type: custom }
Event Subscribers:
$grid->on('pre_build_query', [$this, 'on
How can I help you explore Laravel packages today?