goten4/gtn-datatables

Zend Framework 2 Module that provides Server Side support for jQuery DataTables

dev-master 2015-04-17 07:41 UTC

This package is not auto-updated.

Last update: 2020-01-06 03:36:38 UTC


README

Build Status Coverage Status

Introduction

GtnDataTables is a Zend Framework 2 module providing basics support for server side jQuery DataTables.

Requirements

  • Zend Framework 2

Installation

  • Add the following line in the require section of your composer.json

    "goten4/gtn-datatables": "dev-master"

  • Then run the following command

    php composer.phar update

  • Or simply clone this project into your ./vendor/ directory.

  • Enable the module in your ./config/application.config.php file.

Usage

A good example is worth a thousand words ;)

Configuration

'datatables' => array(
    'servers_datatable' => array(
        /**
         * Id attribute of the table HTML element.
         * Optional: if not provided the key of the datatable config is used (servers_datatable here).
         */
        'id' => 'servers',

        /**
         * Class attribute of the table HTML element.
         * Optional.
         */
        'classes' => array('table', 'bootstrap-datatable'),

        /**
         * Must implements Zend\ServiceManager\FactoryInterface.
         * createService method must return GtnDataTables\CollectorInterface.
         * Mandatory.
         */
        'collectorFactory' => 'MyProject\Service\MyCollectorFactory',

        /**
         * List of the columns of the datatable.
         * Mandatory.
         */
        'columns' => array(
            array(
                /**
                 * Must extend GtnDataTables\View\AbstractDecorator.
                 * Mandatory.
                 */
                'decorator' => 'MyProject\View\MyDecorator',

                /**
                 * Used to identify the column for ordering.
                 * Optionnal (if the column is not orderable).
                 */
                'key' => 'name',
            )
        )
    )
)

Collector

class ServersCollector implements CollectorInterface
{
    /**
     * @param int    $start
     * @param int    $length
     * @param string $search
     * @param array  $order
     * @return array
     */
    public function findAll($start = null, $length = null, $search = null, $order = null)
    {
        // Get the $servers, $total and $filteredCount
        return Collection::factory($servers, $total, $filteredCount);
    }
}

Column Decorator

class ServerNameDecorator extends AbstractDecorator
{
    /**
     * @return string
     */
    public function decorateTitle()
    {
        return $this->getViewHelperManager()->get('translator')->translate('Server');
    }

    /**
     * @param Server $object
     * @return string
     */
    public function decorateValue($object)
    {
        return '<strong>' . $object->getName() . '</strong>';
    }
}

In the controller

public function indexAction()
{
    $model = new JsonModel();
    $datatable = $this->getServiceLocator()->get('servers_datatable');
    $result = $datatable->getResult($this->params()->fromQuery());
    $model->setVariable('draw', $result->getDraw());
    $model->setVariable('recordsTotal', $result->getRecordsTotal());
    $model->setVariable('recordsFiltered', $result->getRecordsFiltered());
    $model->setVariable('data', $result->getData());
    return $model;
}

In the view

<?php echo $this->dataTable('servers_datatable')->renderHtml(); ?>