sansamael 2011-07-28 09:24:08 3957次浏览 4条回复 0 0 0

这只是我个人使用的一点心得,希望能抛砖引玉,出现更好的,更详细的帖子来造福我们这些新手。 数据表infoClass_list字段: infoClass_id:类目id, infoClass_fid:父类id(根类的父类id默认为0), infoClass_name:类目名称, infoClass_sort:排序, view中:

<?php  
$this->widget(
    'CTreeView',
    array('persist'=>'cookie',
    'animated'=>'fast',
    'url' => array('ajaxCTree'),
    'htmlOptions'=>array('id'=>'treeview','class'=>'treeview treeview-famfamfam'))
);
?>

controller中:

public function getSonId($id)
{   
  $connection=Yii::app()->db;  
  $command=$connection->createCommand("SELECT * FROM infoClass_list where infoClass_fid=".$id." order by infoClass_sort asc");
  $dataReader=$command->query();
  $rows=$dataReader->readAll();
  return $rows;
}
public function actionAjaxCTree()
{
  if ($_REQUEST['root'] == "source")
  {
    //第一次传值是 source
    $id=0;
  }
  else
  {
    $id = $_REQUEST['root'];
  }
  $tmp = array();
  $row=$this->getSonId($id);
  foreach ($row as $key =>$val){
    $rs=$this->getSonId($val['infoClass_id']);
    if (!empty($rs)){			
      $tmp_ = array(
        "id"=>$val['infoClass_id'],"text"=>"<span>{$val['infoClass_name']}</span>",
        "classes"=>'important',"hasChildren"=>true,
    );
  }else {
    $tmp_ = array(
      "id"=>$val['infoClass_id'],
      "text"=>"<span>{$val['infoClass_name']}</span>",
      "hasChildren"=>false,
    );
  }
  $tmp[] = $tmp_;
}
echo CTreeView::saveDataAsJson($tmp);
}

此代码经测试可用。 新手发帖,写得不好,轻喷-0-:handshake

您需要登录后才可以回复。登录 | 立即注册