how can i generate a select list with optgroup

I saw the API about CHtml,it’s can generate activeDropDownList & dropDownList,but i want a select list with optgroup,how can i do this?

Thanks!

solved by myself,haha…

in the model,i have a function to get category:




public function getCategoryOptions()

	{

		return array(

			array('id'=>256,'text'=>'TV','group'=>'Electrical'),

			array('id'=>257,'text'=>'Radio','group'=>'Electrical'),

		);

	}



in the _form:




<?php $data = CHtml::listData($model->getCategoryOptions(),'id','text','group'); ?>

<?php echo CHtml::activeDropDownList($model,'category',$data); ?>



it’s work.

I love Yii!

Sorry for my English (I’m from Kazakhstan), but how can I use this code with data from database (in getCategoryOptions())?

just what I need, thanks :)

Hi,

Why not do :

$list = CHtml::listData($model, $valueField, $textField, $groupField)

I think Yii supports many util functions for this .

Thanks

why is this under "Portuguese" forum ??

thanks! you save my time :)

Yes, it works after changing ‘categoly’ to one of the field names in database table. However, we need nested two levels of optgroup. Could anyone help?

Thanks,

Tak

Hello my People.

For example, We Have same part in our project, but one difference exist.

Check our part of project

in _form

<div class="row">

 &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'autoservicetypedetail_id'); ?&gt;


 &lt;?php &#036;urCriteria = array('select'=&gt;'id, name_'.Yii::app()-&gt;language);?&gt;


 &lt;?php &#036;data = CHtml::listData(&#036;model-&gt;getCategoryOptions(),'id','text','group'); ?&gt;


 &lt;?php echo CHtml::activeDropDownList(&#036;model,'autoservicetypedetail_id',&#036;data); ?&gt;               


 &lt;?php echo &#036;form-&gt;error(&#036;model,'autoservicetypedetail_id'); ?&gt;

</div>

in model

public function getCategoryOptions()

{

 &#036;asts = Autoservicetypedetail::model()-&gt;findAll();


 &#036;arr = array();


 foreach (&#036;asts as &#036;ast) :


      &#036;astd = Autoservicetype::model()-&gt;findByPk(&#036;ast['autoservicetype_id']);


      &#036;arr[] = array('id'=&gt;&#036;ast['id'],'text'=&gt;&#036;ast['slug'],'group'=&gt;&#036;astd['slug']);


 endforeach;


            


 return &#036;arr;

}

[size="4"]Best Regards Web Studio: www.iSystems.am Official Russia, Armenia[/size]