Yii使用optgroup的活跃下拉列表 [英] Yii active dropdownlist with optgroup

查看:18
本文介绍了Yii使用optgroup的活跃下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用listData和activeDropDownList与Yii创建下拉列表.

I try to create dropdown-list with Yii, using listData and activeDropDownList.

我使用网络上的示例,但它拒绝为我创建optgroup.

I use the examples found on the web, but it refuses to create the optgroups for me.

$data = CHtml::listData(MyModel::model()->getEntries(0), 'id', 'text', 'group');

生成预期的数组:

Array([group1] => Array([10]=>FirstEntry, [20]=>SecondEntry),
 [group2]=>Array([30]=>firstEntryGroup2, [40]=>firstEntryGroup2)) 

以此类推.所以这是一个充满子数组的关联数组...

And so on. So it's an associative array filled with sub-arrays...

但是当我使用

echo CHtml::activeDropDownList($model, 'dropdownName', $data);

我得到的只是一个没有optgroup的固定下拉列表.只是子数组中的条目...

All I get is a flat dropdown without the optgroups. Just the entries from the sub-arrays...

Yii 1.1.6(我阅读了一些有关安全属性的内容,并试图实现它,但没有成功...

Yii 1.1.6 (I read something about safe-attributes and tried to implement it, but with no success...

推荐答案

下面的旧答案不正确.对不起.您可以在下拉数据数组中使用"group"属性创建optgroup.

The old answer below is incorrect. Sorry. You create optgroups using a 'group' attribute in your drop down data array.

<代码>大批(array('id'=> 256,'text'=>'TV','group'=>'Electrical'),array('id'=> 257,'text'=>'Radio','group'=>'Electrical'),);

旧答案:

这里有一个伟大的要旨,展示了如何创建自己想要的东西: https://gist.github.com/StandardNerd/2909111

There is a great gist here that shows how do create what you're after: https://gist.github.com/StandardNerd/2909111

<代码><?php echo CHtml :: dropDownList('Cars','car_id',array('Mazda'=> array('mazda-rx7'=>'RX7','mazda-rx5'=>'RX5',),'Volvo'=> array('volvo-b9tl'=>'B9TL','volvo-l90e-radlader'=>'L90E Radlader',),));?>

您当前使用的是activeDropDownList,它应该有所不同,因为您添加了$ model变量而不是"Cars"并调整了功能

You're currently using an activeDropDownList which should only be different because you add your $model variable instead of 'Cars' and tweak the function

这篇关于Yii使用optgroup的活跃下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