sharkom/yii2-nested-sortable-widget

an implementation of nestedSortable2.0

Maintainers

Package info

github.com/SharKom/yii2-nested-sortable-widget

Type:yii2-extension

pkg:composer/sharkom/yii2-nested-sortable-widget

Statistics

Installs: 176

Dependents: 0

Suggesters: 0

Stars: 0

v1.0.5 2023-09-29 16:48 UTC

This package is auto-updated.

Last update: 2026-02-27 08:14:31 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

an implementation of nestedSortable2.0

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist sharkom/yii2-nested-sortable "*"

or add

"sharkom/yii2-nested-sortable": "*"

to the require section of your composer.json file.

Prepare Model

In table migrateion:

$this->createTable('page', [
    'id'               => $this->primaryKey(),
    'title'            => $this->string(255)->notNull(),
    'parent_id'        => $this->integer()->null(),
    'weight'           => $this->integer(11)->notNull()->defaultValue(1),
]);

$this->createIndex('idx-page-parent_id', 'page', 'parent_id');
$this->addForeignKey('fk-page-parent_id-page-id', 'page', 'parent_id', 'page', 'id', 'SET NULL', 'CASCADE');

In ActiveRecord: for more details on Customizing Query Classes

/**
* @inheridoc
*/
public static function find()
{
    return (new PageQuery(get_called_class()))->orderBy('weight');
}

/**
* @return ActiveQuery
*/
public function getParent()
{
   return $this->hasOne(Page::className(), ['id' => 'parent_id']);
}

/**
* @return ActiveQuery
*/
public function getPages()
{
   return $this->hasMany(Page::className(), ['parent_id' => 'id'])->inverseOf('parent');
}

Usage

Once the extension is installed, simply use it in your code by :

In view:

use sharkom\yii2nestedSortable\NestedSortable;
echo NestedSortable::widget([
    'items'         => Page::find()->andWhere(['parent_id'=>null])->all(),
    'url'           => ['pages/save-sortable'],
    'contentAttribute' => 'title';
    'itemsAttribute' => 'pages';
]);

In controller:

public function actions()
{
    return [
        'save-sortable' => [
            'class' => 'sharkom\yii2nestedSortable\NestedSortableAction',
            //'scenario'=>'editable',  //optional
            'modelclass' => Page::className(),
        ],
    ];
}