richardfan1126 / yii2-sortable-gridview
为 Yii2 的可排序 GridView
v0.0.8
2020-07-25 14:19 UTC
Requires
- yiisoft/yii2: *
- yiisoft/yii2-bootstrap: ~2.0.0
- yiisoft/yii2-jui: *
README
这是 Yii2 GridView 的一个扩展。
此扩展可以渲染一个可排序的 GridView,你可以拖放列表中的记录项,并在 ActiveRecord 中存储排序顺序。
入门指南
安装
使用 Composer 安装
composer require richardfan1126/yii2-sortable-gridview "*"
或
php composer.phar require richardfan1126/yii2-sortable-gridview "*"
或添加
"richardfan1126/yii2-sortable-gridview":"*"
到你的 composer.json 文件的要求部分。
设置 SortableAction
在你的控制器中,将 SortableAction 添加到 action() 中
use richardfan\sortable\SortableAction; public function actions(){ return [ 'sortItem' => [ 'class' => SortableAction::className(), 'activeRecordClassName' => YourActiveRecordClass::className(), 'orderColumn' => 'name_of_field_storing_ordering', 'startPosition' => 1, // optional, default is 0 ], // your other actions ]; }
设置 SortableGridView
在视图文件中,使用 SortableGridView 如使用 Yii 默认的 GridView
use richardfan\sortable\SortableGridView; <?= SortableGridView::widget([ 'dataProvider' => $dataProvider, // you can choose how the URL look like, // but it must match the one you put in the array of controller's action() 'sortUrl' => Url::to(['sortItem']), 'columns' => [ // Data Columns ], ]); ?>
你可能还想禁用数据提供者的分页,以便允许跨页重新排序。
你可以在传递数据提供者到 SortableGridView 之前调用以下操作来做到这一点
$dataProvider->pagination = false;
配置
SortableAction
示例
use richardfan\sortable\SortableAction; public function actions(){ return [ 'sortItem' => [ 'class' => SortableAction::className(), 'activeRecordClassName' => Articles::className(), 'orderColumn' => 'sortOrder', ], // your other actions ]; }
- activeRecordClassName (必需) ActiveRecrod 类名称。使用带命名空间的完整类名。
- orderColumn (必需) 存储每个记录排序顺序的列名称。该列应该是整数。
SortableGridView
示例
use richardfan\sortable\SortableGridView; <?= SortableGridView::widget([ 'dataProvider' => $dataProvider, // SortableGridView Configurations 'sortUrl' => Url::to(['sortItem']), 'sortingPromptText' => 'Loading...', 'failText' => 'Fail to sort', 'moveItem' => '.moveItem', 'columns' => [ [ 'content' => function(){ return "<span class='glyphicon glyphicon-resize-vertical'></span>"; }, 'contentOptions' => ['style'=>'cursor:move;', 'class' => 'moveItem'], ], // Data Columns ], ]); ?>
- sortUrl (必需) 控制器操作中定义的 SortableAction 的 URL 链接。
- sortingPromptText (可选) 当服务器重新排序模型时在模型中显示的文本。您可以使用 HTML 标签在此属性中。默认为 "Loading..."。
- failText (可选) 排序失败时在警告框中显示的文本。默认为 "排序失败"。
许可证
yii2-sortable-gridview 在 MIT 许可证下发布。有关详细信息,请参阅捆绑的 LICENSE 文件。