Yii 1.1: mtreeview

CTreeView++.
29 followers

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

Join Discussion

More Detailed Explanation

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/

http://snipplr.com/view/4383/build-nested-array-from-sql/

Total 19 comments

#7546 report it
macinville at 2012/03/29 05:15am
@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.

#7533 report it
pligor at 2012/03/28 06:19am
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 :)

#7517 report it
pligor at 2012/03/27 02:26pm
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!.... :/

#5963 report it
jover at 2011/11/30 08:51pm
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.

#5587 report it
macinville at 2011/10/22 02:39pm
Re: Broken Link

Hi Kabinenkoffer, the link has been updated, thanks for pointing that out.

#5524 report it
Kabinenkoffer at 2011/10/18 04:59am
Broken Link

Hi, the link to the latest version is broken. Just to let you know. Greetings

#5314 report it
moysoft at 2011/10/04 01:03am
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.

#5303 report it
macinville at 2011/10/03 01:24am
Thanks

Thanks for sharing your experience with us moysoft. I'm glad your problem is now solved. :)

#5301 report it
moysoft at 2011/10/02 07:55pm
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.

#5293 report it
macinville at 2011/10/01 10:38pm
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').

#5265 report it
moysoft at 2011/09/28 12:34am
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 node

no tree view shown. It's looks like every java script i call is not responding.

Please help me, maybe i forget something.

#4668 report it
fahania at 2011/08/02 09:02am
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.

#4508 report it
macinville at 2011/07/14 11:07am
MTreeView v2r2

Thanks to Timbo for noticing that template is still being reset to the old pattern. Apologies for the inconvenience.

#4499 report it
macinville at 2011/07/13 11:14pm
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.

#4465 report it
macinville at 2011/07/11 01:05pm
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. :)

#4464 report it
Phelipe Folgierini at 2011/07/11 12:22pm
Error

Theres a error in the Table of Properties:

text    adjacency,nested set    Required: the URL of a node
url     adjacency,nested set    Optional: the text of a node

When the correct form is as it:

url     adjacency,nested set    Required: the URL of a node
text    adjacency,nested set    Optional: the text of a node
#4454 report it
yiqing95 at 2011/07/10 10:23am
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 .

#4453 report it
macinville at 2011/07/10 09:13am
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.

#4452 report it
emc at 2011/07/10 07:45am
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!

Leave a comment

Please to leave your comment.

Create extension