PHP code example of sem-soft / yii2-sortable
1. Go to this page and download the library: Download sem-soft/yii2-sortable library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sem-soft / yii2-sortable example snippets
$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('Дата создания'),
...
]);
$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);
...
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,
...
];
}
}
...
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
]);
}
},
],
...
];
}
...
}
...
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
],
...
];
}
}
...
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,
],
...
];
}
}
\yii\widgets\Pjax::begin();
\yii\widgets\Pjax::begin();
...
'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'
]
]
);
},
...