taimurkhan911/yii2-sortable-gridview

为 Yii2 的可排序 GridView

v1.0.1 2023-10-09 11:11 UTC

This package is not auto-updated.

Last update: 2024-09-24 14:01:55 UTC


README

这是 Yii2 GridView 的扩展。

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

入门指南

安装

使用 Composer 安装

composer require taimurkhan911/yii2-sortable-gridview "*"

或者

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

或者添加

"taimurkhan911/yii2-sortable-gridview":"*"

到您的 composer.json 文件的 require 部分。

设置 SortableAction

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

use taimurkhan911\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 taimurkhan911\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 (可选) 排序失败时在警告框中显示的文本。默认为 "Fail to sort"。

许可证

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