yetopen/yii2-models-sorting

对活动记录模型进行排序(网格、表单、代码)

安装次数: 18

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

类型:yii2-extension

2.0.1 2019-03-14 11:10 UTC

This package is auto-updated.

Last update: 2024-09-17 20:22:52 UTC


README

通过字段对活动记录模型(网格、表单、代码)进行排序。自动更新排序索引。拖放等。

安装

安装此扩展的首选方式是通过 Composer

运行以下命令之一:

php composer.phar require --prefer-dist matthew-p/yii2-models-sorting "@dev"

或者

"matthew-p/yii2-models-sorting": "@dev"

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

用法

扩展安装后,只需在代码中简单使用即可:

如果需要,向 gridview 添加排序列(拖放到 "tr" 中的第一个 "td" 上)

GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel'  => $searchModel,
    'columns'      => [
        ['class' => 'yii\grid\SerialColumn'],

        'id',
        'title',
        // Sort column
        [
            'class'      => \MP\ARSorting\ARSortColumn::class,
            'attribute'  => 'sort',
            'hideColumn' => false,
        ],
        [
            'attribute' => 'created_at',
            'format'    => ['date', 'format' => 'php: d/m/Y H:i:s'],
        ],

        ['class' => 'yii\grid\ActionColumn'],
    ],
]);

在控制器中添加操作(仅适用于 gridview 排序)

class SampleController extends Controller
{
...
    public function actions(): array
    {
        return array_merge(parent::actions(), [
            'mp-ar-sort' => \MP\ARSorting\ARSortAction::class,
        ]);
    }
...
}

将行为添加到 AR 模型

class SampleModel extends ActiveRecord
{
    ...
    /**
     * @inheritdoc
     */
    public function behaviors(): array
    {
        return [
            [
                'class'          => \MP\ARSorting\ARSortBehavior::class,
                'attribute'      => 'sort',
                // Optional
                'queryCondition' => function ($query, self $model) {
                    $query->andWhere(['parent_id' => $model->parent_id]);
                },
            ],
        ];
    }
    ...
}

在 params.php 中定义加密密钥

'MPARSort' => [
    'encryptionKey' => 'RandomKey',
],

or 

'MPComponents' => [
    'encryptionKey' => 'RandomKey',
],

就这么多。检查一下。