All Projects → NullRefExcep → Yii2 Datatables

NullRefExcep / Yii2 Datatables

Licence: mit
Yii2 Widget for DataTables jQuery plug-in

Projects that are alternatives of or similar to Yii2 Datatables

Vue Data Tables
A simple, customizable and pageable table with SSR support, based on vue2 and element-ui
Stars: ✭ 976 (+1356.72%)
Mutual labels:  datatables
Modern Datatables
They are many ways to build reactive web interfaces but do we really need to add the complexity of JavaScript frameworks like Vue.js or React?
Stars: ✭ 48 (-28.36%)
Mutual labels:  datatables
Yii2 Psr Log Target
Yii 2.0 log target that is able to write messages to PSR-3 compatible logger
Stars: ✭ 58 (-13.43%)
Mutual labels:  yii2-extension
Spicy Datatable
React.js datatables without jQuery. Smart react datatable that includes search, pagination and localization.
Stars: ✭ 36 (-46.27%)
Mutual labels:  datatables
Yii2 Lifecycle Behavior
Define the lifecycle of a model by defining allowed status changes.
Stars: ✭ 47 (-29.85%)
Mutual labels:  yii2-extension
Yii2 Rabbitmq
RabbitMQ Extension for Yii2
Stars: ✭ 52 (-22.39%)
Mutual labels:  yii2-extension
Yii2 Many To Many
Implementation of Many-to-many relationship for Yii 2 framework
Stars: ✭ 30 (-55.22%)
Mutual labels:  yii2-extension
Yii2 Jwt
JWT implementation for Yii2 Authorization process
Stars: ✭ 61 (-8.96%)
Mutual labels:  yii2-extension
Yii2 Editable
Editable widget and column for gridview.
Stars: ✭ 47 (-29.85%)
Mutual labels:  yii2-extension
Php frameworks analysis
php框架源码分析
Stars: ✭ 57 (-14.93%)
Mutual labels:  yii2-extension
Yii2 Gravatar
Gravatar Widget for Yii Framework 2
Stars: ✭ 36 (-46.27%)
Mutual labels:  yii2-extension
Yii2 Relation Trait
Yii 2 Models add functionality for load with relation, & transactional save with relation PLUS soft delete/restore feature
Stars: ✭ 47 (-29.85%)
Mutual labels:  yii2-extension
Config
Yii2 application runtime configuration support
Stars: ✭ 54 (-19.4%)
Mutual labels:  yii2-extension
Yii2 Image Attachment
This extension intended to handle images associated with model.
Stars: ✭ 35 (-47.76%)
Mutual labels:  yii2-extension
Sitemap
Site map creation support
Stars: ✭ 59 (-11.94%)
Mutual labels:  yii2-extension
Dashvis
An open-source Dashboard built for users, to organize their resources via Tables and Folders.
Stars: ✭ 31 (-53.73%)
Mutual labels:  datatables
Yii2 Social Share
With this extension you can share data from your web pages to any social network!
Stars: ✭ 48 (-28.36%)
Mutual labels:  yii2-extension
Vueye Table
A data table created using Vue.js
Stars: ✭ 64 (-4.48%)
Mutual labels:  datatables
Yii2 Collection
Collection extension for Yii 2
Stars: ✭ 62 (-7.46%)
Mutual labels:  yii2-extension
Svelte Simple Datatables
A Datatable component for Svelte
Stars: ✭ 56 (-16.42%)
Mutual labels:  datatables

Yii2 DataTables

Yii2 Widget for DataTables plug-in for jQuery

Installation

The preferred way to install this extension is through composer.

Either run

composer require nullref/yii2-datatables

or add

"nullref/yii2-datatables": "~1.0"

to the require section of your composer.json file.

Basic Usage

<?= \nullref\datatable\DataTable::widget([
    'data' => $dataProvider->getModels(),
    'columns' => [
        'id',
        'name',
        'email'
    ],
]) ?>

DataTable options

Also you can use all Datatables options

To pass them as widget options:

<?= \nullref\datatable\DataTable::widget([
    'data' => $dataProvider->getModels(),
    'scrollY' => '200px',
    'scrollCollapse' => true,
    'paging' => false,
    'columns' => [
        'name',
        'email'
    ],
    'withColumnFilter' => true,
]) ?>

Specifies header label and css class for cell

    <?= \nullref\datatable\DataTable::widget([
        'columns' => [
            //other columns
            [
                'data' => 'active',
                'title' => 'Is active',
                'sClass' => 'active-cell-css-class',
            ],
        ],
    ]) ?>

Add Links to row

    <?= \nullref\datatable\DataTable::widget([
        'columns' => [
            //other columns
            [
                'class' => 'nullref\datatable\LinkColumn',
                'url' => ['/model/delete'],
                'linkOptions' => ['data-confirm' => 'Are you sure you want to delete this item?', 'data-method' => 'post'],
                'label' => 'Delete',
            ],
        ],
    ]) ?>

Properties of LinkColumn:

  • label - text placed in a tag;
  • title - header title of column;
  • url - will be passed to Url::to();
  • linkOptions - HTML options of the a tag;
  • queryParams - array of params added to url, ['id'] by default;
  • render - custom render js function. E.g:
//config ...
    'columns' => [
        //...
        [
            'class' => 'nullref\datatable\LinkColumn',
            'queryParams' => ['some_id'],
            'render' => new JsExpression('function render(data, type, row, meta ){
                return "<a href=\"/custom/url/"+row["some_id"]+"\">View</a>"
            }'),
        ],
    ],
