jlorente / yii2-datatables
为Yii2框架提供JQuery DataTables插件的扩展
1.0.1
2022-11-08 19:15 UTC
Requires
- bower-asset/datatables: >= 1.9.4
- bower-asset/datatables-bootstrap3: *
- bower-asset/datatables-tabletools: *
This package is auto-updated.
Last update: 2024-09-08 23:31:41 UTC
README
此扩展受到 yii2-widget-datatables 的启发,作者是 fedemotta。
此扩展为Yii2框架提供DataTables集成。
安装
推荐通过 Composer 安装此扩展。
安装Composer后,可以使用以下命令安装扩展
$ php composer.phar require jlorente/yii2-datatables "*"
或者添加
... "require": { // ... other configurations ... "jlorente/yii2-datatables": "*" }
到您的 composer.json
文件的 require
部分。
使用方法
将DataTables作为任何其他Yii2小部件使用。
use jlorente\datatables\grid\DataTables;
$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设置,如下所示示例。
DataTables::widget([ //Other configurations '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 = [ ... 'jlorente\datatables\assets\DataTablesAsset', ... ];