This bundle helps you to create and manage simple to complex datagrids quickly and easily.
Unlike other similar bundle already available on github and/or packagist, there is no magic method that will render the datagrid in you view. This technical choice allow you to completely customize your datagrid aspect and render (filter fields, buttons, columns, data displayed in each column, pagination links and informations, etc.)
This make it easy to implement and use in both back-end and front-end applications.
Still skeptical ? Let's see how it works !
Since composer is the simplest and fastest way to install dependencies, the only way to install this bundle automatically is to add the following line to your dependencies
composer require spyrit/propel-datagrid-bundle
csanquer/colibri-csv 1.2
csanquer/colibri-csv
1.2spyrit/colibri-csv
1.3Symfony\Component\DependencyInjection\ContainerInterface
.Symfony\Component\HttpFoundation\RequestStack
Symfony\Component\Form\FormFactoryInterface
Symfony\Component\Routing\RouterInterface
Enable the bundle in your application
// config/bundles.php
<?php
return [
...
Spyrit\PropelDatagridBundle\SpyritPropelDatagridBundle::class => ['all' => true],
May be the most interesting part of this documentation which quickly describe how to create and use your first Datagrid.
To create a datagrid you have to create a single class that inherit from the PropelDatagrid object and implement all methods from the PropelDatagridInterface :
<?php
namespace Spyrit\PropelDatagridBundle\Datagrid\Demo;
use Spyrit\PropelDatagridBundle\Datagrid\PropelDatagrid;
class BookDatagrid extends PropelDatagrid
{
public function configureQuery()
{
return BookQuery::create();
}
public function getDefaultSortColumn()
{
return 'id';
}
public function getName()
{
return 'book';
}
public function configureFilter()
{
return [
'id' => [
'type' => IntegerType::class,
'options' => [
'label' => '#',
'required' => false,
],
],
'title' => [
'type' => TextType::class,
'options' => [
'label' => 'Title',
],
]
];
}
}
The configureQuery method must return a predefined PropelQuery object (example: BookQuery object) as shown here :
<?php
//...
public function configureQuery()
{
return BookQuery::create()
->joinWith('Author', \Criteria::LEFT_JOIN)
->joinWith('Publisher', \Criteria::LEFT_JOIN)
;
}
<?php
namespace App\Controller;
use App\Propel\Annonce;
use App\Propel\AnnonceQuery;
use App\Datagrid\BookDatagrid;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/books')]
class BookController extends AbstractController
{
#[Route('/')]
public function list(BookDatagrid $annonceDatagrid): Response
{
$datagrid = $annonceDatagrid->execute();
return $this->render('book/list.html.twig', [
'datagrid' => $datagrid,
]);
}
}
{% extends "base.html.twig" %}
{% import "@SpyritPropelDatagrid/Datagrid/macros.html.twig" as macros %}
{% block body %}
{% set form = datagrid.filterFormView %}
<form method="post">
{{ form_widget(form.id) }}
{{ form_widget(form.title) }}
{{ form_errors(form) }}
{{ form_rest(form) }}
</form>
<table>
<thead>
<tr>
<th>{{ macros.sort('id', "#", route, datagrid) }}</th>
<th>{{ macros.sort('title', "Title", route, datagrid) }}</th>
</tr>
</thead>
<tbody>
{% for book in datagrid.results %}
<tr>
<td>{{ book.id }}</td>
<td>{{ book.title }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
Todo
Our special thanks go to ...