以前做项目是有添加过treeview 这个jquery的插件的.
用上yii的treeview后,果然很方便.
但是yii上的treeview缺少了 treeview-famfamfam 这个样式.
补全功能.分享.
public static function saveDataAsHtml($data)
{
$html='';
if(is_array($data))
{
foreach($data as $node)
{
if(!isset($node['text']))
continue;
$id=isset($node['id']) ? (' id="'.$node['id'].'"') : '';
if(isset($node['expanded']))
$css=$node['expanded'] ? 'open' : 'closed';
else
$css='';
if(isset($node['hasChildren']) && $node['hasChildren'])
{
if($css!=='')
$css.=' ';
$css.='hasChildren';
}
//add by biner 2009-9-11 添加文件夹目录
if(isset($node['icon']))
{
$icon = $node['icon'];
}
else
{
$icon = isset($node['children']) ? 'folder' : 'file';
}
$node['text'] = '<span class="'.$icon.'">'.$node['text'].'</span>';
//add end
if($css!=='')
$css=' class="'.$css.'"';
$html.="<li{$id}{$css}>{$node['text']}";
if(isset($node['children']))
{
$html.="\n<ul>\n";
$html.=self::saveDataAsHtml($node['children']);
$html.="</ul>\n";
}
$html.="</li>\n";
}
}
return $html;
}
我是作为后台的导航来使用treeview的.
附带修改过的css一份,可以通过添加class来自定义各个选项上的图标.
<?php
$root = array(
'1'=>'test',
);
$data = array(
'user' => array(
"text"=> "用户中心",
"expanded"=> true,
"classes" => "important",
"children" => array(
'list' => array(
'text'=> CHtml::link('用户列表',array('/user/')),
),
'add' => array(
'text'=> '添加管理员',
),
),
),
'notice' => array(
"text"=> "新闻中心",
"expanded"=> true,
"classes" => "important",
"children" => array(
0 => array(
'text'=> CHtml::link('新闻列表',array('/notice/')),
),
1 => array(
'text'=> CHtml::link('添加新闻',array('/notice/create')),
),
),
),
'notice' => array(
"text"=> "工作计划",
"expanded"=> true,
"classes" => "important",
"children" => array(
0 => array(
'text'=> CHtml::link('计划列表',array('/plan/')),
),
1 => array(
'text'=> CHtml::link('我的工作计划',array('/plan/admin','uid'=>Yii::app()->user->id)),
),
),
),
'rbac' => array(
"text"=> "RBAC",
"expanded"=> true,
"classes" => "important",
"children" => array(
'list' => array(
'text'=> CHtml::link('用户列表',array('/srbac/')),
),
'manage' => array(
'text'=> CHtml::link('管理',array('/srbac/authItem/manage/')),
),
),
),
);
$this->widget('CTreeView',array('persist'=>'cookie','data'=>$data,'animated'=>'fast','htmlOptions'=>array('id'=>'treeview','class'=>'filetree treeview-famfamfam')));
?>