我有保存在數據庫中的類別和子類別。我想向他們展示在了CHtml下拉列表如下:Yii在數據庫的下拉列表中顯示treeview
Patrent_cat
sub_cat1
sub_cat2
Parent_cat2
...
我的類別表是這樣的
id name parent_id
和PARENT_ID爲0如果元組是
我已經嘗試過父母本身這在我的分類模型:
public function relations()
{
return array(
'getparent' => array(self::BELONGS_TO, 'Category', 'parent_id'),
'childs' => array(self::HAS_MANY, 'Category', 'parent_id', 'order' => 'id ASC'),
);
}
public function getCategoryTree()
{
$subitems = array();
if($this->childs) foreach($this->childs as $child)
{
$subitems[] = $child->getListed();
}
$returnarray = array($this->id => $this->title);
if($subitems != array())
$returnarray = array_merge($returnarray, array('items' => $subitems));
return $returnarray;
}
在我看來:
<?php
echo CHtml::dropDownList('category', 'id',
Category::model()->CategoryTree,
array('empty' => '(Select a category'));
?>
,但它給了我一個空的下拉菜單。如何在帶選項組的下拉列表中顯示此樹形視圖? (該選項組是父類和選項 - 行業標準。
的'getCategoryTree'功能將無法使用初始化'$ this'當你用'static'模型'$ this'調用它會'null' –