2017 © Pedro Peláez
 

library gtn-datatables

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

image

goten4/gtn-datatables

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

  • Friday, April 17, 2015
  • by goten4
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2,258 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Zend Framework 2 Module for jQuery DataTables

Build Status Coverage Status, (*1)

Introduction

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

Requirements

  • Zend Framework 2

Installation

  • Add the following line in the require section of your composer.json, (*3)

    "goten4/gtn-datatables": "dev-master", (*4)

  • Then run the following command, (*5)

    php composer.phar update, (*6)

  • Or simply clone this project into your ./vendor/ directory., (*7)

  • Enable the module in your ./config/application.config.php file., (*8)

Usage

A good example is worth a thousand words ;), (*9)

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' => ['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' => [
            [
                /**
                 * 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 array $params
     * @return Collection
     */
    public function findAll(array $params = null)
    {
        // Get the $servers, $total and $filteredCount from database (or any other data source)

        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(); ?>

The Versions