Extension for edit Nested Set tree
Include widget and behavior for edit Nested Set tree, (*1)
Installation
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
composer require ale10257/yii2-ext-for-work-nested-set "dev-master"
or add, (*4)
"ale10257/yii2-ext-for-work-nested-set": "dev-master"
to the require section of your composer.json
file., (*5)
Capabilities
-
Generating a menu tree as a table (use Nested Set tree), (*6)
-
Drag & Drop rows table, (*7)
-
Save result in DB, (*8)
Example table before move, (*9)
, (*10)
Move, (*11)
, (*12)
After move, (*13)
, (*14)
Important
Only siblings elements can be dragged!!!, (*15)
Usage
Model, (*16)
Set up the model as here: https://github.com/creocoder/yii2-nested-sets
and determine the constant SITE_ROOT_NAME and behaviors, (*17)
NestedSetsBehavior::className(),
'treeAttribute' => 'tree',
],
[
'class' => ChangeTreeBehavior::className(),
'rootSite' => SITE_ROOT_NAME
]
];
}
//...
?>
View index.php for Category, (*18)
//...
<?php if($data) : ?>
<div class="category-index">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('tree', ['data' => $data]) ?>
</div>
<?php endif ?>
//...
View tree.php, (*19)
[
'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, (*20)
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;
}
//...
?>