├── .php_cs ├── TreeAsset.php ├── composer.json ├── LICENSE.md ├── Tree.php └── README.md /.php_cs: -------------------------------------------------------------------------------- 1 | exclude('vendor') 5 | ->in([__DIR__]); 6 | 7 | $config = PhpCsFixer\Config::create() 8 | ->setUsingCache(false) 9 | ->setRules([ 10 | '@Symfony' => true, 11 | 'phpdoc_align' => false, 12 | 'phpdoc_summary' => false, 13 | 'phpdoc_inline_tag' => false, 14 | 'pre_increment' => false, 15 | 'heredoc_to_nowdoc' => false, 16 | 'cast_spaces' => false, 17 | 'include' => false, 18 | 'phpdoc_no_package' => false, 19 | 'concat_space' => ['spacing' => 'one'], 20 | 'ordered_imports' => true, 21 | 'array_syntax' => ['syntax' => 'short'], 22 | ]) 23 | ->setFinder($finder); 24 | 25 | return $config; 26 | -------------------------------------------------------------------------------- /TreeAsset.php: -------------------------------------------------------------------------------- 1 | 11 | * 12 | * @since 1.0 13 | */ 14 | class TreeAsset extends AssetBundle 15 | { 16 | /** 17 | * @var string 18 | */ 19 | public $sourcePath = '@bower/fancytree/dist'; 20 | 21 | /** 22 | * @var array 23 | */ 24 | public $css = [ 25 | 'skin-lion/ui.fancytree.css', 26 | ]; 27 | 28 | /** 29 | * @var array 30 | */ 31 | public $js = [ 32 | 'jquery.fancytree-all.js', 33 | ]; 34 | 35 | /** 36 | * @var array 37 | */ 38 | public $depends = [ 39 | 'yii\jui\JuiAsset', 40 | ]; 41 | } 42 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yii2mod/yii2-tree", 3 | "description": "Tree widget based on Fancytree plugin.", 4 | "type": "yii2-extension", 5 | "keywords": [ 6 | "yii2", 7 | "tree", 8 | "tree widget" 9 | ], 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Igor Chepurnoi", 14 | "email": "chepurnoi.igor@gmail.com" 15 | } 16 | ], 17 | "require": { 18 | "yiisoft/yii2": "*", 19 | "yiisoft/yii2-jui": "~2.0.0", 20 | "bower-asset/fancytree": "*" 21 | }, 22 | "require-dev": { 23 | "friendsofphp/php-cs-fixer": "~2.0" 24 | }, 25 | "autoload": { 26 | "psr-4": { 27 | "yii2mod\\tree\\": "" 28 | } 29 | }, 30 | "repositories": [ 31 | { 32 | "type": "composer", 33 | "url": "https://asset-packagist.org" 34 | } 35 | ] 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 yii2mod 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /Tree.php: -------------------------------------------------------------------------------- 1 | 13 | * 14 | * @since 1.0 15 | */ 16 | class Tree extends Widget 17 | { 18 | /** 19 | * @var array list of categories 20 | */ 21 | public $items = []; 22 | 23 | /** 24 | * @var string container id 25 | */ 26 | public $id = 'tree'; 27 | 28 | /** 29 | * @var array 30 | */ 31 | public $clientOptions = []; 32 | 33 | /** 34 | * Executes the widget. 35 | */ 36 | public function run() 37 | { 38 | $this->clientOptions['source'] = $this->items; 39 | echo Html::tag('div', '', ['id' => $this->id]); 40 | $this->registerAssets(); 41 | } 42 | 43 | /** 44 | * Register assets 45 | */ 46 | protected function registerAssets() 47 | { 48 | $view = $this->getView(); 49 | TreeAsset::register($view); 50 | $js = '$("#' . $this->id . '").fancytree(' . $this->getClientOptions() . ');'; 51 | $view->registerJs($js, $view::POS_END); 52 | } 53 | 54 | /** 55 | * @return string 56 | */ 57 | public function getClientOptions() 58 | { 59 | return Json::encode($this->clientOptions); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | 5 |

Yii2 Tree Widget

6 |
7 |

8 | 9 | Tree widget based on Fancytree extension http://wwwendt.de/tech/fancytree/demo/#sample-default.html 10 | 11 | [![Latest Stable Version](https://poser.pugx.org/yii2mod/yii2-tree/v/stable)](https://packagist.org/packages/yii2mod/yii2-tree) 12 | [![Total Downloads](https://poser.pugx.org/yii2mod/yii2-tree/downloads)](https://packagist.org/packages/yii2mod/yii2-tree) 13 | [![License](https://poser.pugx.org/yii2mod/yii2-tree/license)](https://packagist.org/packages/yii2mod/yii2-tree) 14 | [![Build Status](https://travis-ci.org/yii2mod/yii2-tree.svg?branch=master)](https://travis-ci.org/yii2mod/yii2-tree) 15 | 16 | 17 | Installation 18 | ------------ 19 | 20 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 21 | 22 | Either run 23 | 24 | ``` 25 | php composer.phar require --prefer-dist yii2mod/yii2-tree "*" 26 | ``` 27 | 28 | or add 29 | 30 | ``` 31 | "yii2mod/yii2-tree": "*" 32 | ``` 33 | 34 | to the require section of your composer.json. 35 | 36 | Usage 37 | ------------ 38 | Once the extension is installed, simply add widget to your page as follows: 39 | 40 | ```php 41 | [ 43 | ['title' => 'Category 1'], 44 | ['title' => 'Category 2'], 45 | [ 46 | 'title' => 'Category 3', 47 | 'children' => [ 48 | [ 49 | 'title' => 'Category 3.1', 50 | ], 51 | [ 52 | 'title' => 'Category 3.2', 53 | 'children' => [ 54 | [ 55 | 'title' => 'Category 3.2.1', 56 | ] 57 | ], 58 | 'folder' => true, 59 | ], 60 | ], 61 | 'folder' => true, 62 | ], 63 | 64 | ], 65 | 'clientOptions' => [ 66 | 'autoCollapse' => true, 67 | 'clickFolderMode' => 3, 68 | 'activate' => new \yii\web\JsExpression(' 69 | function(node, data) { 70 | node = data.node; 71 | // Log node title 72 | console.log(node.title); 73 | } 74 | '), 75 | ], 76 | ]); ?> 77 | ``` 78 | 79 | 80 | **You can also change the theme of Fancytree extension!** 81 | 82 | To change the theme, you can configure the assetManager array in your application configuration: 83 | ```php 84 | // skin-win8 85 | 86 | 'assetManager' => [ 87 | 'bundles' => [ 88 | 'yii2mod\tree\TreeAsset' => [ 89 | 'css' => [ 90 | 'skin-win8/ui.fancytree.less', 91 | ] 92 | ], 93 | ], 94 | ] 95 | ``` 96 | --------------------------------------------------------------------------------