2017 © Pedro PelĂĄez
 

symfony-bundle datatable-bundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

image

waldo/datatable-bundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  • Monday, March 5, 2018
  • by waldo2188
  • Repository
  • 4 Watchers
  • 13 Stars
  • 5,364 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 12 Forks
  • 17 Open issues
  • 20 Versions
  • 13 % Grown

The README.md

DatatableBundle

Fork of AliHichem/DatatableBundle, this bundle will add some great features and evolve in a different way than it source., (*1)

Build Status SensioLabsInsight, (*2)

Warning: The jQuery Datatable plugin has evolved (version 1.10) with a all new API and option.
You MUST use the version 2 of DatatableBundle with the jQuery Datatable plugin version lower than 1.10.
You MUST use the version 3 of DatatableBundle with the jQuery Datatable plugin version equal or greater than 1.10., (*3)

The Datatable bundle for symfony2 allow for easily integration of the jQuery Datatable plugin with the doctrine2 entities. This bundle provides a way to make a projection of a doctrine2 entity to a powerful jquery datagrid. It mainly includes:, (*4)

  • datatable service container: to manage the datatable as a service.
  • twig extension: for view integration.
  • dynamic pager handler : no need to set your pager.
  • support doctrine2 association.
  • support of Doctrine Query Builder.
  • support of doctrine subquery.
  • support of column search.
  • support of custom twig/phpClosure renderers.
Screenshot

Summary


Installation

Installation is a quick (I promise!) 3 step process: 1. Download DatatableBundle using Composer 2. Enable the Bundle 3. Configure the Bundle, (*5)

Download DatatableBundle using Composer

Using Composer

Install with this command : composer require waldo/datatable-bundle, (*6)

Generate the assets symlinks :, (*7)

php app/console assets:install --symlink web

Enable the Bundle

Add the bundle to the AppKernel.php, (*8)

$bundles = array(
    \\...
    new Waldo\DatatableBundle\WaldoDatatableBundle(),
    )

Configure the Bundle

In this section you can put the global config that you want to set for all the instance of DataTable in your project., (*9)

To keep it to default

# app/config/config.yml
waldo_datatable:
    all:    ~
    js:     ~

The js config will be applied to DataTable exactly like you do with $().datatable({ your config }); in a javascript part., (*10)

