Welcome Guest, Not a member yet? Register   Sign In
Style on select option form
#1

[eluser]PauloBr[/eluser]
Can I apply any style on select option to produce something like this?

Code:
<select name="frm_select">
    <option value="value" style="background-color:#FFE4E1">Option</option>
</select>

Currently, I'm use form_dropdown function from helper form.

Thanks,
Paulo.
#2

[eluser]xwero[/eluser]
I think you better add an id attribute to the select an do the option styling using the id.
Code:
// view
form_dropdown('name',$options,'select','id="myselect"');
// css
#myselect option { background-color:#FFE4E1; }
#3

[eluser]PauloBr[/eluser]
Thanks for reply. Wink

But I need individual style, for example:

Code:
<select name="frm_select">
    <option value="value" style="background-color:red">Option</option>
    <option value="value" style="background-color:green">Option</option>
    <option value="value" style="background-color:green">Option</option>
</select>

When green option is unlock and red option is lock, for anything. This options and information from lock or unlock, I have on my database.

Thanks,
Paulo.
#4

[eluser]xwero[/eluser]
You would have some value to define which option is locked or unlocked. I don't think you can't use form_dropdown in your case.
#5

[eluser]PauloBr[/eluser]
I have this in my controller:

Code:
$arr = array();

$query = $this->db->get('table');

foreach($query->result() as $row)
{
    $arr[$row->id] = $row->title;
}

And use:
Code:
form_dropdown('select_name', $arr);

I try do this:
Code:
foreach($query->result() as $row)
{
    // s for unlocked, n for locked
    $bgcolor = ( $row->active == 's' ) ? 'CCFFCC' : 'FFE4E1';
    
    $arr[$row->id . '" style="background-color:#' . $bgcolor] = $row->title;
}

Not so beautiful, but it was almost the expected. Now, if I try use the third parameter for option selected, not work.

Well, thanks for attention. I will try other method for this.
#6

[eluser]xwero[/eluser]
Nice hacking skills Smile

I would do it as follows
Code:
// controller
$arr = array();

$query = $this->db->get('table');

foreach($query->result() as $row)
{
    $bgcolor = ( $row->active == 's' ) ? ' class="unlocked"' : '';
    $arr[] = array($row->id,$bgcolor,$row->title);
}
// view
<select name="myselect" id="myselect">
&lt;?php foreach($arr as $option){ ?&gt;
<option value="&lt;?php echo $option[0]; ?&gt;"&lt;?php echo $option[1]; ?&gt;>&lt;?php echo $option[3]; ?&gt;</option>
&lt;?php } ?&gt;
</select>
// css
#myselect option { background-color: #FFE4E1; }
#myselect option.unlocked { background-color: #CCFFCC; }

I like to see as much html code as possible in the view files and separate the css from the html with ids an classes.




Theme © iAndrew 2016 - Forum software by © MyBB