PHP code example of claudejanz / yii2-nested-sortable

1. Go to this page and download the library: Download claudejanz/yii2-nested-sortable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

claudejanz / yii2-nested-sortable example snippets


$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');

/**
* @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');
}

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

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