Yii Framework Forum: how can i generate a select list with optgroup - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

how can i generate a select list with optgroup Rate Topic: ***** 1 Votes

#1 User is offline   holala 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 74
  • Joined: 23-December 09

Posted 26 January 2010 - 07:24 AM

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!
0

#2 User is offline   holala 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 74
  • Joined: 23-December 09

Posted 26 January 2010 - 07:37 AM

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!
4

#3 User is offline   Одиночка Айс 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 25-May 10

Posted 25 May 2010 - 06:24 AM

View Postholala, on 26 January 2010 - 07:37 AM, said:

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())?
Ни любви, ни тоски, ни жалости
0

#4 User is offline   reed1 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 23-June 10

Posted 17 July 2010 - 07:06 PM

View Postholala, on 26 January 2010 - 07:37 AM, said:

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!


just what I need, thanks :)
0

#5 User is offline   thuong02 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 31-March 09
  • Location:Ho Chi Minh, Viet Nam

Posted 17 March 2011 - 02:51 AM

View Postreed1, on 17 July 2010 - 07:06 PM, said:

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
Dinh Thuong
Yahoo: dinhthuong02@yahoo.com
Skype: dinhthuong02
0

#6 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 17 March 2011 - 09:01 AM

why is this under "Portuguese" forum ??
--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#7 User is offline   Aabbid 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 23-February 11

Posted 27 March 2011 - 07:55 AM

thanks! you save my time :)
0

#8 User is offline   takpoli 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 07-March 13

Posted 12 March 2013 - 05:36 PM

View Postholala, on 26 January 2010 - 07:37 AM, said:

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!


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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users