goten4/gtn-datatables

Zend Framework 2 模块,提供 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

简介

GtnDataTables 是一个为服务器端 jQuery DataTables 提供基本支持的 Zend Framework 2 模块。

要求

  • Zend Framework 2

安装

  • 在您的 composer.json 文件的 require 部分中添加以下行

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

  • 然后运行以下命令

    php composer.phar update

  • 或者简单地将此项目克隆到您的 ./vendor/ 目录。

  • 在您的 ./config/application.config.php 文件中启用该模块。

用法

一个好的例子胜过千言万语 ;)

配置

'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',
            )
        )
    )
)

收集器

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);
    }
}

列装饰器

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>';
    }
}

在控制器中

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;
}

在视图中

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