JsTreeBehavior
© 2013 Spiros Kabasakalis The MIT License (MIT)

Tree graphic manipulation of an ActiveRecord with NestedSet Behavior using jstree plugin. This is actually a complete rewrite of my NestedSetAdminGUI extension. Instead of gii generated files for every model,I wrote a reusable behavior that provides the same functionality.I also have separated markup from js,and the code is much cleaner now.Many jstree options can be set in JsTreeWidget.php file,you can add more if you wish-I did'nt include them all,there are so many.
'urlFormat'=>'path', 'showScriptName'=>false,
Create the ActiveRecord class file that corresponds to category,say Category.php.Give it a nested set behavior.In Category.php :
public function behaviors() { return array( 'NestedSetBehavior'=>array( 'class'=>'application.behaviors.NestedSetBehavior', 'leftAttribute'=>'lft', 'rightAttribute'=>'rgt', 'levelAttribute'=>'level', 'hasManyRoots'=>true ), ); }
Copy BaseController.php file to components folder.This controller makes possible for controllers that extend from it to turn behavior inherited functions into actions.
Create a controller in protected/controllers folder that extends from BaseController.php,CategoryController and give it a JsTreeBehavior.In CategoryController:
public function behaviors() { return array( 'jsTreeBehavior' => array('class' => 'application.behaviors.JsTreeBehavior', 'modelClassName' => 'Category', 'form_alias_path' => 'application.views.category._form', 'view_alias_path' => 'application.views.category.view', 'label_property' => 'name', 'rel_property' => 'name' ) ); }
Last, add an action actionTree in Category Controller that renders a tree.php file.In tree.php file render the JsTreeWidget like so
$this->widget('application.widgets.JsTreeWidget', array('modelClassName' => 'Category', 'jstree_container_ID' => 'Category-wrapper',//jstree will be rendered in this div.id of your choice. 'themes' => array('theme' => 'default', 'dots' => true, 'icons' => true), 'plugins' => array('themes', 'html_data', 'contextmenu', 'crrm', 'dnd', 'cookies', 'ui') ));
Fot themes and plugins options see jtree documentation
Make sure you include jquery.Either uncomment the relevant line in JsTreeWidget or register it somewhwere else in your code.
To manipulate the tree,navigate to category/tree.Right click on nodes to see available operations.Drag and drop nodes,delete,create,update etc.
Total 2 comments
Hi,thanks for the extension
It works fine
Leave a comment
Please login to leave your comment.