Is there a way to add HTML attributes to an option in a dropdown select with blade Form::select()
?
Well, because I need something like this: (add different CSS classes to the option
tags)
<select id="colors" name="colors">
<option value="1" class="blue">blue</option>
<option value="2" class="red">red</option>
<option value="3" class="yellow">yellow</option>
</select>
UPDATE (The CSS class name should not be the same as the text name. The css classes comes from a table in the database.)
<select id="colors" name="colors">
<option value="1" class="blue">colors-blue</option>
<option value="2" class="red">something-red</option>
<option value="3" class="yellow">banana is yellow</option>
</select>
If it was just one CSS class to add to all those options, I could simply do that with jQuery. But I need to add more than one.
PS: I have the classes name stored in an table from the database.
From the docs, I can't see no light. I've looked to the API also.
Update 2 (Giving some more details of the code)
// In my create view I have this:
{{ Form::select( 'colored_stuffs', $colorsList, null, ['id'=>'colored_stuffs'] ) }}
// The $colorsList generate an array in the ColorsController@create
public function getCreate()
{
$colorsList = $this->colors->listAll();
}
// listAll() is defined here is this repository
public function listAll()
{
$colors = $this->model->lists('name', 'id', 'color_class');
return $colors;
}
// the HTML optput of the create view it's this
<select id="colored_stuffs" name="colored_stuffs">
<option value="1">Red is used to alert something</option>
<option value="2">A banana is yellow</option>
<option value="3">Sorry no color here</option>
</select>
// But I want this
<select id="colored_stuffs" name="colored_stuffs">
<option value="1" class="red">Red is used to alert something</option>
<option value="2" class="light-yellow">A banana is yellow</option>
<option value="3" class="black">Sorry no color here</option>
</select>
Creating macro is no longer necessary. From LaravelCollective 5.4 you can use 5th argument with individual attributes for all options.
You can use it to add custom data-* attributes or standard properties like disabled too.
It's easy, just Make Sure to use class as the fourth paramneter
$yourArray = array(); Use {{Form::select('cat_id', $yourArray,'',['class' => 'form-control'])}}
Update
The default
Form::select()
helper will does not support what you're requesting, but you can add additional Form helpers using a Macro:You can use this new method with the additional
class
as required:You can iterate over the option and then use their text to add as class: