Search by

conquer / datatables

borodulin

Yii2 datatables widget

Package info

github.com/borodulin/yii2-datatables

Type:yii2-extension

pkg:composer/conquer/datatables

Statistics

Installs: 19

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.2.0 2026-11-03 04:12 UTC

This package is auto-updated.

Last update: 2026-11-03 04:14:17 UTC


README

Latest Version Total Downloads License

Description

Yii2 integration of DataTables 1.10 with Bootstrap styling:

  • DatatablesWidget renders a table and initializes DataTables, registering only the extensions you use (AutoFill, Buttons, ColReorder, FixedColumns, FixedHeader, KeyTable, Responsive, RowGroup, RowReorder, Scroller, Select);
  • DatatablesAction implements the DataTables server-side processing protocol on top of a Yii Query / ActiveQuery, including paging, ordering and global/per-column search.

The interface language is taken from Yii::$app->language automatically.

Requirements

  • PHP 7.4 or 8.x
  • Yii 2.0.6 or newer (2.0.x)
  • asset-packagist repository configured in your application's composer.json (standard for Yii2 app templates)

Installation

Install the extension via Composer:

composer require conquer/datatables:^1.1

Usage

Server-side processing

Controller:

use conquer\datatables\DatatablesAction;

class UserController extends \yii\web\Controller
{
    public function actions()
    {
        return [
            'datatables' => [
                'class' => DatatablesAction::class,
                'query' => User::find(),
                // only these fields are sent to the browser
                'attributes' => ['id', 'username', 'email'],
                // maximum rows per request (client "length", including -1, is capped)
                'maxLength' => 500,
            ],
        ];
    }
}

For an ActiveQuery, searching and ordering are applied automatically to the columns that exist in the table schema (LIKE for strings, = for numbers and booleans). For any other query, or to customize the behavior, provide callbacks:

'filterCallback' => function ($query, $columns, $search) { /* ... */ },
'orderCallback' => function ($query, $columns, $order) { /* ... */ },

query can also be a callable returning the query.

Security: without attributes every column selected by the query is returned to the client (for User::find() that includes password hashes and tokens). Always set attributes or restrict select().

View:

use conquer\datatables\DatatablesWidget;

echo DatatablesWidget::widget([
    'ajax' => ['user/datatables'],
    'columns' => [
        'id' => 'ID',
        'username' => 'Username',
        'email' => 'Email',
    ],
    'tableOptions' => ['class' => 'table table-striped table-bordered'],
    'responsive' => true,
    'stateSave' => true,
]);

Keys of columns are the data fields, values are the column titles. When ajax is set, serverSide is enabled by default.

Client-side data

echo DatatablesWidget::widget([
    'columns' => ['name' => 'Name', 'price' => 'Price'],
    'data' => [
        ['name' => 'Apple', 'price' => 1.2],
        ['name' => 'Orange', 'price' => 0.9],
    ],
]);

Options

Common DataTables options are available as widget properties: autoWidth, deferRender, info, lengthChange, ordering, paging, processing, scrollX, scrollY, searching, stateSave. Extension options (autoFill, buttons, colReorder, fixedColumns, fixedHeader, keys, responsive, rowGroup, rowReorder, scroller, select) also register the corresponding assets. Any other DataTables option can be passed via options.

License

conquer/datatables is released under the MIT License. See the bundled LICENSE for details.