PHPackages                             m-comscience/yii2-widget-datatables - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. m-comscience/yii2-widget-datatables

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

m-comscience/yii2-widget-datatables
===================================

DataTables plug-in jQuery Javascript library. for Yii2 framework

v1.0.0(7y ago)02.0k1BSD-3-ClausePHP

Since Nov 25Pushed 7y ago1 watchersCompare

[ Source](https://github.com/MComScience/yii2-widget-datatables)[ Packagist](https://packagist.org/packages/m-comscience/yii2-widget-datatables)[ Docs](https://github.com/MComScience/yii2-widget-datatables)[ RSS](/packages/m-comscience-yii2-widget-datatables/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (16)Versions (2)Used By (0)

yii2-widget-datatables
======================

[](#yii2-widget-datatables)

DataTables plug-in jQuery Javascript library. for Yii2 framework.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Install

```
composer require m-comscience/yii2-widget-datatables '@dev'

```

Usage
-----

[](#usage)

Basic:

```
use mcomscience\datatables\DataTables;

```

Ajax data source (arrays)

```
use mcomscience\datatables\DataTables;
// data source
/*
{
  "data": [
    [
      "Tiger Nixon",
      "System Architect",
      "Edinburgh",
      "5421",
      "2011/04/25",
      "$320,800"
    ],
}
*/

```

Ajax data source (objects) [yii2-data-column ](https://github.com/MComScience/yii2-data-column)

Controller

```
use mcomscience\data\DataColumn;
use mcomscience\data\ActionColumn;
use yii\data\ArrayDataProvider;

public function ActionDataPosition()
{
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    $query = (new \yii\db\Query())
        ->select([
            'position.*',
        ])
        ->from('position')
        ->all();
    $dataProvider = new ArrayDataProvider([
        'allModels' => $query,
    ]);
    // or
    /*
    use yii\data\ActiveDataProvider;

    $query = Position::find()->where(['status' => 1]);

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort' => [
            'defaultOrder' => [
                'created_at' => SORT_DESC,
                'title' => SORT_ASC,
            ]
        ],
    ]);
    */
    $columns = Yii::createObject([
        'class' => DataColumn::className(),
        'dataProvider' => $dataProvider,
        'formatter' => Yii::$app->formatter,
        'columns' => [
            [
                'attribute' => 'id',
            ],
            [
                'attribute' => 'name',
            ],
            [
                'attribute' => 'position',
            ],
            [
                'attribute' => 'salary',
            ],
            [
                'attribute' => 'start_date',
                'format' => ['date','php:d/m/Y'],
            ],
            [
                'attribute' => 'office',
            ],
            [
                'attribute' => 'extn',
            ],
            [
                'class' => ActionColumn::className(),
                'template' => '{view} {update} {delete}',
                'viewOptions' => [
                    'title' => Yii::t('yii','View'),
                    //'label' => 'View'
                ],
                'updateOptions' => [
                    'role' => 'modal-remote',
                    'title' => Yii::t('yii','Edit'),
                ],
                'deleteOptions' => [
                    'class' => 'text-danger on-delete',
                    'title' => Yii::t('yii','Delete'),
                ],
                'urlCreator' => function ($action, $model, $key, $index) {
                    if ($action == 'update') {
                        return Url::to(['update', 'id' => $key]);
                    }
                    if ($action == 'delete') {
                        return Url::to(['delete', 'id' => $key]);
                    }
                },
            ],
            /*
            DropdownButton
            [
                'class' => ActionColumn::className(),
                'template' => '{view} {btn1}',
                'dropdown' => true,
                'dropdownButton' => [
                    'label' => 'Actions',
                    'class' => 'btn btn-success'
                ],
                'viewOptions' => [
                    'role' => 'modal-remote',
                    'title' => 'Detail',
                    'label' => 'Detail',
                ],
                'buttons' => [
                    'btn1' => function ($url, $model, $key) {
                        return Html::tag('li', Html::a('Add', $url, ['data-pjax' => 0]));
                    },
                ],
                'urlCreator' => function ($action, $model, $key, $index) {
                    if ($action == 'view') {
                        return Url::to(['view', 'id' => $key]);
                    }
                },
            ],
            */
        ],
    ]);
    return ['data' => $columns->renderDataColumns()];
}
```

View

```
use mcomscience\datatables\DataTables;
// data source
/*
{
  "data": [
    {
      "id": "1",
      "name": "Tiger Nixon",
      "position": "System Architect",
      "salary": "$320,800",
      "start_date": "2011/04/25",
      "office": "Edinburgh",
      "extn": "5421"
    },
}
*/

```

Callback Function

```
use mcomscience\datatables\DataTables;
