yii2-sortable

Yii2 Sortable Behavior

  • 所有者: paulzi/yii2-sortable
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

Yii2 Sortable Behavior

It implements the ability to control the order of the ActiveRecord.

Packagist Version
Code Coverage
Build Status
Total Downloads

Install

Install via Composer:

composer require paulzi/yii2-sortable

or add

"paulzi/yii2-sortable" : "^1.0"

to the require section of your composer.json file.

Migrations

Add signed integer column to your model.
For quick operation behavior, add index for scopes fields and sort attribute, example:

$this->createIndex('parent_sort', '{{%item}}', ['parent_id', 'sort']);

Configuring

use paulzi\sortable\SortableBehavior;

class Sample extends \yii\db\ActiveRecord
{
    public function behaviors() {
        return [
            [
                'class' => SortableBehavior::className(),
            ],
        ];
    }
}

Options

  • $query = null - list of attributes, callback or ActiveQuery, to find scope element. See below.
  • $sortAttribute = 'sort' - sort attribute in table schema.
  • $step = 100 - gap size between elements.
  • $joinMode = true - search method of unallocated value. When joinMode is true, using join table with self. Otherwise, use the search in the window. Window size defined by $windowSize property.
  • $windowSize = 1000 - defines the size of the search window, when joinMode is false.

Details of $query option:
The list of attributes - a simple way to scope elements with the same content fields, the aliases do not need.
You MUST use tableName() alias in ActiveQuery, when you are using joinMode.

For example,

public function behaviors()
{
    return [
        [
            'class' => SortableBehavior::className(),
            'query' => ['parent_id'],
        ]
    ];
}

This is equivalent to:

public function behaviors()
{
    return [
        [
            'class' => SortableBehavior::className(),
            'query' => function ($model) {
                $tableName = $model->tableName();
                return $model->find()->andWhere(["{$tableName}." => $model->parent_id]);
            },
        ]
    ];
}

Usage

Getting sort attribute value:

$model = Sample::findOne(1);
$position = $model->getSortablePosition();

To move as the first item:

$model = new Sample(['parent_id' => 1]);
$model->moveFirst()->save(); // inserting new node

To move as the last item:

$model = Sample::findOne(1);
$model->moveLast()->save(); // move existing node

To move an item to a specific position:

$model = Sample::findOne(1);
$model->moveTo(-33, true)->save(); // move to position -33, and move existing items forward
$model = Sample::findOne(2);
$model->moveTo(4, false)->save(); // move to position 4, and move existing items backward

To move an item before another:
Note: If you need to change scope, do it manually

$model1 = new Sample(['parent_id' => 1]);
$model2 = Sample::findOne(2);
$model1->moveBefore($model2)->save(); // move $model1 before $model2

To move an item after another:
Note: If you need to change scope, do it manually

$model1 = Sample::findOne(1);
$model2 = Sample::findOne(2);
$model1->parent_id = $model2->parent_id;
$model1->moveAfter($model2)->save(); // move $model1 after $model2 with change scope

Reorder item with the neighboring elements:

$model = Sample::findOne(1);
$model->reorder(true); // reorder with center zero
$model = Sample::findOne(2);
$model->reorder(false); // reorder from zero

SortableTrait

You can use optional SortableTrait, if you need it (for example, you are using something like ISortable interface).

主要指标

概览
名称与所有者paulzi/yii2-sortable
主编程语言PHP
编程语言PHP (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2015-12-11 16:03:40
推送于2022-01-10 13:32:53
最后一次提交2022-01-10 16:31:37
发布数4
最新版本名称v1.0.3 (发布于 )
第一版名称v1.0.0 (发布于 )
用户参与
星数15
关注者数4
派生数3
提交数11
已启用问题?
问题数3
打开的问题数2
拉请求数3
打开的拉请求数0
关闭的拉请求数0
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?