serj/sortable

用于管理项目排序(排序)的Yii2组件

安装: 38

依赖: 1

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

公开问题: 0

类型:yii2-extension

v1.2.2 2019-04-14 07:03 UTC

This package is auto-updated.

Last update: 2024-09-28 16:10:19 UTC


README

安装

要将组件导入您的项目,请将以下行添加到您的composer.json文件的require部分

"serj/sortable": "~1.2.0"

或者运行以下命令

$ composer require serj/sortable "~1.2.0"

配置

假设有一个如下结构的表

cartoons

 id |        title        | category_id | sort_local | sort_general | archived | color
----+---------------------+-------------+------------+--------------+----------+-------
  1 | Fiddlesticks        |          14 |       1000 |         7000 | t        | t
  2 | Trolley Troubles,   |          14 |       2000 |         8000 | f        | f
  3 | Fantasmagorie       |          14 |       3000 |         9000 | t        | f
  4 | Winnie the pooh     |          15 |       3000 |         3000 | f        | t
  5 | Kolobok (The loaf)  |          15 |       1000 |         2000 | f        | t
  6 | Hedgehog in the fog |          15 |       2000 |         1000 | f        | t
  7 | South Park          |          16 |       1000 |         4000 | f        | t
  8 | Futurama            |          16 |       2000 |         5000 | f        | t
  9 | Rick and Morty      |          16 |       3000 |         6000 | f        | t

当您想以排序顺序查询项目时,必须假设排序值较小的项目排在前面(升序)。

通过应用程序配置初始化组件,使用最少的必需设置

'components' => [
    //...
    'sortableCartoons' => [
        'class' => 'serj\sortable\Sortable',
        'targetTable' => 'cartoons',
        'srtColumn' => 'sort_inner'
    ]
]

让我们看看更有趣的场景。我们的表有2列用于维护项目顺序。 sort_inner - 用于分类内的排序,sort_general - 用于整个表的排序。

要维护这两列,我们需要两个组件实例,每个实例对应其各自的列。

'components' => [
    'sortInSingleCat' => [
        'class' => 'serj\sortable\Sortable',
        'targetTable' => 'cartoons',
        'grpColumn' => 'category_id',
        'pkColumn' => 'id',
        'srtColumn' => 'sort_inner',
        'skipRows' => [
            'archived' => true,
            'color' => false
        ]
    ],
    'sortThroughAllCat' => [
        'class' => 'serj\sortable\Sortable',
        'targetTable' => 'cartoons',
        'pkColumn' => 'id',
        'srtColumn' => 'sort_general',
        'skipRows' => [
            'archived' => true,
            'color' => false
        ]
    ]
]

或者如果您想直接使用而不配置

$sortThroughAllCat = new \serj\sortable\Sortable([
    'targetTable' => 'cartoons',
    'pkColumn' => 'id',
    'srtColumn' => 'sort_general',
    'skipRows' => [
        'archived' => true,
        'color' => false
    ]
]);

用法

要获取插入id:5之后的项目排序值

$sortValLocal = \Yii::$app->sortInSingleCat->getSortVal(5, 'after', 15);
$sortValGeneral = \Yii::$app->sortThroughAllCat->getSortVal(5, 'after');

要获取插入id:5之前的项目排序值

$sortValLocal = \Yii::$app->sortInSingleCat->getSortVal(5, 'before', 15);
$sortValGeneral = \Yii::$app->sortThroughAllCat->getSortVal(5, 'before');

然后,如果您使用ActiveRecord,可以像这样插入新记录

(new Cartoon)->setAttributes([
    'title' => 'Some title',
    'category_id' => 15,
    'sort_local' => $sortValLocal,
    'sort_general' => $sortValGeneral
])->save();

要获取插入所有项目之前的项目排序值

// 15 is a category_id (srtColumn)
$sortValLocal = \Yii::$app->sortInSingleCat->getSortValBeforeAll(15);
$sortValGeneral = \Yii::$app->sortThroughAllCat->getSortValBeforeAll();

要获取在特定类别中插入所有项目之后的项目排序值

// 15 is a category_id (srtColumn)
$sortValLocal = \Yii::$app->sortableCartoons->getSortValAfterAll(15);
$sortValGeneral = \Yii::$app->sortThroughAllCat->getSortValAfterAll();

如果您创建了一个新的类别,例如 category_id:17,并且还没有项目

$sortValLocal = \Yii::$app->sortableCartoons->getIniSortVal();

//to insert to the end of the list in terms of the entire table
$sortValGeneral = \Yii::$app->sortThroughAllCat->getSortValAfterAll();

如果您表中有表示记录状态的列或列(例如,已删除、存档),这意味着您不再使用这些记录,或者您只是想忽略它们,您可以在配置中指定它为 skipRows。在这种情况下,这些是 archivedcolor

    'skipRows' => [
        'archived' => true,
        'color' => false
    ]

因此,所有具有 archived = truecolor = false 的元组将不被考虑。有一个陷阱:这些状态必须是持久性的,因此一旦设置,就必须不能回退。如果您来回切换,则不要使用此选项。

替代数据库连接

默认情况下,组件使用 \Yii::$app->db,如果您必须使用其他连接

$connection = new \yii\db\Connection($config)
\Yii::$app->sortableCartoons->setDb($connection);

或者在组件配置中设置它

'components' => [
    'anotherDb' => [
        'class' => 'yii\db\Connection',
        ...
    ],
    ...
    'sortInSingleCat' => [
        'class' => 'serj\sortable\Sortable',
        'dbComponentId' => 'anotherDb'
        ...
    ],
    ...
]

使用MySql数据库(默认是Postgres)

'components' => [
    'sortInSingleCat' => [
        'class' => 'serj\sortable\Sortable',
        'databaseDriver' => serj\sortable\Sortable::DB_DRIVER_MYSQL
        ...
    ],
    ...
]