项目作者: DevGroup-ru

项目描述 :
jsTree tree widget for yii2
高级语言: PHP
项目地址: git://github.com/DevGroup-ru/yii2-jstree-widget.git
创建时间: 2014-11-24T12:36:08Z
项目社区:https://github.com/DevGroup-ru/yii2-jstree-widget

开源协议:MIT License

下载


yii2-jstree-widget

Code Climate
SensioLabsInsight
Scrutinizer Code Quality
Build Status

jsTree tree widget for yii2.

Current state: unstable.

Description

This extension allows you to display and manage hierarchical data structures from your
database using jsTree.

For now following data structure types are supported:

Usage example

For example, we have model Menu that represents our structured data. And MenuController for management purposes.

Adjacency List

In the MenuController:

  1. use devgroup\JsTreeWidget\actions\AdjacencyList\FullTreeDataAction;
  2. use devgroup\JsTreeWidget\actions\AdjacencyList\TreeNodesReorderAction;
  3. use devgroup\JsTreeWidget\actions\AdjacencyList\TreeNodeMoveAction;
  4. ...
  5. public function actions()
  6. {
  7. return [
  8. 'getTree' => [
  9. 'class' => FullTreeDataAction::class,
  10. 'className' => Menu::class,
  11. ],
  12. 'menuReorder' => [
  13. 'class' => TreeNodesReorderAction::class,
  14. 'className' => Menu::class,
  15. ],
  16. 'menuChangeParent' => [
  17. 'class' => TreeNodeMoveAction::class,
  18. 'className' => Menu::class,
  19. ],
  20. ];
  21. }

In your view file call the widget in the right place:

  1. <?= TreeWidget::widget([
  2. 'treeDataRoute' => ['/menu/getTree', 'selected_id' => $parent_id],
  3. 'reorderAction' => ['/menu/menuReorder'],
  4. 'changeParentAction' => ['/menu/menuChangeParent'],
  5. 'treeType' => TreeWidget::TREE_TYPE_ADJACENCY,
  6. 'contextMenuItems' => [
  7. 'open' => [
  8. 'label' => 'Open',
  9. 'action' => ContextMenuHelper::actionUrl(
  10. ['/menu/list'],
  11. ['parent_id']
  12. ),
  13. ],
  14. 'edit' => [
  15. 'label' => 'Edit',
  16. 'action' => ContextMenuHelper::actionUrl(
  17. ['/menu/edit']
  18. ),
  19. ]
  20. ],
  21. ]) ?>

Getting Data, Reordering and Change Parent actions has default implementations, but you can implement and use your own ones, just by changing a routes 'treeDataRoute', 'reorderAction', 'changeParentAction'.

Nested Set

Nested set can work in single or multy root modes. Single root mode by default.
For using multi root mode you have to have tree (or other name you like) column in your database table to store root id. And define this name in all necessary config places (see below).

In the MenuController:

  1. use devgroup\JsTreeWidget\actions\nestedset\FullTreeDataAction;
  2. use devgroup\JsTreeWidget\actions\nestedset\NodeMoveAction;
  3. ...
  4. public function actions()
  5. {
  6. return [
  7. 'getTree' => [
  8. 'class' => FullTreeDataAction::class,
  9. 'className' => Menu::class,
  10. 'rootAttribute' => 'tree', //omit for single root mode
  11. ],
  12. 'treeReorder' => [
  13. 'class' => NodeMoveAction::class,
  14. 'className' => Menu::class,
  15. 'rootAttribute' => 'tree', //omit for single root mode
  16. ],
  17. ];
  18. }

In the view file:

  1. <?= TreeWidget::widget([
  2. 'treeDataRoute' => ['/menu/getTree'],
  3. 'reorderAction' => ['/menu/treeReorder'],
  4. 'treeType' => TreeWidget::TREE_TYPE_NESTED_SET, //important config option
  5. 'contextMenuItems' => [
  6. 'edit' => [
  7. 'label' => 'Edit',
  8. 'action' => ContextMenuHelper::actionUrl(
  9. ['/menu/edit']
  10. ),
  11. ]
  12. ],
  13. ]) ?>

Getting Data and Node Movements actions has the default implementations and are independent from side NestedSet behaviors. But you also can use your own implementation.

TreeWidget will register bundle JsTreeAssetBundle, but you may want to include it as dependency in your main bundle(ie. for minification purpose).

ContextMenuHelper creates JsExpression for handling context menu option click. It automatically adds all data attributes from item link(<a> tag) if it is not specified exactly(as in ‘open’ menu item).