rootlocal/yii2-sortable-grid-view

Sortable Grid View

v2.0.6 2022-09-22 21:57 UTC

This package is auto-updated.

Last update: 2024-10-29 03:09:53 UTC


README

Latest Stable Version

install

composer require rootlocal/yii2-sortable-grid-view

or added composer.json:

    "rootlocal/yii2-sortable-grid-view": "~2.0"

Model Behavior

<?php

namespace common\models;

use rootlocal\widgets\sortable\SortableGridBehavior;

class Book extends ActiveRecord
{
// ...

    /**
     * {@inheritDoc}
     */
    public function behaviors(): array
    {
        return [
            'sort' => ['class' => SortableGridBehavior::class],
            // ...
        ];
    }
}

Query

<?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;
    
    // ...
}

Controller

     /**
     * {@inheritDoc}
     */
    public function actions(): array
    {
        return [
            'sort' => [
                'class' => SortableGridAction::class,
                'model' => Book::class,
            ]

        ];
    }

Sorting

<?php

$query = Book->find()->sortByOrder();

Example

<?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',
            ]),
        ],

    ]]) ?>

Extended config

<?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,
            ],
        ],
        
        // ...

    ]]) ?>