Include widget and behavior for edit Nested Set tree
The preferred way to install this extension is through composer.
Either run
composer require ale10257/yii2-ext-for-work-nested-set "dev-master"
or add
"ale10257/yii2-ext-for-work-nested-set": "dev-master"
to the require section of your composer.json file.
-
Generating a menu tree as a table (use Nested Set tree)
-
Drag & Drop rows table
-
Save result in DB
Example table before move
Move
After move
Only siblings elements can be dragged!!!
Model
Set up the model as here: https://github.com/creocoder/yii2-nested-sets and determine the constant SITE_ROOT_NAME and behaviors
<?php
use creocoder\nestedsets\NestedSetsBehavior;
use ale10257\ext\ChangeTreeBehavior;
//...
const SITE_ROOT_NAME = 'My_SITE_ROOT';
//...
public function behaviors()
{
return [
[
'class' => NestedSetsBehavior::className(),
'treeAttribute' => 'tree',
],
[
'class' => ChangeTreeBehavior::className(),
'rootSite' => SITE_ROOT_NAME
]
];
}
//...
?>View index.php for Category
//...
<?php if($data) : ?>
<div class="category-index">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('tree', ['data' => $data]) ?>
</div>
<?php endif ?>
//...View tree.php
<?php
use ale10257\ext\GetTreeWidget;
use yii\helpers\Url;
//Number tree in your Nested Set tree
echo GetTreeWidget::widget([
'options' => [
'data' => $data,
// action for ajax request for edit tree
'urlChangeTree' => Url::to(['/admin/category/update-tree']),
'urlUpdateTree' => Url::to(['/admin/category/update']),
'urlDeleteTree' => Url::to(['/admin/category/delete']),
'urlAddItem' => Url::to(['/admin/category/create']),
// name field in your table for view
'fieldForTitleItem' => 'name',
]
]);
?>Controller
<?php
//...
public function actionIndex()
{
$category = new Category;
return $this->render('index', [
'data' => $category->getTree(),
]);
}
//...
//accepts the ajax request
public function actionUpdateTree()
{
if (Yii::$app->request->isAjax) {
$model = new Category();
$post = Yii::$app->request->post();
return $this->renderPartial('tree', ['data' => $model->updateTree($post)]);
}
return Yii::$app->request->referrer;
}
//...
?>

