temhri/yii2-datatables

DataTables widget for Yii2

Installs: 51

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:yii2-extension

1.0.0 2023-09-08 06:32 UTC

This package is not auto-updated.

Last update: 2024-05-17 10:14:41 UTC


README

This extension provides the DataTables integration for the Yii2 framework.

Latest Stable Version Total Downloads Latest Unstable Version License

Installation

The preferred way to install this extension is through composer.

With Composer installed, you can then install the extension using the following commands:

composer require --prefer-dist temhri/yii2-datatables "*"

or add the bwlow line to the require section of your composer.json file.

"temhri/yii2-datatables": "*"

Usage

Use DataTables as any other other Yii2 widget.

use temhri\datatables\DataTables;
<?php
    $searchModel = new SearchModel();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    echo DataTables::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ...
        ...
    ],
]);?>

This extension uses the Bootstrap integration plugin to provide a Yii2 style by default.

The TableTools plugin is also available. Specify the DOM and the tableTools settings in the clientOptions array as the following example.

...
'clientOptions' => [
    "lengthMenu"=> [[20,-1], [20,Yii::t('app',"All")]],
    "info"=>false,
    "responsive"=>true,
    "dom"=> 'lfTrtip',
    "tableTools"=>[
        "aButtons"=> [
            [
            "sExtends"=> "copy",
            "sButtonText"=> Yii::t('app',"Copy to clipboard")
            ],[
            "sExtends"=> "csv",
            "sButtonText"=> Yii::t('app',"Save to CSV")
            ],[
            "sExtends"=> "xls",
            "oSelectorOpts"=> ["page"=> 'current']
            ],[
            "sExtends"=> "pdf",
            "sButtonText"=> Yii::t('app',"Save to PDF")
            ],[
            "sExtends"=> "print",
            "sButtonText"=> Yii::t('app',"Print")
            ],
        ]
    ]
],
...

You can also use DataTables in the JavaScript layer of your application. To achieve this, you need to include DataTables as a dependency of your Asset file. In this case, you could use yii\grid\GridView or using the datatables options retrieve => true to avoid errors. In both case all options must be in the Javascript object.

public $depends = [
...
'temhri\datatables\DataTablesAsset',
...
];