vandzaxe / yii2-sortable-gridview
适用于 Yii2 的可排序 GridView
v0.0.7
2019-07-06 02:18 UTC
Requires
- yiisoft/yii2: *
- yiisoft/yii2-bootstrap: ~2.0.0
- yiisoft/yii2-jui: *
README
这是 Yii2 GridView 的一个扩展。
此扩展可以渲染可排序的 GridView,您可以从列表中拖放记录项,并在 ActiveRecord 中存储排序顺序。
入门指南
安装
使用 Composer 安装
composer require vandzaxe/yii2-sortable-gridview "*"
或
php composer.phar require vandzaxe/yii2-sortable-gridview "*"
或添加
"vandzaxe/yii2-sortable-gridview":"*"
到您的 composer.json 文件的 require 部分。
设置 SortableAction
在您的控制器中,将 SortableAction 添加到 action() 中
use vandzaxe\sortable\SortableAction; public function actions(){ return [ 'sortItem' => [ 'class' => SortableAction::className(), 'activeRecordClassName' => YourActiveRecordClass::className(), 'orderColumn' => 'name_of_field_storing_ordering', ], // your other actions ]; }
设置 SortableGridView
在视图文件中,使用 SortableGridView,就像使用 Yii 默认的 GridView 一样
use vandzaxe\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']), // The starting number of the sort. Default sort starts at 0. 'startNumber' => 0, // The incremental number of the sort. Default increment is 1. 'incrementalNumber' => 1, 'columns' => [ // Data Columns ], ]); ?>
您可能还想禁用数据提供程序的分页,以便允许跨页面重新排序。
您可以通过调用以下代码在将数据提供者传递到 SortableGridView 之前完成此操作
$dataProvider->pagination = false;
配置
SortableAction
示例
use vandzaxe\sortable\SortableAction; public function actions(){ return [ 'sortItem' => [ 'class' => SortableAction::className(), 'activeRecordClassName' => Articles::className(), 'orderColumn' => 'sortOrder', ], // your other actions ]; }
- activeRecordClassName (必需) ActiveRecrod 类名称。使用带有命名空间的完整类名称。
- orderColumn (必需) 存储每个记录排序顺序的列名称。该列应为整数。
SortableGridView
示例
use vandzaxe\sortable\SortableGridView; <?= SortableGridView::widget([ 'dataProvider' => $dataProvider, // SortableGridView Configurations 'sortUrl' => Url::to(['sortItem']), 'startNumber' => 0, 'incrementalNumber' => 1, 'sortingPromptText' => 'Loading...', 'failText' => 'Fail to sort', 'columns' => [ // Data Columns ], ]); ?>
- sortUrl (必需) 控制器动作中定义的 SortableAction 的 URL 链接。
- startNumber (可选) 排序的起始数字。默认为 0。
- incrementalNumber (可选) 排序的增量数字。默认为 1。
- sortingPromptText (可选) 服务器重新排序模型时在模型中显示的文本。您可以使用 HTML 标签在此属性中。默认为 "Loading..."。
- failText (可选) 排序失败时在警告框中显示的文本。默认为 "Fail to sort"。
许可证
yii2-sortable-gridview 在 MIT 许可证下发布。有关详细信息,请参阅捆绑的 LICENSE 文件。