Hi all,
I design a ctreeview from a table which is working very well through ajax request. Now, each of my node corresponds to a type defined by an integer. My wish would be to put a static tree, an array i think, in a node if his type is, for example, equal to 2.
He is my controller function :
public function actionAjaxFillTree(){
if (!Yii::app()->request->isAjaxRequest) {
exit();
}
$parentId = "NULL";
if (isset($_GET['root']) && $_GET['root'] !== 'source') {
$parentId = (int) $_GET['root'];
}
else {
$parentId = '0';
}
$sql = 'select distinct on (m1.id) '
.'m1.id, m1.name, m2.id is not null as haschildren, m1.type '
.'from treemenu as m1 '
.'left join treemenu as m2 on m1.id=m2.parent_id '
.'where m1.parent_id='.$parentId.';' ;
$req = Yii::app()->db->createCommand($sql);
$children = $req->queryAll();
$children = $this->createLinks($children,$parentId);
echo str_replace(
'"haschildren":"0"',
'"haschildren":false',
CTreeView::saveDataAsJson($children)
);
exit();
}
private function createLinks($children,$parentId){
$return = array();
if($parentId != '0')
$model=$this->loadModel($parentId);
else
$model=$this->loadModel('1');
if ($model->type == '1')
{
foreach($children AS $key=>$value){
$child = array();
$child['id']=$value['id'];
$child['text']=$value['name'];
if($value['type']=='1')
{
$child['hasChildren']=true;
}
else
{
$child['hasChildren']=$value['haschildren'];
}
/*if(strlen($value['url'])>0){
$child['text'] = $this->format($value['text'],$value['url'],Yii::app()->request->url);
}*/
$return[] = $child;
}
}
elseif ($model->type == '2')
{
$return[] = array(
array('text'=>'First','children'=>array(
array('text'=>'Some Point', ),
array('text'=>'Another Point', 'children'=>array(
array('text'=>'Sub points are good', 'children'=>array(
array('text'=>'Like this'),
array('text'=>'Or even like this'),
)),
array('text'=>'Sub points are bad'),
array('text'=>'Sub points are sub points'),
)),
array('text'=>'More stuff'),
array('text'=>'Also this'),
)),
array('text'=>'Second'),
array('text'=>'Third', 'children'=>array(
array('text'=>'It goes on', ),
array('text'=>'and on...',)
)),
);
}
//if ($model->type != '2')
{ // Add new 'New entry' line
$child = array();
$child['id']=0;
$img = '<img id="treeImg1" src="/myNewAppTest/images/add.gif" height="10px" width="10px">';
$child['text'] = sprintf('<span>%s</span>',CHtml::link(($img." New entry"),"",array('onclick'=>"{updateElement('".$parentId."','".$this->createUrl('')."','treemenu-grid','#dialogTreemenu'); $('#dialogTreemenu').dialog('open');}")));
$child['hasChildren']=false;
$return[] = $child;
}
return $return;
}
and here my div
<?php $this->widget(
'CTreeView',
array(
'animated'=>'fast', //quick animation
'collapsed' => false,
'url' => array('/treemenu/ajaxFillTree'),
'persist' => 'cookie',
'cookieId' => 'group-tree',
'htmlOptions'=>array(
'class'=>'treeview-grey',
)
)
);
?>
My tree works very well except that I didn’t succeed to get my static tree shaped by the array. Indeed when I creat a node with a type 2, it’s not watched as a nod but as a simple children.
Do you have an Idea ? I don’t want to put all my array entries in my table.
thanks.