morning-bird/yii2-sortable-gridview

适用于 Yii2 的可排序 GridView

安装: 405

依赖: 0

建议者: 0

安全: 0

星星: 0

观察者: 0

分支: 19

类型:yii2-extension

v1.0.3 2020-01-29 05:31 UTC

This package is auto-updated.

Last update: 2024-09-29 05:29:42 UTC


README

Latest Stable Version Total Downloads GitHub stars GitHub issues

这是 Yii2 GridView 的一个扩展。

这个扩展可以渲染一个可排序的 GridView,你可以拖动列表中的记录项,并将顺序存储在 ActiveRecord 中。

开始使用

安装

使用 Composer 安装

composer require morning-bird/yii2-sortable-gridview "*"

或者

php composer.phar require morning-bird/yii2-sortable-gridview "*"

或者将以下内容添加到你的 composer.json 文件中的 require 部分。

"morning-bird/yii2-sortable-gridview":"*"

设置 SortableAction

在你的控制器中,将 SortableAction 添加到 action() 中

use richardfan\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 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 (必需) ActiveRecord 类名称。使用带命名空间的完整类名称。
  • orderColumn (必需) 存储每个记录排序顺序的列名称。该列应为整数。

SortableGridView

示例

use richardfan\sortable\SortableGridView;

<?= SortableGridView::widget([
    'dataProvider' => $dataProvider,
    
    // SortableGridView Configurations
    'sortUrl' => Url::to(['sortItem']),
    'sortingPromptText' => 'Loading...',
    'failText' => 'Fail to sort',
    
    'columns' => [
        // Data Columns
    ],
]); ?>
  • sortUrl (必需) 控制器动作中定义的 SortableAction 的 URL 链接。
  • sortingPromptText (可选) 服务器重新排序模型时在模型中显示的文本。您可以使用 HTML 标签在此属性中使用。默认为 "Loading..."。
  • failText (可选) 排序失败时在警报框中显示的文本。默认为 "Fail to sort"。

许可

yii2-sortable-gridview 在 MIT 许可下发布。有关详细信息,请参阅附带 LICENSE 文件。