ale10257 / yii2-ext-for-work-nested-set
Include widget and behavior for edit Nested Set tree
Installs: 153
Dependents: 2
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- ale10257/yii2-ext-for-work-nested-set: dev-master
- creocoder/yii2-nested-sets: dev-master
- rmrevin/yii2-fontawesome: ~2.17
- yiisoft/yii2: ~2.0.0
This package is not auto-updated.
Last update: 2022-01-08 14:07:22 UTC
README
Include widget and behavior for edit Nested Set tree
Installation
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.
Capabilities
-
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
Important
Only siblings elements can be dragged!!!
Usage
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; } //... ?>