dev-master
9999999-dev https://github.com/dutchbridge/DatatableBundleSymfony2 Datatable For Doctrine Entities
MIT
The Requires
- php >=5.3.2
- symfony/framework-bundle ~3.0
by dutchbridge
symfony2 table grid datatable
Symfony2 Datatable For Doctrine Entities
Symfony2 Datatable For Doctrine Entities., (*1)
Status: not yet ready, hard-development, (*2)
Before installing DatatableBundle you need to have a working installation of Symfony2., (*3)
Download DatatableBundle using composer, (*4)
Add DatatableBundle in your composer.json:, (*5)
{ "require": { "dutchbridge/datatablebundle": "dev-master" } }
Tell composer to download the bundle by running the command:, (*6)
bash
$ php composer.phar update
, (*7)
Enable the bundle, (*8)
Enable the bundle in the kernel:, (*9)
``` php <?php // app/AppKernel.php, (*10)
public function registerBundles() { $bundles = array( // ... new DutchBridge\DatatableBundle\DutchBridgeDatatableBundle() ); } ```, (*11)
Bootstrap 2.3 installation, (*12)
The DatatableBundle no contains the assets files from Twitter Bootstrap. You can e.g. download a ZIP archive with the files from the Bootstrap repository on Github., (*13)
Create your layout.html.twig, (*14)
``` html {% extends '::base.html.twig' %}, (*15)
{% block title %}UserBundle{% endblock %}, (*16)
{% block stylesheets %}, (*17)
<link href="{{ asset('bundles/dutchbridgedatatable/css/bootstrap.min.css') }}" rel="stylesheet" type="text/css" /> <link href="{{ asset('bundles/dutchbridgedatatable/css/dataTables_bootstrap.css') }}" rel="stylesheet" type="text/css" />
{% endblock %}, (*18)
{% block body%}, (*19)
{% block scripts %} <script src="{{ asset('bundles/dutchbridgedatatable/js/jquery-2.0.2.min.js') }}" type="text/javascript"></script> <script src="{{ asset('bundles/dutchbridgedatatable/js/bootstrap.min.js') }}" type="text/javascript"></script> <script src="{{ asset('bundles/dutchbridgedatatable/js/jquery.dataTables.min.js') }}" type="text/javascript"></script> <script src="{{ asset('bundles/dutchbridgedatatable/js/dataTables_bootstrap.js') }}" type="text/javascript"></script> {% endblock %} <div class="container"> {% block content %} {% endblock %} </div>
{% endblock %} ```, (*20)
Create your Datatable class, (*21)
``` php <?php, (*22)
namespace DutchBridge\UserBundle\Datatable;, (*23)
use DutchBridge\DatatableBundle\Datatable\AbstractDatatableView; use DutchBridge\DatatableBundle\Datatable\Field; use DutchBridge\DatatableBundle\Datatable\Action;, (*24)
/**, (*25)
@package DutchBridge\UserBundle\Datatable */ class UserDatatable extends AbstractDatatableView { /**, (*26)
{@inheritdoc} */ public function build() { $this->setTableId('user_datatable'); $this->setSAjaxSource('user_results');, (*27)
$this->setTableHeaders(array( 'Username', 'Email', 'Enabled', '' ));, (*28)
$nameField = new Field('username');, (*29)
$emailField = new Field('email');, (*30)
$enabledField = new Field('enabled'); $enabledField->setBSearchable('false'); $enabledField->setSWidth('90'); $enabledField->setMRender("render_boolean_icons(data, type, full)");, (*31)
$idField = new Field('id'); $idField->setBSearchable('false'); $idField->setBSortable('false'); $idField->setMRender("render_action_icons(data, type, full)"); $idField->setSWidth('92');, (*32)
$this->addField($nameField); $this->addField($emailField); $this->addField($enabledField); $this->addField($idField);, (*33)
$editAction = new Action(); $editAction->setType('route'); $editAction->setClass('btn btn-sm btn-primary'); $editAction->setRoute('user_edit'); $editAction->setIcon('icon-edit'); $editAction->addRouteParameter('userId', array('field' => 'id'));, (*34)
$removeAction = new Action(); $removeAction->setType('route'); $removeAction->setClass('btn btn-sm btn-danger'); $removeAction->setRoute('user_remove'); $removeAction->setIcon('icon-remove'); $removeAction->addRouteParameter('userId', array('field' => 'id')); $removeAction->addRouteParameter('lorem', array('value' => 'ipsum'));, (*35)
$this->addAction($editAction); $this->addAction($removeAction); } } ```, (*36)
Create your index.html.twig, (*37)
``` html {% extends 'SgUserBundle::layout.html.twig' %}, (*38)
{% block title %}{{ title }}{% endblock %}, (*39)
{% block content %}, (*40)
<h2>{{ title }}</h2> {{ datatable_render(datatable) }}
{% endblock %} ```, (*41)
Add controller actions, (*42)
``` php /**, (*43)
@return array */ public function indexAction() { /**, (*44)
/**, (*45)
return array( 'title' => 'Enabled Users', 'datatable' => $datatableView, ); }, (*46)
/**, (*47)
@return \Symfony\Component\HttpFoundation\Response */ public function indexResultsAction() { /**, (*48)
/**, (*49)
@var \Doctrine\ORM\QueryBuilder $qb */ $callbackFunction =, (*50)
function($qb) { $andExpr = $qb->expr()->andX(); $andExpr->add($qb->expr()->eq('fos_user.enabled', '1')); $qb->andWhere($andExpr); };, (*51)
$datatable->addWhereBuilderCallback($callbackFunction);, (*52)
return $datatable->getSearchResults(); } ```, (*53)
Working with Associations, (*54)
We extend the user entity to multiple OneToMany associations (Post and Comment)., (*55)
``` php <?php, (*56)
namespace DutchBridge\UserBundle\Entity;, (*57)
use Doctrine\Common\Collections\ArrayCollection; use FOS\UserBundle\Model\User as BaseUser; use Doctrine\ORM\Mapping as ORM; use DutchBridge\AppBundle\Entity\Comment; use DutchBridge\AppBundle\Entity\Post;, (*58)
/**, (*59)
@package DutchBridge\UserBundle\Entity */ class User extends BaseUser { /**, (*60)
/**, (*61)
/**, (*62)
/**, (*63)
Ctor. */ public function __construct() { parent::__construct();, (*64)
// your own logic, (*65)
$this->posts = new ArrayCollection(); $this->comments = new ArrayCollection(); }, (*66)
// ..., (*67)
}, (*68)
For a comma-separated view of all blog titles we add the following to the UserDatatable class. ``` php $this->setTableHeaders(array( 'Username', 'Email', 'Enabled', 'Posts', '' )); $postsField = new Field('posts'); $postsField->setRenderArray(true); $postsField->setRenderArrayFieldName('title'); $postsField->setSName('posts.title'); $this->addField($nameField); $this->addField($emailField); $this->addField($enabledField); $this->addField($postsField); $this->addField($idField);
The result should looks like that:, (*69)
Symfony2 Datatable For Doctrine Entities
MIT
symfony2 table grid datatable