rootlocal/yii2-jstree

jsTree is jquery plugin for Yii2

Installs: 19

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 1

Open Issues: 0

Type:yii2-extension

v1.0.3 2023-12-24 04:43 UTC

This package is auto-updated.

Last update: 2024-04-26 01:52:07 UTC


README

Latest Stable Version

tree1

Install

composer require rootlocal/yii2-jstree

or add

"rootlocal/yii2-jstree" : "dev-master",

to the require section of your composer.json.

View

<?php
use rootlocal\widgets\jstree\JsTreeWidget;
use yii\helpers\Url;
use yii\web\View;

/**
 * @var View $this
 */
?>

<?= JstreeWidget::widget([
    'theme' => JsTreeWidget::THEME_BOOTSTRAP3,
    'url' => Url::to(['tree/explorer-request']),
    'plugins' => ['dnd'],

    'actions' => [

        'rename' => [
            'url' => Url::to(['tree/rename-node']),
            'label' => 'Rename',
            'icon' => 'glyphicon glyphicon-pencil',
        ],

        'remove' => [
            'url' => Url::to(['tree/delete-node']),
            'label' =>  'Remove',
            'icon' => 'glyphicon glyphicon-remove',
        ],

        'select' => [
            'url' => Url::to('menu/update'),
        ],

        'move' => [
            'url' => Url::to(['tree/move-node']),
        ]

    ],

    'types' => [
        'child' => ['icon' => 'glyphicon glyphicon-leaf'],
        'root' => ['icon' => 'glyphicon glyphicon-folder-open'],
    ],

]) ?>

TreeController

use yii\web\Controller;

class TreeController extends Controller {
// ...

    /**
    * Request tree
    * @param string|int $id
    * @return array
    */
    public function actionExplorerRequest($id = '#'): array
    {
        if (Yii::$app->request->isPjax || Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
        }

        $data = [];

        if ($id === '#') {
            $children = Menu::find()->roots()->orderBy(['id' => SORT_ASC])->all();


            foreach ($children as $child) {
                $childrenCount = $child->getChildren()->count();

                $data[] = [
                    'id' => $child->id,
                    'text' => $child->name,
                    'children' => $childrenCount > 0,
                    //'icon' => 'glyphicon glyphicon-folder-close',
                    'type' => $childrenCount > 0 ? '' : 'child',
                ];
            }

            return $data;
        }

        $root = Menu::findOne($id);
        $data['id'] = $id;
        $data['text'] = $root->name;
        $data['children'] = [];

        $children = $root->getChildren()->all();

        foreach ($children as $child) {
            $childrenCount = Menu::findOne($child['id'])->getChildren()->count();

            $data['children'][] = [
                'id' => $child->id,
                'text' => $child->name,
                'children' => $childrenCount > 0,
                //'icon' => 'glyphicon glyphicon-folder-close',
                'type' => $childrenCount > 0 ? '' : 'child',
            ];
        }

        return $data;
    }
}