## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require apy/datagrid-bundle
Add the bundle to config/bundles.php:
return [
// ...
APY\DataGridBundle\APYDataGridBundle::class => ['all' => true],
];
First Grid (ORM Example): Create a controller method to initialize a grid:
use APY\DataGridBundle\Grid\Source\Entity;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ProductController extends AbstractController
{
public function listAction()
{
$source = new Entity('App\Entity\Product');
$grid = $this->get('grid');
$grid->setSource($source);
return $this->render('product/list.html.twig', [
'grid' => $grid->getGridResponse('product/_grid.html.twig')
]);
}
}
Basic Twig Template:
Create templates/product/_grid.html.twig:
{{ grid(grid) }}
Clear Cache:
php bin/console cache:clear
use APY\DataGridBundle\Grid\Mapping as GRID;
/**
* @GRID\Source(columns="id, name, price")
*/
class Product {}
$grid->setSource(new Entity('App\Entity\Product'));
$grid->setTitle('Products');
return $this->render('product/list.html.twig', ['grid' => $grid]);
Use PHP or YAML to define columns dynamically:
# config/packages/apy_datagrid.yaml
services:
App\Grid\ProductGrid:
tags: [apy.datagrid]
arguments:
$columns:
- { name: 'name', label: 'Product Name' }
- { name: 'price', type: 'currency', label: 'Price' }
Add filters to columns in the grid:
$grid->add('name', 'text', 'Name', [
'filter' => [
'operator' => 'contains',
'input' => 'text'
]
]);
Enable exports in the grid:
$grid->setExport([
'csv' => true,
'excel' => true,
'pdf' => true
]);
Add bulk actions:
$grid->addMassAction('delete', 'Delete', 'fa-trash', [
'route' => 'product_delete_mass',
'routeParams' => ['_token' => '{{ csrf_token }}']
]);
Configure for infinite scroll or lazy loading:
$grid->setAjax(true);
$grid->setAjaxUrl($this->generateUrl('product_ajax'));
Override Twig templates:
{# templates/APYDataGrid/grid.html.twig #}
{% extends 'APYDataGridBundle::grid.html.twig' %}
{% block grid_row %}{{ parent() }}<custom-content>{{ row.id }}</custom-content>{% endblock %}
Restrict grid access:
$grid->setAccessControl('ROLE_ADMIN');
$grid->setColumnAccessControl([
'price' => 'ROLE_SUPER_ADMIN'
]);
Entity or Document sources for database-backed grids.$source->setDql('SELECT p FROM App\Entity\Product p WHERE p.active = :active');
$source->setDqlParams(['active' => true]);
$form = $this->createFormBuilder()
->add('category', EntityType::class, ['class' => 'App\Entity\Category'])
->getForm();
$grid->setFilterForm($form);
$source->setPagerfanta(true);
$source->setItemsPerPage(20);
# config/packages/apy_datagrid.yaml
apy_datagrid:
locale: '%locale%'
use APY\DataGridBundle\Grid\Column\ColumnInterface;
class CustomColumn implements ColumnInterface
{
public function renderCell($value, $row)
{
return '<span class="custom-badge">' . $value . '</span>';
}
}
Column Configuration:
$source->setColumns(['id', 'name', 'price']);
Ajax Filtering:
useGridPrefix is set in config/packages/apy_datagrid.yaml:
apy_datagrid:
use_grid_prefix: true
Doctrine Query Issues:
$source->setDql('SELECT p.id AS id, p.name AS name FROM App\Entity\Product p');
Twig Template Overrides:
templates/APYDataGrid/ and clear cache.Security Roles:
security.yaml and use setAccessControl:
$grid->setAccessControl(['ROLE_ADMIN', 'ROLE_EDITOR']);
Locale Conflicts:
$grid->setLocale('fr_FR');
Mass Action Tokens:
'routeParams' => ['_token' => '{{ csrf_token("product_delete_mass") }}']
Enable Debug Mode:
# config/packages/dev/apy_datagrid.yaml
apy_datagrid:
debug: true
Log DQL Queries:
config/packages/dev/doctrine.yaml:
doctrine:
dbal:
logging: true
profiling: true
Check Grid Events:
$grid->on('pre_build', function($event) {
dump($event->getGrid()->getSource()->getDql());
});
Validate YAML Configuration:
php bin/console debug:config apy_datagrid
Custom Sources:
APY\DataGridBundle\Grid\Source\SourceInterface for non-DB data:
class ApiSource implements SourceInterface
{
public function load($offset, $length, array $orderBy, array $filter)
{
$data = $this->fetchFromApi($filter);
return new ArrayCollection($data);
}
}
Column Types:
APY\DataGridBundle\Grid\Column\ColumnInterface.Filters:
APY\DataGridBundle\Grid\Filter\FilterInterface for custom operators.Templates:
templates/APYDataGrid/.Events:
$grid->on('post_build', function($event) {
$grid = $event->getGrid();
// Custom logic here
});
Security Voters:
APY\DataGridBundle\Grid\Security\VoterInterface for custom access control.useGridPrefix:
true, grid parameters are prefixed (e.g., `grid[filter][nameHow can I help you explore Laravel packages today?