Note: all your js config have to be string typed, make sure to use (") as delimiters., (*11)

Config sample

waldo_datatable:
    all:
        search:           false
    js:
        pageLength: "10"
        lengthMenu: [[5,10, 25, 50, -1], [5,10, 25, 50, 'All']]
        dom: '<"clearfix"lf>rtip'
        jQueryUI: "false"

How to use DatatableBundle ?

Assuming for example that you need a grid in your "index" action, create in your controller method as below :, (*12)

Warning alias as is case-sensitive, always write it in lower case, (*13)

/**
 * 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
}

Rendering inside Twig

You have the choice, you can render the HTML table part and Javascript part in just one time with the Twig function datatable, like below., (*14)




<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/r/dt/dt-1.10.9/datatables.min.css"/>



{{ datatable({
        'js' : {
            'ajax' : path('route_for_your_datatable_action')
        }
    })
}}

Or, render each part separatly., (*15)

datatable_html is the Twig function for the HTML part.
datatable_js is the Twig function for the Javascript part., (*16)

{% block body %}
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/r/dt/dt-1.10.9/datatables.min.css"/>
    {{ datatable_html({
            'id' : 'dta-offres'
        })
    }}

{% endblock %}

{% block javascripts %}


{{ datatable_js({
        'id' : 'dta-offres',
        'js' : {
            'dom': '<"clearfix"lf>rtip',
            'ajax': path('route_for_your_datatable_action'),
        }
    })
}}
{% endblock javascripts %}

Advanced Use of DatatableBundle

Advanced php config

Assuming the example above, you can add your joins and where statements., (*17)

/**
 * 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)
                ;
}

Use of search filter

Activate search globally

The searching functionality that is very useful for quickly search through the information from the database. This bundle provide two way of searching, who can be used together : global search and individual column search., (*18)

By default the filtering functionality is disabled, to get it working you just need to activate it from your configuration method like this :, (*19)

private function datatable()
{
    return $this->get('datatable')
                //...
                ->setSearch(true); // for individual column search
                // or
                ->setGlobalSearch(true);
}

Set search fields

You can set fields where you want to enable your search. Let say you want search to be active only for "field 1" and "field3 ", you just need to activate search for the approriate column key and your datatable config should be :, (*20)

/**
 * 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))
    ;
}

Multiple actions, how to had checkbox for each row ?

Sometimes, it's good to be able to do the same action on multiple records like deleting, activating, moving ... Well this is very easy to add to your datatable: all what you need is to declare your multiple action as follow., (*21)

/**
 * 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 field1" => 'x.field1',   // column key 0
                                "label of field2" => 'x.field2',   // column key 1
                                "_identifier_" => 'x.id')          // column key 2
                    )
                    ->setMultiple(
                                array(
                                    'delete' => array(
                                        'title' => 'Delete',
                                        'route' => 'multiple_delete_route' // path to multiple delete route action
                                    ),
                                    'move' => array(
                                        'title' => 'Move',
                                        'route' => 'multiple_move_route' // path to multiple move route action
                                    ),
                                )
                        )
    ;
}

Then all what you have to do is to add the necessary logic in your "multiple_delete_route" (or whatever your route is for). In that action, you can get the selected ids by :, (*22)

$data = $this->getRequest()->get('dataTables');
$ids  = $data['actions'];

Custom renderer for cells

Twig renderers

To set your own column structure, you can use a custom twig renderer as below : In this example you can find how to set the use of the default twig renderer for action fields which you can override as your own needs., (*23)

/**
 * 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 field1" => 'x.field1',
                                "label of field2" => 'x.field2',
                                "_identifier_" => 'x.id')
                    )
                    ->setRenderers(
                            array(
                                2 => array(
                                    'view' => 'XXXMyBundle:Renderers:_actions.html.twig', // Path to the template
                                    'params' => array( // All the parameters you need (same as a twig template)
                                            'edit_route'    => 'route_edit',
                                            'delete_route'  => 'route_delete'
                                        ),
                                ),
                            )
                    );
}

In a twig renderer you can have access the the field value using dt_item variable,, (*24)

// XXXMyBundle:Renderers:_actions.html.twig
{{ dt_item }}

or access the entire entity object using dt_obj variable., (*25)

// XXXMyBundle:Renderers:_actions.html.twig
<a href="{{ path('route_to_user_edit',{ 'user_id' : dt_obj.id }) }}">{{ dt_obj.username }}</a>

NOTE: be careful of Doctrine's LAZY LOADING when using dt_obj !, (*26)

PHP Closures

Assuming the example above, you can set your custom fields renderer using PHP Closures., (*27)

/**
 * set datatable configs
 * @return \Waldo\DatatableBundle\Util\Datatable
 */
private function datatable() {

    $controller_instance = $this;
    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"
                            "_identifier_"  => 'x.id')          // you have to put the identifier field without label. Do not replace the "_identifier_"
                        )
                ->setRenderer(
                    function(&$data) use ($controller_instance) {
                        foreach ($data as $key => $value) {
                            if ($key == 1) {            // 1 => address field
                                $data[$key] = $controller_instance
                                        ->get('templating')
                                        ->render(
                                               'XXXMyBundle:Module:_grid_entity.html.twig',
                                               array('data' => $value)
                                        );
                            }
                        }
                    }
                );
}

DataTable Callbacks options

If you need to put some Javascript Callbacks like drawCallback, you can do it localy with the Datatable js option. See the two examples below:, (*28)

// XXXMyBundle:Welcome:list.html.twig
{{ datatable_js({
        'id' : 'datable-id',
        'js' : {
            'ajax': "/some/path",
            'createdRow': 'function(){console.log("Do something useful here");}'
        }
    })
}}

