承接 paulzi/yii2-sortable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

paulzi/yii2-sortable

最新稳定版本:v1.0.3

Composer 安装命令:

composer require paulzi/yii2-sortable

包简介

Sortable Behavior for Yii2

README 文档

README

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}.[[parent_id]]" => $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).

统计信息

  • 总下载量: 298.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 1
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 4
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-12-11