composer require rootlocal/yii2-sortable-grid-view
or added composer.json:
"rootlocal/yii2-sortable-grid-view": "~2.0"
<?php
namespace common\models;
use rootlocal\widgets\sortable\SortableGridBehavior;
class Book extends ActiveRecord
{
// ...
/**
* {@inheritDoc}
*/
public function behaviors(): array
{
return [
'sort' => ['class' => SortableGridBehavior::class],
// ...
];
}
}
<?php
namespace common\models;
use rootlocal\widgets\sortable\SortableGridQueryTrait;
use yii\db\ActiveQuery;
/**
* This is the ActiveQuery class for [[Book]].
*
* @see Book
* @mixin SortableGridQueryTrait
*/
class BookQuery extends ActiveQuery
{
use SortableGridQueryTrait;
// ...
}
/**
* {@inheritDoc}
*/
public function actions(): array
{
return [
'sort' => [
'class' => SortableGridAction::class,
'model' => Book::class,
]
];
}
<?php
$query = Book->find()->sortByOrder();
<?php
use common\models\BookSearch;
use rootlocal\widgets\sortable\SortableGridColumnWidget;
use yii\data\ActiveDataProvider;
use yii\grid\GridView;
use yii\web\View;
/**
* @var View $this
* @var BookSearch $searchModel
* @var ActiveDataProvider $dataProvider
*/
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'class' => SortableGridColumnWidget::class,
'sortableWidgetOptions' => ['sortValueSelector' => '.sort_order'],
],
// ...
[
'attribute' => 'sort_order',
'format' => 'raw',
'value' => fn(BookSearch $model) => Html::tag('span', $model->sort_order, [
'class' => 'sort_order',
]),
],
]]) ?>
<?php
use common\models\BookSearch;
use rootlocal\widgets\sortable\SortableGridColumnWidget;
use yii\data\ActiveDataProvider;
use yii\grid\GridView;
use yii\web\View;
use yii\helpers\Url;
/**
* @var View $this
* @var BookSearch $searchModel
* @var ActiveDataProvider $dataProvider
*/
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'class' => SortableGridColumnWidget::class,
'template' => '{sort} {test}',
'sortableWidgetOptions' => [
// Sort action
'sortableAction' => Url::to(['/book/sort'])
],
'buttons' => [
'test' => fn(BookSearch $model, $key) => '<i class="fa fa-address-book"></i>',
],
'visibleButtons' => [
'test' => fn(BookSearch $model) => $model->status === $model::STATUS_TEST10,
],
],
// ...
]]) ?>