1. Go to this page and download the library: Download waldo/datatable-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
waldo / datatable-bundle example snippets
$bundles = array(
\\...
new Waldo\DatatableBundle\WaldoDatatableBundle(),
)
/**
* set datatable configs
* @return \Waldo\DatatableBundle\Util\Datatable
*/
private function datatable() {
return $this->get('datatable')
->setEntity("XXXMyBundle:Entity", "x") // replace "XXXMyBundle:Entity" by your entity
->setFields(
array(
"Name" => 'x.name', // Declaration for fields:
"Address" => 'x.address', // "label" => "alias.field_attribute_for_dql"
"Total" => 'COUNT(x.people) as total', // Use SQL commands, you must always define an alias
"Sub" => '(SELECT i FROM ... ) as sub', // you can set sub DQL request, you MUST ALWAYS define an alias
"_identifier_" => 'x.id') // you have to put the identifier field without label. Do not replace the "_identifier_"
)
->setWhere( // set your dql where statement
'x.address = :address',
array('address' => 'Paris')
)
->setOrder("x.created", "desc"); // it's also possible to set the default order
}
/**
* Grid action
* @Route("/", name="datatable")
* @return Response
*/
public function gridAction()
{
return $this->datatable()->execute(); // call the "execute" method in your grid action
}
/**
* Lists all entities.
* @Route("/list", name="datatable_list")
* @return Response
*/
public function indexAction()
{
$this->datatable(); // call the datatable config initializer
return $this->render('XXXMyBundle:Module:index.html.twig'); // replace "XXXMyBundle:Module:index.html.twig" by yours
}
/**
* set datatable configs
*
* @return \Waldo\DatatableBundle\Util\Datatable
*/
private function datatable()
{
return $this->get('datatable')
->setEntity("XXXMyBundle:Entity", "x") // replace "XXXMyBundle:Entity" by your entity
->setFields(
array(
"Name" => 'x.name', // Declaration for fields:
"Address" => 'x.address', // "label" => "alias.field_attribute_for_dql"
"Group" => 'g.name',
"Team" => 't.name',
"_identifier_" => 'x.id') // you have to put the identifier field without label. Do not replace the "_identifier_"
)
->addJoin('x.group', 'g', \Doctrine\ORM\Query\Expr\Join::INNER_JOIN)
->addJoin('x.team', 't', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('x.something', 's', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN, \Doctrine\ORM\Query\Expr\Join::WITH, 's.id = :someId')
->setWhere( // set your dql where statement
'x.address = :address',
array('address' => 'Paris')
)
->setOrder("x.created", "desc") // it's also possible to set the default order.
->setParameter('someId', 12)
;
}
private function datatable()
{
return $this->get('datatable')
//...
->setSearch(true); // for individual column search
// or
->setGlobalSearch(true);
}
/**
* set datatable configs
*
* @return \Waldo\DatatableBundle\Util\Datatable
*/
private function datatable()
{
$datatable = $this->get('datatable');
return $datatable->setEntity("XXXMyBundle:Entity", "x")
->setFields(
array(
"label of field 1" => 'x.field1', // column key 0
"label of field 2" => 'x.field2', // column key 1
"label of field 3" => 'x.field3', // column key 2
"_identifier_" => 'x.id') // column key 3
)
->setSearch(true)
->setSearchFields(array(0,2))
;
}