我将类别和子类别保存在数据库中.我想在这样的CHtml下拉列表中显示它们:
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'));
?>
但它给了我一个空的下拉列表.如何在带有选项组的下拉列表中显示此树视图? (选项组是父类别,选项是sub_categories.
解决方法:
试试这个
echo $form->dropDownList($model,'id',CHtml::listData(SubCat::model()->findAll(),'id', 'name','relation_name.name'),array('prompt'=>'Choose'));
这将在下拉列表中显示父母和子类别
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。