2017 © Pedro Peláez
 

library php-datatables

Server-Side backend for DataTables

image

keanor/php-datatables

Server-Side backend for DataTables

  • Wednesday, March 1, 2017
  • by keanor
  • Repository
  • 1 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

PHP DataTables Server-Side

Install:, (*1)

$ composer require keanor/php-datatables

Example, for users table:, (*2)

1) Create datatable class:, (*3)

<?php
/**
 * Created by PhpStorm.
 * User: keanor
 * Date: 17.02.17
 * Time: 11:39
 */
namespace Administration\DataTable;

use PHPDataTables\AbstractDataTable;

/**
 * Class UserDataTable
 * 
 * @package PHPDataTables\DataTables
 */
class UserDataTable extends AbstractDataTable
{
    /**
     * @return string
     */
    public function getTableName(): string
    {
        return 'users';
    }

    /**
     * Initialize table
     */
    public function init()
    {
        $this->addColumn([
            'name' => 'id',
            'options' => [
                'search_type' => 'exactly',
                'label' => 'ID',
            ],
        ]);
        $this->addColumn([
            'name' => 'role',
            'options' => [
                'label' => 'Роль',
            ],
        ]);
        $this->addColumn([
            'name' => 'phone',
            'options' => [
                'search_type' => 'fulltext',
                'label' => 'Телефон',
            ],
        ]);
        $this->addColumn([
            'name' => 'last_name',
            'options' => [
                'label' => 'Фамилия',
            ],
        ]);
        $this->addColumn([
            'name' => 'first_name',
            'options' => [
                'label' => 'Имя',
            ],
        ]);
        $this->addColumn([
            'name' => 'second_name',
            'options' => [
                'label' => 'Отчество',
            ],
        ]);
    }
}

2) Inject DataTableView into page with table:, (*4)

2.1) Create DataTable in controller:, (*5)

// ...

        $adapter = new DoctrineDBAL($this->connection);
        $usersDataTable = new UserDataTable($adapter);

        $dataTableView = new DataTableView(
            '#usersTable', // HTML ID Attribute
            '/administration/user/data', // ajax URL
            $usersDataTable,
            [ // html tag <table> attributes
                'id' => 'usersTable',
                'class' => 'table table-striped table-bordered',
                'cellspacing' => '0',
                'width' => '100%',
            ]
        );

// ... invoke renderer or include view

2.2) Invoke DataTableView in template, (*6)

For native php:, (*7)

// render table
echo $dataTableView->renderHtml();

// render js
echo '';

For twig:, (*8)

{% block content %}
    {{ dataTableView.renderHtml() | raw }}
{% endblock %}
{% block inline %}
    <script type="text/javascript">
    {{ dataTableView.renderJs() | raw }}
    </script>
{% endblock %}

3) Create data request handler, (*9)

In controller:, (*10)

        $adapter = new DoctrineDBAL($this->connection);
        $table = new UserDataTable($adapter);
        $data = $table->getData($this->getRequest());
        // echo json_encode($data);
        return new JsonModel($data);

Propose pull requests!, (*11)

The Versions

01/03 2017

dev-master

9999999-dev https://github.com/keanor/php-datatables

Server-Side backend for DataTables

  Sources   Download

MIT

The Requires

 

25/02 2017