goten4 / gtn-datatables
Zend Framework 2 Module that provides Server Side support for jQuery DataTables
Installs: 2 318
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/goten4/gtn-datatables
Requires
- php: >=5.3.3
- zendframework/zend-i18n: ~2.3
- zendframework/zend-loader: ~2.3
- zendframework/zend-modulemanager: ~2.3
- zendframework/zend-mvc: ~2.3
- zendframework/zend-servicemanager: ~2.3
- zendframework/zend-view: ~2.3
Requires (Dev)
- phpunit/phpunit: 3.7.*
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2020-01-06 03:36:38 UTC
README
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.phpfile.
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(); ?>