PHP code example of coderovich / yii2-jstree
1. Go to this page and download the library: Download coderovich/yii2-jstree 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/ */
coderovich / yii2-jstree example snippets
const NODE_NAME = "nodeName";
# Your custom item renderer, named like get+self::NODE_NAME
public function getNodeName() {
return "<strong>" . $this->name . "</strong>";
}
public function actions() {
$modelClass = 'app\models\TreeModel';
return [
'moveNode' => [
'class' => 'coderovich\jsTree\actions\MoveNodeAction',
'modelClass' => $modelClass,
],
'deleteNode' => [
'class' => 'coderovich\jsTree\actions\DeleteNodeAction',
'modelClass' => $modelClass,
],
'updateNode' => [
'class' => 'coderovich\jsTree\actions\UpdateNodeAction',
'modelClass' => $modelClass,
],
'createNode' => [
'class' => 'coderovich\jsTree\actions\CreateNodeAction',
'modelClass' => $modelClass,
],
'fetchTree' => [
'class' => 'coderovich\jsTree\actions\FetchTreeAction',
'modelClass' => $modelClass,
# Максимально возможное число уровней в дереве. Опционально.
# Callable
"maxDepth" => function ( $node ) {
/** @var \app\models\TreeModel $node */
return $node->depth < 2;
}
],
];
}
use coderovich\jsTree\JsTree;
echo JsTree::widget([
'modelClass' => '\app\models\TreeModel',
'core' => [
"themes" => [ "stripes" => true ],
],
'contextmenu' => [
"items_top"=>new \yii\web\JsExpression( '{"assoc": {
"separator_before": false,
"separator_after": false,
"label": "My Label",
"title": "My Label",
"_disabled":function(){
return node.text.match(/disable_me_by_some_js_behavior_if_needed/i);
},
"action": function (obj) {
window.location = "'.\yii\helpers\Url::to(['update']).'?id="+node.id
}
}}' )
],
'plugins' => [ "changed", "state", 'types', 'dnd', 'contextmenu', 'sort' ,'wholerow'],
//...
]);
<div class="region-form">
$form = yii\widgets\ActiveForm::begin( [
"enableAjaxValidation" => false,
"enableClientValidation" => true,
"options" => [ "id"=>"JsTreeDialogForm",'role' => "form" ]
] );