Yml Style Example

Version: 0.4.0 +

In keeping with the Doctrine\Symfony style, DataTables can be defined as yml config files. DataTables can be defined in the dataTable.yml file of any bundle and will be loaded and available through the Table Manger

Implementation

To implement a yml based data table, simply define the required schema within the dataTable.yml file.

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.

Yml Schema

DataTableId:
  entity: (Doctrine Entity Namespace)
  queryBuilder: (Name of function on Repository that returns query builder)
  deferLoading: false
  displayLength: 20
  paginate: true
  sortable: true
  searchable: true
  serverSideProcessing: false
  processign: true
  info: true
  paginationType: full_numbers
  changeLength: true
  columns:
    columnSource:
      name: (Column Name)
      visible: true
      sortable: true
      searchable: true
      class: (css class)
      width: (width value)
      defaultSort: false
      format:
        dataFields:
          fieldName: (field source)
          ...
        template: (path to template)
    ...

Example Table

Id Question Answer Last Name First Name User

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 DtYmlStyleController
 *
 * @Route("/style/yml")
 *
 * @package Brown298\DtTestBundle\Controller
 */
class DtYmlStyleController extends Controller
{
    /**
     * indexAction
     *
     * @Route("", name="yml_style")
     * @Template()
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return array
     */
    public function indexAction(Request $request)
    {
        $dataTable = $this->get('data_tables.manager')->getTable('ymlDataTable');
        if ($response = $dataTable->ProcessRequest($request)) {
            return $response;
        }

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

}

Yml Configuration

ymlDataTable:
  entity: Brown298\DtTestBundle\Entity\Faq
  columns:
    faq.id:
      name: "Id"
    faq.question:
      name: "Question"
    faq.answer:
      name: "Answer"
    faq.createdBy.lastName:
      name: "Last Name"
    faq.createdBy.firstName:
      name: "First Name"
    faq.createdBy.username:
      name: "User"
      format:
        dataFields:
          id: faq.createdBy.id
          name: faq.createdBy.username
        template: Brown298DtTestBundle:DtYmlStyle:nameFormatter.html.twig

Twig Index (index.html.twig)

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

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

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

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

{% endblock %}

Twig Formatter (nameFormatter.html.twig)

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