matthew-p/yii2-models-sorting

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

该软件包的官方仓库似乎已消失,因此该软件包已被冻结。

安装: 602

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 1

开放性问题: 0

类型:yii2-extension

3.0.0 2020-09-21 11:38 UTC

This package is auto-updated.

Last update: 2023-07-21 18:33:49 UTC


README

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

安装

通过composer安装此扩展是首选方式。

运行以下命令之一

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

或者添加以下依赖项到您的composer.json文件的require部分,仅当您将使用\MP\ARSorting\ARSortColumn类时

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

和以下依赖项

"yiisoft/yii2-jui": "@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',
],

这样就完成了。检查一下。