mix8872/yii2-sortable-gridview

为Yii2的排序GridView

安装: 8

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 19

类型:yii2-extension

v0.0.6 2018-08-29 06:16 UTC

This package is not auto-updated.

Last update: 2024-09-20 11:56:19 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文件的require部分。

设置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

在视图文件中,像使用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 (必需)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。