sem-soft/yii2-sortable
Composer 安装命令:
composer require sem-soft/yii2-sortable
包简介
Full sortable pack for managing entities ordering by specific attribute and grouping filter
README 文档
README
Full sortable pack for managing entities ordering by specific attribute and grouping filter
Usage
Next, an example of using the entire Sortable package
Tables migration
- Table with simple one filed order column 'sort'
$this->createTable('{{%banner}}', [ 'id' => $this->primaryKey(), 'is_active' => $this->boolean()->notNull()->defaultValue(1)->comment('Активность записи'), 'sort' => $this->sort()->decimal(65, 13)->notNull()->unique()->comment('Порядок'), 'url' => $this->string()->comment('Ссылка для перехода'), ... 'created_at' => $this->integer()->comment('Дата создания'), ... ]);
- Table with complex sorting group fields 'sort' and 'document_group_id'
$this->createTable('{{%document}}', [ 'id' => $this->primaryKey(), ... 'document_group_id' => $this->integer()->comment('Категория документа'), ... 'sort' => $this->decimal(65,13)->notNull()->comment('Порядок'), ... 'url' => $this->string()->comment('Ссылка'), 'published_at' => $this->integer()->notNull()->comment('Дата публикации'), ... ]); $this->createIndex('idx-document-document_group_id-sort', '{{%document}}', ['document_group_id', 'sort'], true);
Models configuration
Include simple rule for 'sort' field and special behavior setting.
- Model with simple ordering by 'sort' field
<?php ... namespace common\modules\banner\models; ... use Yii; use sem\sortable\behaviors\SortAttributeBehavior; ... class Banner extends ActiveRecord { ... public function rules() { return [ ... [['sort'], 'number'], ... ]; } ... public function behaviors() { return [ ... SortAttributeBehavior::class, ... ]; } }
- Model with complex grouping ordering by two fields: 'document_group_id' and 'sort'
<?php ... namespace common\modules\document\models; ... use Yii; use sem\sortable\behaviors\SortAttributeBehavior; ... class Document extends ActiveRecord { ... public function rules() { return [ ... [['sort'], 'number'], ... ]; } ... public function behaviors() { return [ ... [ 'class' => SortAttributeBehavior::class, 'filter' => function (Query $query, Document $model) { if ($model->document_group_id) { $query->andWhere([ 'document_group_id' => $model->document_group_id ]); } }, ], ... ]; } ... }
Controller special actions configuration
Their is several special actions for reordering entities. Configuration of this actions may be done in controller actions() method. Don't forgot about RBAC and filtering permissions for this actions
- Step based actions. These actions allow move entity upstair and downstair by one step only. The example below shows the configuration of two actions with a complex grouping anonymous sort function.
<?php ... namespace common\modules\document\controllers; ... use sem\sortable\enums\MoveDirection; use Yii; ... class DocumentController extends Controller { ... public function actions() { $sortFilter = function (Query $query, Document $model) { if ($model->document_group_id) { $query->andWhere([ 'document_group_id' => $model->document_group_id ]); } }; return [ ... 'up' => [ 'class' => 'sem\sortable\actions\StepMoveAction', 'modelClass' => Document::class, 'direction' => MoveDirection::DOWN, 'filter' => $sortFilter ], 'down' => [ 'class' => 'sem\sortable\actions\StepMoveAction', 'modelClass' => Document::class, 'direction' => MoveDirection::UP, 'filter' => $sortFilter ], ... ]; } }
- Drag and drop action. This action allow move entity in randomly place in list. Example below.
<?php ... namespace common\modules\privates\bsnner\controllers; ... use sem\sortable\actions\DragDropMoveAction; use Yii; ... class SliderController extends Controller { ... public function actions() { return [ ... 'swap' => [ 'class' => DragDropMoveAction::class, 'modelClass' => Banner::class, ], ... ]; } }
Widgets
All widgets adapted to work with Pjax tech.
-
GridView widget with Step-based ordering buttons.
<?php \yii\widgets\Pjax::begin();?> <?= \yii\grid\GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => [ ... [ 'class' => 'yii\grid\ActionColumn', 'template' => '{up} {down} {update} {delete}', 'buttons' => [ 'up' => function ($url, Document $model) { return Html::a('<span class="fa fa-arrow-up"></span>', ['up', 'id' => $model->id], [ 'data' => [ 'pjax' => 1, 'method' => 'post', ], ]); }, 'down' => function ($url, Document $model) { return Html::a('<span class="fa fa-arrow-down"></span>', ['down', 'id' => $model->id], [ 'data' => [ 'pjax' => 1, 'method' => 'post' ], ]); } ], ], ], ]); ?> <?php \yii\widgets\Pjax::end();?>
When Primary key of sortable entity is composite the ActionColumn buttons url configuration look like the following:
... 'up' => function ($url, Document $model) { return Html::a('<span class="fa fa-arrow-up"></span>', array_merge( ['up'], $model->getPrimaryKey(true) ), [ 'data' => [ 'pjax' => 1, 'method' => 'post' ] ] ); }, ... -
SortableGridView widget. Allows Drag and Drop selected entity over his table list. Also support Pjax. Widget detects Pjax sorround automaticly. Now, this widget supports only GET-method. In the future, POST-method support will be added.
<?php \yii\widgets\Pjax::begin();?> <?= \sem\sortable\widgets\gridview\SortableGridView::widget([ 'dataProvider' => $dataProvider, 'sortActionRoute' => ['swap'], 'columns' => [ ... [ 'class' => 'yii\grid\ActionColumn', 'template' => '{update} {delete}', ], ... ], ]); ?> <?php \yii\widgets\Pjax::end();?>
sem-soft/yii2-sortable 适用场景与选型建议
sem-soft/yii2-sortable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.28k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sem-soft/yii2-sortable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sem-soft/yii2-sortable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2018-08-31