MTreeView extends CTreeView, which displays a tree view of hierarchical data. It can handle both nested set and adjacency hierarchy model,and can create a linked node and adds icon if you wish. It can also be used to render AJAX tree!
Requirements ¶
Yii 1.1.7 and above (not tested with 1.1.6 and below,so I'm not sure whether it'll work there)
Usage ¶
Download the latest MTreeView_2.1.zip and extract to extensions folder.
Click here to view all releases or MTreeView
Refer here to view all the available options for MTreeView.
Upgrade Instructions ¶
If you are already using the initial release of MTreeView, all you have to do is replace the MTreeView.php. There should be no problem encountered after the update, but just to be sure, just rename the old MTreeView.php. Please report any errors which will be encountered at our discussion page.
For more detailed explanation about this extension, visit my blog or download the latest sample zip file to play with the extension. The new sample zip file can be used with the sample database for version 1.1 (mtreeview_1.1.sql).
Example ¶
$this->widget('application.extensions.MTreeView.MTreeView',array(
'collapsed'=>true,
'animated'=>'fast',
//---MTreeView options from here
'table'=>'menu_adjacency',//what table the menu would come from
'hierModel'=>'adjacency',//hierarchy model of the table
'conditions'=>array('visible=:visible',array(':visible'=>1)),//other conditions if any
'fields'=>array(//declaration of fields
'text'=>'title',//no `text` column, use `title` instead
'alt'=>false,//skip using `alt` column
'id_parent'=>'parent_id',//no `id_parent` column,use `parent_id` instead
'task'=>false,
'icon'=>false,
'url'=>array('/menuAdjacency/view',array('id'=>'id'))
),
));
Download the latest sample ZIP file for examples of usage and further details.
Links ¶
References (aside from Yii docs) ¶
http://stackoverflow.com/questions/841014/nested-sets-php-array-and-transformation
http://semlabs.co.uk/journal/converting-nested-set-model-data-in-to-multi-dimensional-arrays-in-php
http://www.fliquidstudios.com/2008/12/23/nested-set-in-mysql/
very nice!
Wow, I'm amazed with your packed example. Though the explanation is not so detailed, it can be easily understood.
So far so good!Keep up the good work!
Thumbs up!
Minor Change
I have replaced the downloadable files, I made a small difference with AJAX tree. For some reason, the node can no longer be collapsed or expanded when the text of the node is clicked (unlike with the non-AJAX tree). At any rate, the tree can still be opened by clicking the "hitArea" (the icon to open the tree) of the node. Nevertheless, I will still look for a solution for this. If someone has a solution for this, please inform me so I can update the extension.
cool i v expected such a combination a long time , it comes!
when faced hierarchy data , it's hard to choice which model meet my requirements , in my project i use the adjacency model,but i found the nested set model some times more convenience ,so i need a combination UI ,in future may be i will change the adjacency to nested set model but do little change of my existing view file .
Error
Theres a error in the Table of Properties:
When the correct form is as it:
Updates!
I updated the extension so that each node can now have different htmlOptions. I added the field 'option', which value should be encoded in JSON form (such as {"style":"font-weight:bold"}). This is disabled by default, so if you're not going to use this additional feature, you don't have to update anything in your created widget.
Download the latest sample zip file to further test the update. Your comments and suggestions will be greatly appreciated.
@mettalph
Thanks! I forgot to update it. :)
Minor update
I have changed the downloadable latest version, I forgot to consider the 'options' of the node (if given) once the 'url' is an array.
Apologies for the inconvenience.
MTreeView v2r2
Thanks to Timbo for noticing that template is still being reset to the old pattern. Apologies for the inconvenience.
Nested Set
I use this extension in combination with the Yii nestedset extension. Works great. But your example of the widget does not work entirely
'conditions'=>array('visible=:visible',array(':visible'=>1))
should be replaced by
'conditions'=>array('t1.visible=:visible',array(':visible'=>1))
otherwise you get a not ambigious error. Not really a neat solution i think.
Ajax Not Work!!
Hi macinville,
I have installed your sample code and i have to try the sample, it's work, but when i apply to my yii project it doesn't work.
This is my controller (MemberController) looks like :
class MemberController extends Controller { ... public function accessRules() { return array( ... array('allow', 'actions'=>array('ajaxfilltree','update','home','interests','addsomething'), 'users'=>array('@'), ), ... } public function actionAddSomeThing(){ $this->render('_ajaxTree'); } public function actionajaxFillTree(){ Yii::import('application.extensions.MTreeView.MTreeView'); if (!Yii::app()->request->isAjaxRequest) { exit(); } $parentId = "NULL"; if (isset($_GET['root']) && $_GET['root'] !== 'source') { $parentId = (int) $_GET['root']; } $sql = "SELECT m1.id, m1.title AS text, m2.id IS NOT NULL AS hasChildren " . "FROM menu_adjacency AS m1 LEFT JOIN menu_adjacency AS m2 ON m1.id=m2.parent_id " . "WHERE m1.parent_id <=> $parentId " . "GROUP BY m1.id ORDER BY m1.position ASC"; $req = Yii::app()->db->createCommand($sql); $children = $req->queryAll(); echo str_replace( '"hasChildren":"0"', '"hasChildren":false', MTreeView::saveDataAsJson($children) ); exit(); }
in view _ajaxTree.php looks like this :
<p> AJAX Tree with links and icons, with the template set to '{icon}Text:{text}'.<br> UPDATED: considered the htmlOptions (which is saved in the `options` field) for the node </p> <?php $this->widget('application.extensions.MTreeView.MTreeView', array('url'=>array('ajaxFillTree'), 'animated'=>'fast', ) ); ?>
and in the view that call the addSomeThing (_something.php) :
<?php echo CHtml::link('Add Something',array('/member/addsomething',)); ?>
and the browser just output :
AJAX Tree with links and icons, with the template set to '{icon}Text:{text}'.
UPDATED: considered the htmlOptions (which is saved in the
options
field) for the nodeno tree view shown. It's looks like every java script i call is not responding.
Please help me, maybe i forget something.
Hi moysoft
So sorry I was not able to attend to your issue quick enough. Anyway, I think there's something wrong with the spelling of your controller. Instead of
public function actionajaxFillTree(){ ... }
, it should be
public function actionAjaxFillTree(){ ... }
(take note of the capital 'A' in the word 'ajax').
Re: Ajax Not Works!!!
I had solved my problem, the problem is in my sql statement that i used. In my case i'm using SQLServer 2008 Express, it's totally different with MySQL syntax that you used here.
public function actionajaxFillTree(){ Yii::import('application.extensions.MTreeView.MTreeView'); if (!Yii::app()->request->isAjaxRequest) { exit(); } $parentId = 0; if (isset($_GET['root']) && $_GET['root'] !== 'source') { $parentId = (int) $_GET['root']; } $sql = "select e.id,e.name as text, " . "(select sign(count(*)) from employee d " . " where d.manager_id = e.id " . " ) as hasChildren,'' as url,'' as icon,'' as tooltip,'' as options " . " from employee e where isnull(e.manager_id,0) = $parentId " . " order by 2 "; $req = Yii::app()->db->createCommand($sql); $children = $req->queryAll(); echo str_replace( '"hasChildren":"0"', '"hasChildren":false', MTreeView::saveDataAsJson($children) ); exit(); }
Rather than using "NULL" in initialy $parent_id i'm using 0. And change the sign <=> with isnull SQLServer function.
... $parentId = 0; if (isset($_GET['root']) && $_GET['root'] !== 'source') { $parentId = (int) $_GET['root']; } $sql = "select e.id,e.name as text, " . "(select sign(count(*)) from employee d " . " where d.manager_id = e.id " . " ) as hasChildren,'' as url,'' as icon,'' as tooltip,'' as options " . " from employee e where isnull(e.manager_id,0) = $parentId " . " order by 2 "; ...
Thank you for great Yii extension.
Thanks
Thanks for sharing your experience with us moysoft. I'm glad your problem is now solved. :)
Searching in tree
Hi MacInville,
Since i have a huge database record to presented as a tree, please provide the component with search capability to find text in the tree.
Thank you.
Broken Link
Hi,
the link to the latest version is broken. Just to let you know.
Greetings
Re: Broken Link
Hi Kabinenkoffer, the link has been updated, thanks for pointing that out.
AJAX tree with ajaxLink-enabled Tree, viewing the details of every node's link
Hi macinville,
I hope the AJAX tree also has the feature the same with adjacency and nested set which allow viewing the details of every node's link in a grid.
Line 291 error of version (tested at same day as comment)
After I changed it your code in line 291 of php file looks like this:
//return Yii::app()->createUrl($this->_url[0], $arrTemp, $arrOptions); return Yii::app()->createUrl($this->_url[0], $arrTemp);
It is strange that you call some options to be passed as a third parameter to createUrl. Maybe are you referring to an older version of Yii?..
It is also strange that inside your sample project everything works ok. I noticed that in your example the $arrOptions is not empty but it is a string with css style red
Still wondering how this string passed as a third parameter to createUrl does not return an error, like in my case!.... :/
case of an empty tree
Because in strict php the undefined variables are not allowed please place a $list=null; inside your code like this:
private function getAdjacentTree($menu) { $list = null; //...
Waiting for the new version!
Very good job until now, and also nicely presented :)
@pligor
My apologies if I have been so busy with my real life projects. I was in the process of improving the mtreeview,with new features like draggable nodes,node searching and editable labels, sadly my hands are full as of the moment so I can't update the extension yet.
Thank you all for the support.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.