fedemotta / yii2-widget-datatables
Yii2的DataTables小部件
v1.3
2015-06-08 16:07 UTC
Requires
- bower-asset/datatables: >= 1.9.4
- bower-asset/datatables-bootstrap3: *
- bower-asset/datatables-tabletools: *
- bower-asset/jquery: >= 1.7.0
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-14 17:21:29 UTC
README
本扩展为Yii2框架提供了DataTables集成。
安装
安装此扩展的首选方式是通过Composer。
安装Composer后,您可以使用以下命令安装扩展:
composer global require "fxp/composer-asset-plugin:~1.0.0"
composer require --prefer-dist fedemotta/yii2-widget-datatables "*"
第一条命令安装Composer资产插件,它允许通过Composer管理bower和npm包依赖。您只需运行此命令一次即可。第二条命令安装datatables小部件。
您也可以将以下内容添加到composer.json
文件的require部分(代替第二条命令):
"fedemotta/yii2-widget-datatables": "*"
使用方法
可以将DataTables作为任何其他Yii2小部件使用。
use fedemotta\datatables\DataTables;
<?php $searchModel = new ModelSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); ?> <?= DataTables::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], //columns ['class' => 'yii\grid\ActionColumn'], ], ]);?>
此扩展使用Bootstrap集成插件,默认提供Yii2风格。
TableTools插件也可用。在clientOptions数组中指定DOM和tableTools设置,如下例所示。
... '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") ], ] ] ], ...
您还可以在应用程序的JavaScript层中使用DataTables。为此,您需要将DataTables作为Asset文件的依赖项包括。在这种情况下,您可以使用yii\grid\GridView或使用datatables选项retrieve => true以避免错误。在两种情况下,所有选项都必须在JavaScript对象中。
public $depends = [ ... 'fedemotta\datatables\DataTablesAsset', ... ];