2017 © Pedro Peláez
 

yii2-extension sort

Yii2 behavior for sorting models

image

demi/sort

Yii2 behavior for sorting models

  • Monday, January 29, 2018
  • by demi
  • Repository
  • 3 Watchers
  • 2 Stars
  • 398 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 3 Versions
  • 12 % Grown

The README.md

yii2-sort

Yii2 behavior to sort models, (*1)

Installation

Run, (*2)

composer require "demi/sort" "~1.0"

Configuration

In model file add sort behavior:, (*3)

public function behaviors()
{
    return [
        // ...
        'sortBehavior' => [
            'class' => 'demi\sort\SortBehavior',
            'sortConfig' => [
                'sortAttribute' => 'sort',
                'condition' => function ($query, $model) {
                        /* @var $query \yii\db\Query */
                        /* @var $model self */
                        $query->andWhere(['category_id' => $model->category_id]);
                    },
            ],
        ],
    ];
}

Usage

In GridView:, (*4)

<?= GridView::widget([
    // ...
    'columns' => [
        // ...
        [
            'class' => 'demi\sort\SortColumn',
            'action' => 'change-sort', // optional
        ],
    ],
]); ?>

Don't forget set default order! usually CategorySearch::search(), (*5)

$dataProvider = new ActiveDataProvider([
    'query' => $query,
    'sort' => ['defaultOrder' => ['sort' => SORT_ASC]],
]);

In view file:, (*6)

$canSortDown = $model->canSort(SORT_DESC);
$canSortUp = $model->canSort(SORT_ASC);

if ($canSortDown) {
    echo Html::a('Down', ['change-sort', 'id' => $model->id, 'direction' => SORT_DESC]);
}

if ($canSortUp) {
    echo Html::a('Up', ['change-sort', 'id' => $model->id, 'direction' => SORT_ASC]);
}

In conrtoller/model file:, (*7)

// sort model down
$model->changeSorting(SORT_DESC);

// sort model up
$model->changeSorting(SORT_DESC);

Bonus: Sort Action

Add this code to you controller:, (*8)

public function actions()
{
    return [
        'change-sort' => [
            'class' => 'demi\sort\SortAction',
            'modelClass' => \common\models\Category::className(),

            // optionaly
            'afterChange' => function ($model) {
                    if (!Yii::$app->request->isAjax) {
                        return Yii::$app->response->redirect(Url::to(['update', 'id' => $model->category_id]));
                    } else {
                        return Yii::$app->controller->renderPartial('index', ['model' => $model]);
                    }
                },
            // or
            'redirectUrl' => ['index'],
            // or
            'redirectUrl' => function ($model) {
                    return ['update', 'id' => $model->id];
                },

            'canSort' => Yii::$app->user->can('admin'),
            // or
            'canSort' => function ($model) {
                    return Yii::$app->user->id == $model->user_id;
                },
        ],
    ];
}

The Versions

29/01 2018

dev-master

9999999-dev https://github.com/demisang/yii2-sort#readme

Yii2 behavior for sorting models

  Sources   Download

GNU GPL-3.0-or-later

The Requires

 

yii2 behavior sort sorting

09/04 2017

1.0.1

1.0.1.0 https://github.com/demisang/yii2-sort#readme

Yii2 behavior for sorting models

  Sources   Download

GNU

The Requires

 

yii2 behavior sort sorting

02/07 2015

1.0.0

1.0.0.0 https://github.com/demisang/yii2-sort#readme

Yii2 behavior for sorting models

  Sources   Download

GNU

The Requires

 

yii2 behavior sort sorting