CTreeView AJAX Tree

:o

It seems to exit in the very beginning:


if (!Yii::app()->request->isAjaxRequest) {

                        exit();

}

That’s the problem. Do you know why is not an ajax request?

Thats whay i was wondering if I should do anything to enable Ajax or what…

I would appreciatte if you could give any hint on this. Thank u.

Hi hav3fun,

Wierd, because I actually got it working.

These is my table:




CREATE TABLE `menu` (

 `id` int(11) NOT NULL AUTO_INCREMENT,

 `id_parent` int(11) DEFAULT NULL,

 `title` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,

 `position` int(2) NOT NULL,

 `url` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,

 `icon` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,

 PRIMARY KEY (`id`),

 KEY `id_parent` (`id_parent`)

) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1



This is the part of my controller:




public function actionAjaxFillTree() {

        if (!Yii::app()->request->isAjaxRequest) {

            exit();

        }

        $parentId = "NULL";

        if (isset($_GET['root']) && $_GET['root'] !== 'source') {

            $parentId = (int) $_GET['root'];

        }

        $req = Yii::app()->db->createCommand(

                        "SELECT m1.id, m1.title AS text, m2.id IS NOT NULL AS hasChildren "

                        . "FROM menu AS m1 LEFT JOIN menu AS m2 ON m1.id=m2.id_parent "

                        . "WHERE m1.id_parent <=> $parentId "

                        . "GROUP BY m1.id ORDER BY m1.title ASC"

        );

        $children = $req->queryAll();

        echo str_replace(

                '"hasChildren":"0"', '"hasChildren":false', CTreeView::saveDataAsJson($children)

        );

        exit();

    }



And this is my view:




<?php

$this->widget('CTreeView', array(

    'animated' => 'fast', //quick animation

    'collapsed' => true,

    'url' => array('site/ajaxFillTree'),

    'htmlOptions' => array(

        'class' => 'treeview-famfamfam',

    ))

);

?>



insert these in your menu table for samples:




INSERT INTO `menu` (

`id` ,

`id_parent` ,

`title` ,

`position` ,

`url` ,

`icon`

)

VALUES (

'1', NULL , 'tree1', '', '', NULL

), (

'2', '1', 'subtree1', '', '', NULL

), (

'3', NULL , 'tree2', '', '', NULL

), (

'4', '3', 'subtree2', '', '', NULL

), (

'5', '3', 'subtree2', '', '', NULL

);




Thanks for the info,

Finally it worked yesterday. Basically I didn´t changed anything but in the way of calling the view with the tree, so that I´m sure that the call is an AJAX call.

Now I´m trying with the SimpleTree extension, which was my intention in the first place. Wish me luck! :)

Thanks

YEAAAAH , IT WORKED , Thanks Macinville , you hellped me alot =D . :D

@hav3fun

Glad you got it rolling. Good luck! :)

@talles

Welcome! Good luck also! :)

Hi, I am having the same problem that have3fun was having. The html only shows


<ul class="treeview-famfamfam" id="yw0"></ul>

and no content is loaded. I am using the wiki example. Can you help, is there a problem with accessrules.

the problem that caused the sample code above not working is this

app()->createUrl

it missed Yii::app()->createUrl

after i changed that, it works.

Hi

CTreeView won’t display when i add createLinks() function

my CTreeView works great with out urls

how can i add url for each nodes??

I’m currently working on extending the CTreeView to allow links if is given in the table. But for now, can you please post your table here? Let me use it to check my extension if it will work.

I have just write something about this. you can find solution here

Hi… i tried but get error like:

use: sqlite3 and Yii Version 1.1.8

I do not understand the meaning of this syntax:




<=> $parentId



The explanation for ‘<=>’ is in the wiki page:

Probably an error when used with sqlite3? I’m tied to MySQL as of the moment, can’t test your scenario yet.

BTW,in my example given, I have provided a link to my base theme to retrieve the icons. If your tree does not need an icon, delete that part…it might just cause trouble if not.

I have finished creating an extension which extends to CTreeView and can create links with ease. It can handle both Nested Set and Adjacency List model, and can be used in AJAX calls too!

Extension page: MTreeView

Downloads: MTreeView.zip

Explanation: My Blog

This is the thread I created so can voice out your observations and comments and suggestions.

Edit: updated the links

@macinville

thank you :)

hi

thanks … it’s work. :D

when data inserted to the database, how can i update CTreevie with Jquery (without refresh page)??

a great work, many thanks~

I have improved the extension so as to make it useful to more “tree-lovers”. Click here to be redirected to the extension’s discussion page.

where,

if i call tree from three table??

If I use oracle database then what should be the query to get treeview for the same example?