Yii Framework Forum: Chtml::dropdownlist Specific Option - Yii Framework Forum

Jump to content

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

Chtml::dropdownlist Specific Option css style Rate Topic: -----

#1 User is online   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,306
  • Joined: 21-February 11
  • Location:Greece

Posted 17 January 2014 - 10:26 AM

Hi

Is there way to style a specific generated option using CHtml::dropDownList ?

I want to achieve something like that

<select name="a_name" id="an_id">
<option value="1" class="marked">label 1</option>
<option value="2">label 2</option>
<option value="3"  class="marked">label 3</option>
<option value="4">label 4</option>
</select>


Thanks
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#2 User is offline   Dave 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 187
  • Joined: 09-October 08

Posted 17 January 2014 - 10:56 AM

According to the code you can set it within the htmlOptions array().

The structure should be like:

 $htmlOptions = array(
   'options' => array(
     '<keyOfTheEntryMostlyThePk>' => array(
       'class' => 'your desired class'
     ');
   )
 );


You can find the entire code in CHtml::listOptions(); Here is an excerpt, take a look at the last few lines starting with "if(isset($options[$key]))"

foreach($listData as $key=>$value)
		{
			if(is_array($value))
			{
				$content.='<optgroup label="'.($raw?$key : self::encode($key))."\">\n";
				$dummy=array('options'=>$options);
				if(isset($htmlOptions['encode']))
					$dummy['encode']=$htmlOptions['encode'];
				$content.=self::listOptions($selection,$value,$dummy);
				$content.='</optgroup>'."\n";
			}
			else
			{
				$attributes=array('value'=>(string)$key,'encode'=>!$raw);
				if(!is_array($selection) && !strcmp($key,$selection) || is_array($selection) && in_array($key,$selection))
					$attributes['selected']='selected';
				if(isset($options[$key]))
					$attributes=array_merge($attributes,$options[$key]);
				$content.=self::tag('option',$attributes,$raw?(string)$value : self::encode((string)$value))."\n";
			}
		}


I did not tested the code, I just took a quick look at the code, so sorry if I missed something :)
0

#3 User is online   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,306
  • Joined: 21-February 11
  • Location:Greece

Posted 20 January 2014 - 05:29 AM

View PostDave, on 17 January 2014 - 10:56 AM, said:

According to the code you can set it within the htmlOptions array().

The structure should be like:

 $htmlOptions = array(
   'options' => array(
     '<keyOfTheEntryMostlyThePk>' => array(
       'class' => 'your desired class'
     ');
   )
 );


You can find the entire code in CHtml::listOptions(); Here is an excerpt, take a look at the last few lines starting with "if(isset($options[$key]))"

foreach($listData as $key=>$value)
		{
			if(is_array($value))
			{
				$content.='<optgroup label="'.($raw?$key : self::encode($key))."\">\n";
				$dummy=array('options'=>$options);
				if(isset($htmlOptions['encode']))
					$dummy['encode']=$htmlOptions['encode'];
				$content.=self::listOptions($selection,$value,$dummy);
				$content.='</optgroup>'."\n";
			}
			else
			{
				$attributes=array('value'=>(string)$key,'encode'=>!$raw);
				if(!is_array($selection) && !strcmp($key,$selection) || is_array($selection) && in_array($key,$selection))
					$attributes['selected']='selected';
				if(isset($options[$key]))
					$attributes=array_merge($attributes,$options[$key]);
				$content.=self::tag('option',$attributes,$raw?(string)$value : self::encode((string)$value))."\n";
			}
		}


I did not tested the code, I just took a quick look at the code, so sorry if I missed something :)


Thanks!

If this is the only Yii way then another way is using pure php to generate html dropdownlist or extends the Chtml::dropdownlist to do this :)
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
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