Ковыряю nested sets, ну и собственно виджет от kartik.
Для меню.
И не могу понять как правильно реализовать добавление нового корня.
То есть, при клике на +, появляется форма справа.
Но поле ID создаваемого элемента - видимое. И нередактируемое.
Хотя должно быть невидимым и id в бд создаваться по AI.
Форма по дефолту.
Если через экшен create (зелёная кнопка сверху) - всё создаётся.
Вообщем прошу помощи.
Может быть кто-нибудь имеет опыт использования?
Как реализовали?
Облазил всю документацию, но ничего не сообразил.
Скорее всего нужно копать Nested-sets от creocodera. Вечер потерян. Результата нет.
index.php
Код: Выделить всё
/* @var $this yii\web\View */
/* @var $searchModel backend\Models\KartikTreeMenuSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Kartik Tree Menus';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="kartik-tree-menu-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Kartik Tree Menu', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= TreeView::widget([
// single query fetch to render the tree
// use the Product model you have in the previous step
'query' => KartikTreeMenuSearch::find()->addOrderBy('root, lft'),
'headingOptions' => ['label' => 'Меню'],
'fontAwesome' => true, // font awesome icons instead of glyphicons
'isAdmin' => true, // optional (toggle to enable admin mode)
'displayValue' => 1, // initial display value
'iconEditSettings' => [
'show' => 'list',
'listData' => [
'folder' => 'Folder',
'file' => 'File',
'mobile' => 'Phone',
'bell' => 'Bell',
],
],
'softDelete' => false, // Удаление пункта
'cacheSettings' => [
'enableCache' => true // defaults to true
],
// 'nodeAddlViews' => [
// \kartik\tree\Module::VIEW_PART_2 => '@backend/views/mainmenu/_form',
// ]
]); ?>
</div>
Код: Выделить всё
<?php
namespace backend\controllers;
use Yii;
use common\models\KartikTreeMenu;
use backend\models\KartikTreeMenuSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* KartikTreeMenuController implements the CRUD actions for KartikTreeMenu model.
*/
class MainmenuController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all KartikTreeMenu models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new KartikTreeMenuSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single KartikTreeMenu model.
* @param string $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new KartikTreeMenu model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new KartikTreeMenu();
// var_dump($model);
$model->makeRoot();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect('/');
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing KartikTreeMenu model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param string $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing KartikTreeMenu model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param string $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the KartikTreeMenu model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $id
* @return KartikTreeMenu the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = KartikTreeMenu::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
Код: Выделить всё
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "kartik_tree_menu".
*
* @property string $id
* @property integer $root
* @property integer $lft
* @property integer $rgt
* @property integer $lvl
* @property string $name
* @property string $url
* @property string $icon
* @property integer $icon_type
* @property integer $active
* @property integer $selected
* @property integer $disabled
* @property integer $readonly
* @property integer $visible
* @property integer $collapsed
* @property integer $movable_u
* @property integer $movable_d
* @property integer $movable_l
* @property integer $movable_r
* @property integer $removable
* @property integer $removable_all
*/
class KartikTreeMenu extends \kartik\tree\models\Tree
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'kartik_tree_menu';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['lft', 'rgt', 'lvl', 'icon_type', 'active', 'selected', 'disabled', 'readonly', 'visible', 'collapsed', 'movable_u', 'movable_d', 'movable_l', 'movable_r', 'removable', 'removable_all'], 'integer'],
[['name', 'url'], 'required'],
[['name'], 'string', 'max' => 60],
[['url'], 'string', 'max' => 100],
[['icon'], 'string', 'max' => 255],
[['root', 'id', 'lft', 'rgt', 'lvl'], 'safe']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'root' => 'Root',
'lft' => 'Lft',
'rgt' => 'Rgt',
'lvl' => 'Lvl',
'name' => 'Name',
'url' => 'Url',
'icon' => 'Icon',
'icon_type' => 'Icon Type',
'active' => 'Активный',
'selected' => 'Selected',
'disabled' => 'Disabled',
'readonly' => 'Readonly',
'visible' => 'Visible',
'collapsed' => 'Collapsed',
'movable_u' => 'Movable U',
'movable_d' => 'Movable D',
'movable_l' => 'Movable L',
'movable_r' => 'Movable R',
'removable' => 'Removable',
'removable_all' => 'Removable All',
];
}
}