//...

You should pass fields that are using at render function to queryParams property

Column filtering

You can add column filtering functionality by setting option withColumnFilter to true :

  • By default it generates a text field as filter input.
  • It can be replaced by a combo box using filter parameter when defining column. It should be a associative array where key is used as filter (value sent to server) and value for cell rendering
  • It can be avoided by setting filter to false
    <?= \nullref\datatable\DataTable::widget([
        'columns' => [
            'id',
            //...
            [
                'data' => 'active',
                'title' => \Yii::t('app', 'Is active'),
                'filter' => ['true' => 'Yes', 'false' => 'No'],
            ],
            [
                'data' => 'last_connection',
                'filter' => false,
            ],
        ],
    ]) ?>
//...

In this example above, filter for active field sent to server will contains 'true' or 'false' but the cell content will be 'Yes' or 'No' and the filter will be rendered as a combo box.

No filter will be generated for last_connection attrribute.

Advanced column definition

Cell rendering or filter can be customized using \nullref\datatable\DataTableColumn class.

    <?= \nullref\datatable\DataTable::widget([
        'columns' => [
            //other columns
            [
                'class' => 'nullref\datatable\DataTableColumn', // can be omitted
                'data' => 'active',
                'renderFiler' => new \yii\web\JsExpression('function() { ' .
                    'return jQuery(\'<input type="checkbox" value="true"/> Active only\'); ' .
                '}'),
                'render' => new \yii\web\JsExpression('function(data, type, row, meta) { ' .
                    'return jQuery(\'<input type="checkbox" value="true" disabled/>\')' .
                    '    .prop(\'checked\', data == \'true\'); ' .
                    '}'),
            ],
        ],
    ]) ?>

Styling

DataTables supports several styling solutions, including Bootstrap, jQuery UI, Foundation.

'assetManager' => [
    'bundles' => [
        'nullref\datatable\assets\DataTableAsset' => [
            'styling' => \nullref\datatable\assets\DataTableAsset::STYLING_BOOTSTRAP,
        ]
    ],
],

Bootstrap

Bootstrap tables require the class 'table', so you'll need to add the 'table' class using tableOptions via the widget config.

<?= \nullref\datatable\DataTable::widget([
    'data' => $dataProvider->getModels(),
    'tableOptions' => [
        'class' => 'table',
    ],
    'columns' => [
        'id',
        'name',
        'email',
    ],
]) ?>

Custom assets

It's possible to use custom styles and scripts:

'nullref\datatable\assets\DataTableAsset' => [
    'sourcePath' => '@webroot/js/plugin/datatables/',
    'js' => [
        'jquery.dataTables-1.10-cust.js',
        'DT_bootstrap.js',
    ],
    'css' => [
        'data-table.css',
    ],
    'styling' => false,
]

Server-side processing

To enable server-side processing add DataTableAction to controller like this:

class SomeController extends Controller
{
    public function actions()
    {
        return [
            'datatables' => [
                'class' => 'nullref\datatable\DataTableAction',
                'query' => Model::find(),
            ],
        ];
    }
}

Searching and ordering can be customized using closures

public function actions()
{
    return [
         'datatables' => [
             'class' => 'nullref\datatable\DataTableAction',
             'query' => Model::find(),
             'applyOrder' => function($query, $columns, $order) {
                //custom ordering logic
                $orderBy = [];
                foreach ($order as $orderItem) {
                    $orderBy[$columns[$orderItem['column']]['data']] = $orderItem['dir'] == 'asc' ? SORT_ASC : SORT_DESC;
                }
                return $query->orderBy($orderBy);
             },
             'applyFilter' => function($query, $columns, $search) {
                //custom search logic
                $modelClass = $query->modelClass;
                $schema = $modelClass::getTableSchema()->columns;
                foreach ($columns as $column) {
                    if ($column['searchable'] == 'true' && array_key_exists($column['data'], $schema) !== false) {
                        $value = empty($search['value']) ? $column['search']['value'] : $search['value'];
                        $query->orFilterWhere(['like', $column['data'], $value]);
                    }
                }
                return $query;
             },
         ],
    ];
}

If you need to get some relation data you can call join or similar methods from $query in applyFilter closure.

And add options to widget:

<?= \nullref\datatable\DataTable::widget([
    /** ... */
    'serverSide' => true,
    'ajax' => '/site/datatables',
]) ?>

Extra columns

If need to use some custom fields from your model at your render function at column you could pass extraColumns param.

It available at DataTable widget, column and server side action definition:

<?= \nullref\datatable\DataTable::widget([
    /** ... */
    'data' => $dataProvider->getModels(),
    'extraColumns' => ['customPrice'],
    'columns' => [
        [
            'title' => 'Custom column',
            'extraColumns' => ['customField'],
            'render' => new JsExpression($customColumnRender),
        ],
    ],
]) ?>
class SomeController extends Controller
{
    public function actions()
    {
        return [
            'datatables' => [
                'class' => 'nullref\datatable\DataTableAction',
                'query' => Model::find(),
                'extraColumns' => ['customPrice'],
            ],
        ];
    }
}
<?= \nullref\datatable\DataTable::widget([
    /** ... */
    'extraColumns' => ['customPrice'],
]) ?>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].