PHP code example of keanor / php-datatables

1. Go to this page and download the library: Download keanor/php-datatables library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

keanor / php-datatables example snippets



/**
 * 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' => 'Отчество',
            ],
        ]);
    }
}

// ...

        $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 

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

// render js
echo '<script type="text/javascript">';
echo $dataTableView->renderJs();
echo '</script>';

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