Laravel,在选择下拉列表的选项中添加不同的html属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28672267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel, add different html attributes to the options in select drop-down Lists
提问by agaezcode
Is there a way to add HTML attributes to an option in a dropdown select with blade Form::select()
?
有没有办法将 HTML 属性添加到带有刀片的下拉选择中的选项Form::select()
?
Well, because I need something like this: (add different CSS classes to the option
tags)
好吧,因为我需要这样的东西:(向option
标签添加不同的 CSS 类)
<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.)
UPDATE(CSS 类名称不应与文本名称相同。css 类来自数据库中的一个表。)
<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.
如果只有一个 CSS 类可以添加到所有这些选项中,我可以简单地使用 jQuery 来实现。但我需要添加不止一个。
PS: I have the classes name stored in an table from the database.
PS:我将类名存储在数据库的表中。
From the docs, I can't see no light. I've looked to the API also.
从文档中,我看不到任何光。我也看过API。
Update 2(Giving some more details of the code)
更新 2(提供代码的更多详细信息)
// 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>
采纳答案by Kirk Beard
The default Form::select()
helper will does not support what you're requesting, but you can add additional Form helpers using a Macro:
默认Form::select()
助手将不支持您的请求,但您可以使用宏添加其他表单助手:
Form::macro('fancySelect', function($name, $list = array(), $selected = null, $options = array())
{
$selected = $this->getValueAttribute($name, $selected);
$options['id'] = $this->getIdAttribute($name, $options);
if ( ! isset($options['name'])) $options['name'] = $name;
$html = array();
foreach ($list as $list_el)
{
$selectedAttribute = $this->getSelectedValue($list_el['value'], $selected);
$option_attr = array('value' => e($list_el['value']), 'selected' => $selectedAttribute, 'class' => $list_el['class']);
$html[] = '<option'.$this->html->attributes($option_attr).'>'.e($list_el['display']).'</option>';
}
$options = $this->html->attributes($options);
$list = implode('', $html);
return "<select{$options}>{$list}</select>";
});
You can use this new method with the additional class
as required:
您可以class
根据需要将此新方法与其他方法一起使用:
$options = [
[
'value' => 'value-1',
'display' => 'display-1',
'class' => 'class-1'
],
[
'value' => 'value-2',
'display' => 'display-2',
'class' => 'class-2'
],
[
'value' => 'value-3',
'display' => 'display-3',
'class' => 'class-3'
],
];
echo Form::fancySelect('fancy-select', $options);
回答by Marek Gralikowski
Creating macro is no longer necessary. From LaravelCollective 5.4 you can use 5th argument with individual attributes for all options.
不再需要创建宏。从 LaravelCollective 5.4 开始,您可以将第 5 个参数与所有选项的各个属性一起使用。
{{ Form::select('name', $list, null, ['id' => 'colored_stuffs'],
[1 => ['color' => 'blue'], 2 => ['color' => 'red'], 3 => ['color' => 'yellow']) }}
You can use it to add custom data-* attributes or standard properties like disabled too.
您可以使用它来添加自定义 data-* 属性或禁用的标准属性。
回答by Milind Anantwar
You can iterate over the option and then use their text to add as class:
您可以遍历该选项,然后使用它们的文本添加为类:
$('#colors option').each(function(){
$(this).addClass($(this).text());
});
回答by Sadikhasan
$colors = array("green","blue","red");
echo '<select id="colors" name="colors">';
foreach($colors as $key => $color)
{
echo '<option value="'.$key+1.'" class="'.$color.'">'.$color.'</option>';
}
echo '</select>';
Update
更新
$('#colors option').each(function(){
color = $(this).text().split("-")[1];
$(this).addClass(color);
});
$('#colors option').each(function(){
color = $(this).text().split("-")[1];
$(this).addClass(color);
});
.blue{
color:blue;
}
.yellow{
color:yellow;
}
.red{
color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="colors" name="colors">
<option value="1">colors-blue</option>
<option value="2">something-red</option>
<option value="3">banana-yellow</option>
</select>
回答by Salman Kashfy
It's easy, just Make Sure to use class as the fourthparamneter
很简单,只要确保使用 class 作为第四个参数
$yourArray = array(); Use {{Form::select('cat_id', $yourArray,'',['class' => 'form-control'])}}
$yourArray = array(); 使用 {{Form::select('cat_id', $yourArray,'',['class' => 'form-control'])}}