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?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?