juozasl/yii2-sortable-gridview

适用于 Yii2 的可排序 GridView

安装: 187

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 19

类型:yii2-extension

v0.0.9 2020-05-07 18:48 UTC

This package is not auto-updated.

Last update: 2024-09-21 14:38:02 UTC


README

Latest Stable Version Total Downloads GitHub stars GitHub issues

这是 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

在视图文件中,像使用 Yii 默认的 GridView 一样使用 SortableGridView

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',
    
    'columns' => [
        // Data Columns
    ],
]); ?>
  • sortUrl (必需) 控制器操作中定义的 SortableAction 的 URL 链接。
  • sortingPromptText (可选) 服务器重新排序模型时显示在模型中的文本。您可以使用 HTML 标签在此属性中。默认为 "加载..."。
  • failText (可选) 排序失败时在警报框中显示的文本。默认为 "排序失败"。

许可证

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