{# or #}

{{ datatable_js({
        'id' : 'datable-id',
        'js' : {
            'ajax': "/some/path",
            'createdRow': 'myUsefullThing'
        }
    })
}}





You can also define a Callback globally by setting it up in the config.yml like below :, (*29)

waldo_datatable:
    js:
        createdRow: |
            function(){console.log("Do something useful here");}

Translation

You can set your own translated labels by adding in your translation catalog entries as define in Resources/translations/messages.en.yml, (*30)

You can also get the translated labels from official DataTable translation repository, by configuring the bundle like below :, (*31)

waldo_datatable:
    all:    ~
    js:
        language:
            url: "//cdn.datatables.net/plug-ins/1.10.9/i18n/Chinese.json"

This bundle includes nine translation catalogs: Arabic, Chinese, Dutch, English, Spanish, French, Italian, Polish, Russian and Turkish To get more translated entries, you can follow the official DataTable translation, (*32)

Doctrine Query Builder

To use your own query object to supply to the datatable object, you can perform this action using your own "Doctrine Query object": DatatableBundle allow to manipulate the query object provider which is now a Doctrine Query Builder object, you can use it to update the query in all its components except of course in the selected field part., (*33)

This is a classic config before using the Doctrine Query Builder:, (*34)

private function datatable()
{
    $datatable = $this->get('datatable')
                ->setEntity("XXXBundle:Entity", "e")
                ->setFields(
                        array(
                            "column1 label" => 'e.column1',
                            "_identifier_" => 'e.id')
                        )
                ->setWhere(
                    'e.column1 = :column1',
                    array('column1' => '1' )
                )
                ->setOrder("e.created", "desc");

     $qb = $datatable->getQueryBuilder()->getDoctrineQueryBuilder();
     // This is the Doctrine Query Builder object, you can
     // retrieve it and include your own change

     return $datatable;
}

This is a config that uses a Doctrine Query object a query builder :, (*35)

private function datatable()
{
    $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
    $qb->from("XXXBundle:Entity", "e")
       ->where('e.column1 = :column1')
       ->setParameters(array('column1' = 0))
       ->orderBy("e.created", "desc");

    $datatable = $this->get('datatable')
                ->setFields(
                        array(
                            "Column 1 label" => 'e.column1',
                            "_identifier_" => 'e.id')
                        );

    $datatable->getQueryBuilder()->setDoctrineQueryBuilder($qb);

    return $datatable;
}

Multiple DataTable in the same view

To declare multiple DataTables in the same view, you have to set the datatable identifier in you controller with setDatatableId : Each of your DataTable config methods ( datatable() , datatable_1() .. datatable_n() ) needs to set the same identifier used in your view:, (*36)

In the controller

protected function datatable()
{
    // ...
    return $this->get('datatable')
                ->setDatatableId('dta-unique-id_1')
                ->setEntity("XXXMyBundle:Entity", "x")
    // ...
}

protected function datatableSecond()
{
    // ...
    return $this->get('datatable')
                ->setDatatableId('dta-unique-id_2')
                ->setEntity("YYYMyBundle:Entity", "y")
    // ...
}

In the view

{{
    datatable({
        'id' : 'dta-unique-id_1',
        ...
            'js' : {
            'ajax' : path('route_for_your_datatable_action_1')
            }
    })
}}

{{
    datatable({
        'id' : 'dta-unique-id_2',
        ...
        'js' : {
            'ajax' : path('route_for_your_datatable_action_2')
        }
    })
}}

Use specific jQuery DataTable options

Some time we need to apply some specific options to a grid, like a specific width for the second column. DataTable comes with a lot a feature that you can always use, even with this bundle., (*37)

In the code below, we use the columnDefs option to fix the width of the second column., (*38)

{{
    datatable({
        'id' : 'dta-id',
        'js' : {
            'ajax' : path('route_for_your_datatable_action'),
            'columnDefs': [
                { "width": "15%", "targets": 1 }
            ]
        }
    })
}}

You really can play with all the DataTable's options., (*39)

The Versions

05/03 2018

dev-master

9999999-dev https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

31/03 2016

4.0.2

4.0.2.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

07/01 2016

4.0.1

4.0.1.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

16/12 2015

4.0.0

4.0.0.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

06/10 2015

3.0.12

3.0.12.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

05/10 2015

3.0.11

3.0.11.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

05/10 2015

3.0.10

3.0.10.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

05/10 2015

3.0.9

3.0.9.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

01/10 2015

3.0.8

3.0.8.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

01/10 2015

3.0.7

3.0.7.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

30/09 2015

3.0.6

3.0.6.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

30/09 2015

3.0.5

3.0.5.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

29/09 2015

3.0.4

3.0.4.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

28/09 2015

3.0.3

3.0.3.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

17/09 2015

3.0.2

3.0.2.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

17/09 2015

3.0.1

3.0.1.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

16/09 2015

3.0.0

3.0.0.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

14/09 2015

2.0.x-dev

2.0.9999999.9999999-dev https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

14/09 2015

2.0.1

2.0.1.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable

09/09 2015

2.0.0

2.0.0.0 https://github.com/waldo2188/DatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali hichem
by SĂ©bastien Convers

jquery symfony2 datatable