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
);