Git Product home page Git Product logo

yii2-sortable-behavior's Introduction

Yii2 sortable behavior for ActiveRecord

Installation

add co composer.json:

"repositories": [
	...
	{"type": "vcs", "url": "https://github.com/sashsvamir/yii2-sortable-behavior"}
],

Either run

php composer.phar require --prefer-dist sashsvamir/yii2-sortable-behavior "*@dev"

Requirements

To perform sorting your model with this behavior, model should have sort attribute (or any name you want), so you must provide existing model attribute with int type to store sorting index of model.

SortableBehavior

This behavior allow you to add sort logic with ActiveRecord behavior.

Usage

Attach the behavior to your model class:

use sashsvamir\sortableBehavior\SortableBehavior;

public function behaviors()
{
    return [
        [
            'class' => SortableBehavior::className(),
            'attribute' => 'sort', // model attribute with sorting index
        ],
        // OR (will be used 'sort' attribute by default)
        SortableBehavior::className()
    ];
}

Now your model store sorting index, which created in save/update model event or when you sorting your model in GridView (see below).

SortableColumn and SortableAction

This allow you to perform sorting rows in GridView table with saving new order of items by ajax.

Usage

To perform sorting in GridView add to widget follow column:

GridView::widget([
	'dataProvider' => $dataProvider,
    'tableOptions' => ['class' => 'table'],
    'columns' => [
        ...
	    [
	        'class' => SortableColumn::className(),
	        'attribute' => 'sort',
	        'action' => \yii\helpers\Url::to('/site/slider/sort'), // route to controller with SortableAction
	    ],
    ]
]);

Also add SortableAction to controller (see in example above grid-view column action param):

public function actions()
{
    return [
        'sort' => [
            'class' => SortableAction::className(),
            'parent' => Slider::className(), // class name of sortable model
        ],
    ];
}

Additional

Also you probably want to add follow sorting for your model:

in backend ModelSearch:

$dataProvider = new ActiveDataProvider([
	'query' => $query,
	'sort' => [
		'defaultOrder' => ['sort' => SORT_ASC],
		'attributes' => ['sort'], // allow sorting by [[sort]] attribute only
	],
]);

in frontend ModelQuery:

public function init()
{
    $modelClass = $this->modelClass;
	$this->orderBy([$modelClass::tableName() . '.sort' => SORT_ASC]);
	parent::init();
}

yii2-sortable-behavior's People

Contributors

sashsvamir avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.