Skip to content

Commit a61ba64

Browse files
committed
Release 1.0.0
- New scructure - NestedSets
1 parent 9fff664 commit a61ba64

14 files changed

+1288
-356
lines changed

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# phpstorm project files
2+
.idea
3+
4+
# netbeans project files
5+
nbproject
6+
7+
# zend studio for eclipse project files
8+
.buildpath
9+
.project
10+
.settings
11+
12+
# windows thumbnail cache
13+
Thumbs.db
14+
15+
# composer vendor dir
16+
/vendor
17+
18+
# composer itself is not needed
19+
composer.phar
20+
21+
# Mac DS_Store Files
22+
.DS_Store
23+
24+
# phpunit itself is not needed
25+
phpunit.phar
26+
# local phpunit config
27+
/phpunit.xml

README.md

Lines changed: 97 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,120 @@ jsTree tree widget for yii2.
1010

1111
Current state: **unstable**.
1212

13-
Created for use in [DotPlant2 E-Commerce CMS](http://dotplant.ru/).
13+
Description
14+
-----------
1415

16+
This extension allows you to display and manage hierarchical data structures from your
17+
database using [jsTree](https://www.jstree.com/).
18+
19+
For now following data structure types are supported:
20+
- [adjacency list](https://en.wikipedia.org/wiki/Adjacency_list);
21+
- [nested set](https://en.wikipedia.org/wiki/Nested_set_model).
1522

1623

1724
Usage example
1825
-------------
26+
For example, we have model Menu that represents our structured data. And MenuController for management purposes.
1927

20-
_Note:_ This package uses `devgroup\JsTreeWidget` namespace.
21-
22-
In your controller(assuming it's `backend/category`) add special action for retrieving Adjacency Tree:
28+
Adjacency List
29+
--------------
30+
In the MenuController:
2331

2432
``` php
33+
use devgroup\JsTreeWidget\actions\AdjacencyList\FullTreeDataAction;
34+
use devgroup\JsTreeWidget\actions\AdjacencyList\TreeNodesReorderAction;
35+
use devgroup\JsTreeWidget\actions\AdjacencyList\TreeNodeMoveAction;
36+
...
2537
public function actions()
26-
{
27-
return [
28-
'getTree' => [
29-
'class' => AdjacencyFullTreeDataAction::className(),
30-
'class_name' => Category::className(),
31-
'model_label_attribute' => 'name',
32-
33-
],
34-
];
35-
}
38+
{
39+
return [
40+
'getTree' => [
41+
'class' => FullTreeDataAction::class,
42+
'className' => Menu::class,
43+
],
44+
'menuReorder' => [
45+
'class' => TreeNodesReorderAction::class,
46+
'className' => Menu::class,
47+
],
48+
'menuChangeParent' => [
49+
'class' => TreeNodeMoveAction::class,
50+
'className' => Menu::class,
51+
],
52+
];
53+
}
3654
```
3755

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

4058
``` php
41-
<?=
42-
TreeWidget::widget([
43-
'treeDataRoute' => ['/backend/category/getTree', 'selected_id' => $parent_id],
44-
'contextMenuItems' => [
45-
'open' => [
46-
'label' => 'Open',
47-
'action' => ContextMenuHelper::actionUrl(
48-
['/backend/category/index'],
49-
[
50-
'parent_id',
51-
]
52-
),
53-
],
54-
'edit' => [
55-
'label' => 'Edit',
56-
'action' => ContextMenuHelper::actionUrl(
57-
['/backend/category/edit']
58-
),
59-
]
59+
<?= TreeWidget::widget([
60+
'treeDataRoute' => ['/menu/getTree', 'selected_id' => $parent_id],
61+
'reorderAction' => ['/menu/menuReorder'],
62+
'changeParentAction' => ['/menu/menuChangeParent'],
63+
'treeType' => TreeWidget::TREE_TYPE_ADJACENCY,
64+
'contextMenuItems' => [
65+
'open' => [
66+
'label' => 'Open',
67+
'action' => ContextMenuHelper::actionUrl(
68+
['/menu/list'],
69+
['parent_id']
70+
),
6071
],
61-
]);
62-
?>
72+
'edit' => [
73+
'label' => 'Edit',
74+
'action' => ContextMenuHelper::actionUrl(
75+
['/menu/edit']
76+
),
77+
]
78+
],
79+
]) ?>
80+
```
81+
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'`.
82+
83+
Nested Set
84+
----------
85+
Nested set can work in single or multy root modes. Single root mode by default.
86+
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).
6387

88+
In the MenuController:
89+
90+
``` php
91+
use devgroup\JsTreeWidget\actions\nestedset\FullTreeDataAction;
92+
use devgroup\JsTreeWidget\actions\nestedset\NodeMoveAction;
93+
...
94+
public function actions()
95+
{
96+
return [
97+
'getTree' => [
98+
'class' => FullTreeDataAction::class,
99+
'className' => Menu::class,
100+
'rootAttribute' => 'tree', //omit for single root mode
101+
],
102+
'treeReorder' => [
103+
'class' => NodeMoveAction::class,
104+
'className' => Menu::class,
105+
'rootAttribute' => 'tree', //omit for single root mode
106+
],
107+
];
108+
}
109+
```
110+
In the view file:
111+
```php
112+
<?= TreeWidget::widget([
113+
'treeDataRoute' => ['/menu/getTree'],
114+
'reorderAction' => ['/menu/treeReorder'],
115+
'treeType' => TreeWidget::TREE_TYPE_NESTED_SET, //important config option
116+
'contextMenuItems' => [
117+
'edit' => [
118+
'label' => 'Edit',
119+
'action' => ContextMenuHelper::actionUrl(
120+
['/menu/edit']
121+
),
122+
]
123+
],
124+
]) ?>
64125
```
126+
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.
65127

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

TreeWidget.php

Lines changed: 0 additions & 183 deletions
This file was deleted.

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "devgroup/yii2-jstree-widget",
33
"description": "jsTree widget for yii2",
44
"type": "yii2-extension",
5-
"keywords": ["yii2","jstree","tree","widget"],
5+
"keywords": ["yii2","jstree","tree","widget","nested-sets"],
66
"license": "MIT",
77
"authors": [
88
{
@@ -20,12 +20,13 @@
2020
],
2121
"minimum-stability": "dev",
2222
"require": {
23-
"yiisoft/yii2": "*",
24-
"bower-asset/jstree": "*"
23+
"yiisoft/yii2": "~2.0.0",
24+
"bower-asset/jstree": "*",
25+
"devgroup/yii2-tag-dependency-helper": "~0.0.1"
2526
},
2627
"autoload": {
2728
"psr-4": {
28-
"devgroup\\JsTreeWidget\\": ""
29+
"devgroup\\JsTreeWidget\\": "src/"
2930
}
3031
}
3132
}

0 commit comments

Comments
 (0)