├── .gitignore ├── DataTables.php ├── DataTablesAsset.php ├── DataTablesBootstrapAsset.php ├── DataTablesTableToolsAsset.php ├── LICENSE ├── README.md └── composer.json /.gitignore: -------------------------------------------------------------------------------- 1 | # phpstorm project files 2 | .idea 3 | 4 | # netbeans project files 5 | nbproject 6 | 7 | # zend studio for eclipse project files 8 | .buildpath 9 | .project 10 | .settings 11 | 12 | # windows thumbnail cache 13 | Thumbs.db -------------------------------------------------------------------------------- /DataTables.php: -------------------------------------------------------------------------------- 1 | 5 | * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) 6 | * @package yii2-widget-datatables 7 | */ 8 | namespace fedemotta\datatables; 9 | 10 | use yii\helpers\Json; 11 | use yii\helpers\ArrayHelper; 12 | use yii\helpers\Html; 13 | 14 | 15 | /** 16 | * Datatables Yii2 widget 17 | * @author Federico Nicolás Motta 18 | */ 19 | class DataTables extends \yii\grid\GridView 20 | { 21 | /** 22 | * @var array the HTML attributes for the container tag of the datatables view. 23 | * The "tag" element specifies the tag name of the container element and defaults to "div". 24 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. 25 | */ 26 | public $options = []; 27 | 28 | /** 29 | * @var array the HTML attributes for the datatables table element. 30 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. 31 | */ 32 | public $tableOptions = ["class"=>"table table-striped table-bordered","cellspacing"=>"0", "width"=>"100%"]; 33 | 34 | /** 35 | * @var array the HTML attributes for the datatables table element. 36 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. 37 | */ 38 | public $clientOptions = []; 39 | 40 | 41 | /** 42 | * Runs the widget. 43 | */ 44 | public function run() 45 | { 46 | $clientOptions = $this->getClientOptions(); 47 | $view = $this->getView(); 48 | $id = $this->tableOptions['id']; 49 | 50 | //Bootstrap3 Asset by default 51 | DataTablesBootstrapAsset::register($view); 52 | 53 | //TableTools Asset if needed 54 | if (isset($clientOptions["tableTools"]) || (isset($clientOptions["dom"]) && strpos($clientOptions["dom"], 'T')>=0)){ 55 | $tableTools = DataTablesTableToolsAsset::register($view); 56 | //SWF copy and download path overwrite 57 | $clientOptions["tableTools"]["sSwfPath"] = $tableTools->baseUrl."/swf/copy_csv_xls_pdf.swf"; 58 | } 59 | $options = Json::encode($clientOptions); 60 | $view->registerJs("jQuery('#$id').DataTable($options);"); 61 | 62 | //base list view run 63 | if ($this->showOnEmpty || $this->dataProvider->getCount() > 0) { 64 | $content = preg_replace_callback("/{\\w+}/", function ($matches) { 65 | $content = $this->renderSection($matches[0]); 66 | 67 | return $content === false ? $matches[0] : $content; 68 | }, $this->layout); 69 | } else { 70 | $content = $this->renderEmpty(); 71 | } 72 | $tag = ArrayHelper::remove($this->options, 'tag', 'div'); 73 | echo Html::tag($tag, $content, $this->options); 74 | } 75 | 76 | /** 77 | * Initializes the datatables widget disabling some GridView options like 78 | * search, sort and pagination and using DataTables JS functionalities 79 | * instead. 80 | */ 81 | public function init() 82 | { 83 | parent::init(); 84 | 85 | //disable filter model by grid view 86 | $this->filterModel = null; 87 | 88 | //disable sort by grid view 89 | $this->dataProvider->sort = false; 90 | 91 | //disable pagination by grid view 92 | $this->dataProvider->pagination = false; 93 | 94 | //layout showing only items 95 | $this->layout = "{items}"; 96 | 97 | //the table id must be set 98 | if (!isset($this->tableOptions['id'])) { 99 | $this->tableOptions['id'] = 'datatables_'.$this->getId(); 100 | } 101 | } 102 | /** 103 | * Returns the options for the datatables view JS widget. 104 | * @return array the options 105 | */ 106 | protected function getClientOptions() 107 | { 108 | return $this->clientOptions; 109 | } 110 | 111 | public function renderTableBody() 112 | { 113 | $models = array_values($this->dataProvider->getModels()); 114 | if (count($models) === 0) { 115 | return "\n"; 116 | } else { 117 | return parent::renderTableBody(); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /DataTablesAsset.php: -------------------------------------------------------------------------------- 1 | 5 | * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) 6 | * @package yii2-widget-datatables 7 | */ 8 | namespace fedemotta\datatables; 9 | use yii\web\AssetBundle; 10 | 11 | /** 12 | * Asset for the DataTables JQuery plugin 13 | * @author Federico Nicolás Motta 14 | */ 15 | class DataTablesAsset extends AssetBundle 16 | { 17 | public $sourcePath = '@bower/datatables'; 18 | 19 | public $css = [ 20 | "media/css/jquery.dataTables.css", 21 | ]; 22 | 23 | public $js = [ 24 | "media/js/jquery.dataTables.js", 25 | ]; 26 | 27 | public $depends = [ 28 | 'yii\web\JqueryAsset', 29 | ]; 30 | } -------------------------------------------------------------------------------- /DataTablesBootstrapAsset.php: -------------------------------------------------------------------------------- 1 | 5 | * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) 6 | * @package yii2-widget-datatables 7 | */ 8 | namespace fedemotta\datatables; 9 | use yii\web\AssetBundle; 10 | 11 | /** 12 | * Asset for the DataTables Bootstrap JQuery plugin 13 | * @author Federico Nicolás Motta 14 | */ 15 | class DataTablesBootstrapAsset extends AssetBundle 16 | { 17 | public $sourcePath = '@bower/datatables-bootstrap3'; 18 | 19 | public $css = [ 20 | "BS3/assets/css/datatables.css", 21 | ]; 22 | 23 | public $js = [ 24 | "BS3/assets/js/datatables.js", 25 | ]; 26 | 27 | public $depends = [ 28 | 'yii\web\JqueryAsset', 29 | 'fedemotta\datatables\DataTablesAsset', 30 | ]; 31 | } -------------------------------------------------------------------------------- /DataTablesTableToolsAsset.php: -------------------------------------------------------------------------------- 1 | 5 | * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) 6 | * @package yii2-widget-datatables 7 | */ 8 | namespace fedemotta\datatables; 9 | use yii\web\AssetBundle; 10 | 11 | /** 12 | * Asset for the DataTables TableTools JQuery plugin 13 | * @author Federico Nicolás Motta 14 | */ 15 | class DataTablesTableToolsAsset extends AssetBundle 16 | { 17 | public $sourcePath = '@bower/datatables-tabletools'; 18 | 19 | public $css = [ 20 | "css/dataTables.tableTools.css", 21 | ]; 22 | 23 | public $js = [ 24 | "js/dataTables.tableTools.js", 25 | ]; 26 | 27 | public $depends = [ 28 | 'yii\web\JqueryAsset', 29 | 'fedemotta\datatables\DataTablesAsset', 30 | ]; 31 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | DataTables widget for Yii2 2 | =========================== 3 | This extension provides the [DataTables](https://github.com/DataTables/DataTables) integration for the Yii2 framework. 4 | 5 | [![Latest Stable Version](https://poser.pugx.org/fedemotta/yii2-widget-datatables/v/stable)](https://packagist.org/packages/fedemotta/yii2-widget-datatables) [![Total Downloads](https://poser.pugx.org/fedemotta/yii2-widget-datatables/downloads)](https://packagist.org/packages/fedemotta/yii2-widget-datatables) [![Latest Unstable Version](https://poser.pugx.org/fedemotta/yii2-widget-datatables/v/unstable)](https://packagist.org/packages/fedemotta/yii2-widget-datatables) [![License](https://poser.pugx.org/fedemotta/yii2-widget-datatables/license)](https://packagist.org/packages/fedemotta/yii2-widget-datatables) 6 | 7 | Installation 8 | ------------ 9 | 10 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 11 | 12 | With Composer installed, you can then install the extension using the following commands: 13 | 14 | composer global require "fxp/composer-asset-plugin:~1.0.0" 15 | composer require --prefer-dist fedemotta/yii2-widget-datatables "*" 16 | 17 | The first command installs the [composer asset plugin](https://github.com/francoispluchino/composer-asset-plugin/) 18 | which allows managing bower and npm package dependencies through Composer. You only need to run this command 19 | once for all. The second command installs the datatables widget. 20 | 21 | You can also add (instead of the second command): 22 | 23 | ``` 24 | "fedemotta/yii2-widget-datatables": "*" 25 | ``` 26 | 27 | to the require section of your `composer.json` file. 28 | 29 | Usage 30 | ----- 31 | Use DataTables as any other other Yii2 widget. 32 | 33 | ```php 34 | use fedemotta\datatables\DataTables; 35 | ``` 36 | 37 | ```php 38 | search(Yii::$app->request->queryParams); 41 | ?> 42 | $dataProvider, 44 | 'filterModel' => $searchModel, 45 | 'columns' => [ 46 | ['class' => 'yii\grid\SerialColumn'], 47 | 48 | //columns 49 | 50 | ['class' => 'yii\grid\ActionColumn'], 51 | ], 52 | ]);?> 53 | ``` 54 | This extension uses the Bootstrap integration plugin to provide a Yii2 style by default. 55 | 56 | The TableTools plugin is also available. Specify the DOM and the tableTools settings in the clientOptions array as the following example. 57 | 58 | ```php 59 | ... 60 | 'clientOptions' => [ 61 | "lengthMenu"=> [[20,-1], [20,Yii::t('app',"All")]], 62 | "info"=>false, 63 | "responsive"=>true, 64 | "dom"=> 'lfTrtip', 65 | "tableTools"=>[ 66 | "aButtons"=> [ 67 | [ 68 | "sExtends"=> "copy", 69 | "sButtonText"=> Yii::t('app',"Copy to clipboard") 70 | ],[ 71 | "sExtends"=> "csv", 72 | "sButtonText"=> Yii::t('app',"Save to CSV") 73 | ],[ 74 | "sExtends"=> "xls", 75 | "oSelectorOpts"=> ["page"=> 'current'] 76 | ],[ 77 | "sExtends"=> "pdf", 78 | "sButtonText"=> Yii::t('app',"Save to PDF") 79 | ],[ 80 | "sExtends"=> "print", 81 | "sButtonText"=> Yii::t('app',"Print") 82 | ], 83 | ] 84 | ] 85 | ], 86 | ... 87 | ``` 88 | 89 | 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. 90 | 91 | ```php 92 | public $depends = [ 93 | ... 94 | 'fedemotta\datatables\DataTablesAsset', 95 | ... 96 | ]; 97 | ``` 98 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fedemotta/yii2-widget-datatables", 3 | "description": "DataTables widget for Yii2", 4 | "type": "yii2-extension", 5 | "minimum-stability": "stable", 6 | "keywords": ["yii2","extension","jquery","datatables","table","javascript","library"], 7 | "license": "MIT", 8 | "support": { 9 | "issues": "https://github.com/fedemotta/yii2-widget-datatables/issues?state=open", 10 | "source": "https://github.com/fedemotta/yii2-widget-datatables" 11 | }, 12 | "authors": [ 13 | { 14 | "name": "Federico Nicolás Motta", 15 | "email": "fedemotta@gmail.com" 16 | } 17 | ], 18 | "require": { 19 | "yiisoft/yii2":"*", 20 | "bower-asset/jquery":">= 1.7.0", 21 | "bower-asset/datatables":">= 1.9.4", 22 | "bower-asset/datatables-bootstrap3":"*", 23 | "bower-asset/datatables-tabletools":"*" 24 | }, 25 | "autoload": { 26 | "psr-4": { 27 | "fedemotta\\datatables\\": "" 28 | } 29 | }, 30 | "extra": { 31 | "asset-installer-paths": { 32 | "bower-asset-library": "vendor/bower" 33 | } 34 | } 35 | } 36 | --------------------------------------------------------------------------------