Xml Style Example

Version: 0.4.0 +

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

Implementation

To implement a xml based data table, simply define the required schema within the dataTable.xml 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.

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

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

}

xml Configuration

<?xml version="1.0" ?>

<DataTables>
    <Table id="xmlDataTable" entity="Brown298\DtTestBundle\Entity\Faq">
        <Columns>
            <Column source="faq.id" name="Id" />
            <Column source="faq.question" name="Question" />
            <Column source="faq.answer" name="Answer" />
            <Column source="faq.createdBy.lastName" name="Last Name" />
            <Column source="faq.createdBy.firstName" name="First Name" />
            <Column source="faq.createdBy.username" name="User" >
                <Format>
                    <DataFields>
                        <Field source="faq.createdBy.id" name="id"/>
                        <Field source="faq.createdBy.username" name="name" />
                    </DataFields>
                    <Template source="Brown298DtTestBundle:DtYmlStyle:nameFormatter.html.twig" />
                </Format>
            </Column>
        </Columns>
    </Table>
    <Table id="xmlDataTable2" entity="Brown298\DtTestBundle\Entity\Faq">
        <Columns>
            <Column source="faq.id" name="Id" />
            <Column source="faq.question" name="Question" />
            <Column source="faq.answer" name="Answer" />
            <Column source="faq.createdBy.lastName" name="Last Name" />
            <Column source="faq.createdBy.firstName" name="First Name" />
            <Column source="faq.createdBy.username" name="User" >
                <Format>
                    <DataFields>
                        <Field source="faq.createdBy.id" name="id"/>
                        <Field source="faq.createdBy.username" name="name" />
                    </DataFields>
                    <Template source="Brown298DtTestBundle:DtYmlStyle:nameFormatter.html.twig" />
                </Format>
            </Column>
        </Columns>
    </Table>
</DataTables>

Twig Index (index.html.twig)

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

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

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

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

{% endblock %}

Twig Formatter (nameFormatter.html.twig)

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