Annotation Style Example

Version: 0.4.0 +

In keeping with the Doctrine\Symfony style, DataTables can be defined as annotations on an object. These annotations can go into their own Model, or can be attached to exising Doctrine Entities.

Implementation

If you are used to using annotations for your Doctrine Entities, using this method should be easy. Simply add a "Table" annotation specifiying the id to the model class. Add "Column" annotations to some of the properties and you are done.

In order to make the controller implementation for annotation easier, a DataTables manager (similar to an Entity Manager) has been added. It gathers all of your tables and provides an easy way to access them. Note: The DataTables manager only manages configuration based tables, ie not modle/controller/repository tables.

Annotation Parameters

Table
  • Id (string) required
    - id for the table (used for javascript id)
  • deferLoading (boolean) Default: False
    - should DataTables loading be deferred.
  • paginate (boolean) Default: True
    - should DataTables paginate
  • sortable (boolean) Default: True
    - should DataTables allow sorting
  • changeLength (boolean) Default: Trye
    - allow length change
  • displayLength (integer) Default: 20
    - length of the display for DataTables
  • serverSideProcessing (boolean) Default: False
    - should DataTables look back to the server for searches/sorting etc.
  • processing (boolean) Default: True
    - should DataTables display the processing message
  • paginationType (string) Default: 'full_numbers'
    - should pagination type to use
  • entity (string) Default: null
    - Entity namespace, used to get repository to run querybuilder function
  • queryBuilder (string) Default: null
    - function name on repository (specified by the entity string)
Column
  • source (string) required
    - key to identify where to get the data from
  • name (string) required
    - Title to display at head of table
  • visible (boolean) Default: True
    - should this column display
  • sortable (boolean) Default: True
    - is this column sortable
  • searchable (boolean) Default: True
    - is this column searchable
  • class (string) Default: null
    - css class to apply to 'td' tags in this column
  • width (string) Default: null
    - width to apply to 'td' tags in this column
DefaultSort

No parameters, use to indicate default sorting column

Format
  • DataFields (mixed) required
    - data to be displayed, can be a single field or an array of values
  • template (string)
    - path to template for rendering engine to use

Example Table

Id Question Answer Last Name Last Name User User Roles

Code

Controller

<?php
namespace Brown298\DtTestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
 * Class DtModelStyleController
 *
 * @Route("/style/annotation")
 *
 * @package Brown298\DtTestBundle\Controller
 */
class DtAnnotationStyleController extends Controller
{
    /**
     * indexAction
     *
     * @Route("", name="annotation_style")
     * @Template()
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return array
     */
    public function indexAction(Request $request)
    {
        $dataTable = $this->get('data_tables.manager')->getTable('annotationTable');
        if ($response = $dataTable->ProcessRequest($request)) {
            return $response;
        }

        return array(
            'dataTable' => $dataTable,
        );
    }
}

Model (AnnotationTable.php)

<?php
namespace Brown298\DtTestBundle\Model;

use Brown298\DataTablesBundle\MetaData as DataTable;
use Brown298\DataTablesBundle\Model\DataTable\QueryBuilderDataTableInterface;
use Brown298\DataTablesBundle\Test\DataTable\QueryBuilderDataTable;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Templating\EngineInterface;

/**
 * Class AnnotationTable
 *
 * @package Brown298\DtTestBundle\Model
 *
 * @DataTable\Table(id="annotationTable")
 */
class AnnotationTable extends QueryBuilderDataTable implements QueryBuilderDataTableInterface
{
    /**
     * @var int
     * @DataTable\Column(source="faq.id", name="Id")
     */
    public $id;

    /**
     * @var string
     * @DataTable\Column(source="faq.question", name="Question")
     * @DataTable\DefaultSort()
     */
    public $question;

    /**
     * @var string
     * @DataTable\Column(source="faq.answer", name="Answer")
     * @DataTable\DefaultSort()
     */
    public $answer;

    /**
     * @var string
     * @DataTable\Column(source="faq.createdBy.lastName", name="Last Name")
     */
    public $lastName;

    /**
     * @var string
     * @DataTable\Column(source="faq.createdBy.firstName", name="Last Name")
     */
    public $firstName;

    /**
     * @var string
     * @DataTable\Column(source="faq.createdBy.username", name="User")
     * @DataTable\Format(dataFields={"id":"faq.createdBy.id","name":"faq.createdBy.username"}, template="Brown298DtTestBundle:DtAnnotationStyle:nameFormatter.html.twig")
     */
    public $name;

    /**
     * @var string
     * @DataTable\Column(source="faq.createdBy.roles.name", name="User Roles")
     */
    public $roles;

    /**
     * @var bool hydrate results to doctrine objects
     */
    public $hydrateObjects = true;

    /**
     * getQueryBuilder
     *
     * @param Request $request
     *
     * @return null
     */
    public function getQueryBuilder(Request $request = null)
    {
        $userRepository = $this->container->get('doctrine.orm.entity_manager')
            ->getRepository('Brown298\DtTestBundle\Entity\Faq');
        $qb = $userRepository->createQueryBuilder('faq')
            ->innerJoin('faq.createdBy', 'user')
            ->andWhere('user.deleted = false');

        return $qb;
    }
}

Twig Index (index.html.twig)

{% extends 'Brown298DtTestBundle::base.html.twig' %}

{% block body %}
    <h2>Annotation Style Example</h2>

    {% include "Brown298DtTestBundle:DtAnnotationStyle:content.html.twig" %}

    <h4>Example Table</h4>
    <div class="table-bordered">
        {{ addDataTable(dataTable) }}
    </div>

{% endblock %}

Twig Formatter (nameFormatter.html.twig)

{{ id }}: <strong>{{ name }}</strong>