Php 如何在Laravel中设置禁用的选择选项?
在控制器函数中,我提取所有Php 如何在Laravel中设置禁用的选择选项?,php,laravel,laravel-4,blade,Php,Laravel,Laravel 4,Blade,在控制器函数中,我提取所有属性和我已经使用过的属性 所有属性: $attributeNames = array('' => 'Select Attribute Name') + AttributeName::lists('name' , 'id'); $selectedAttributeNames = $xmlDocument->masterInformation->masterAttributes; 已采用的属性: $attributeNames = array('' =
属性
和我已经使用过的属性
所有属性:
$attributeNames = array('' => 'Select Attribute Name') + AttributeName::lists('name' , 'id');
$selectedAttributeNames = $xmlDocument->masterInformation->masterAttributes;
已采用的属性:
$attributeNames = array('' => 'Select Attribute Name') + AttributeName::lists('name' , 'id');
$selectedAttributeNames = $xmlDocument->masterInformation->masterAttributes;
如何将selectedAttributeNames
设置为disable
下面是var\u dump($selectedAttributeNames)
的输出:
不幸的是,Laravel的
Form::select()
helper方法并没有提供一种方法,用于为select的选项构建html
也就是说,你有几种方法可以做到这一点:
首先:您可以创建自己的表单宏。这是一个过于简化的版本
Form::macro('select2', function($name, $list = [], $selected = null, $options = [], $disabled = []) {
$html = '<select name="' . $name . '"';
foreach ($options as $attribute => $value) {
$html .= ' ' . $attribute . '="' . $value . '"';
}
$html .= '">';
foreach ($list as $value => $text) {
$html .= '<option value="' . $value . '"' .
($value == $selected ? ' selected="selected"' : '') .
(in_array($value, $disabled) ? ' disabled="disabled"' : '') . '>' .
$text . '</option>';
}
$html .= '</select>';
return $html;
});
并使视图中的$attributeNames
和$disabled
都可用您可以像这样使用自定义宏
{{ Form::select2('mydropdown', $attributeNames, null, [], $disabled) }}
第二步:您只需从选项数组中删除(例如,使用array\u diff\u key()
{{ Form::select('mydropdown2', array_diff_key($attributeNames, $disabled), null, []) }}
第三:在您的视图中,您可以吐出一个JavaScript数组,其中包含需要禁用的已选择属性,并使用jQuery或vanilla JS在客户端执行其余操作。与您想要的类似?@halfer可能我不知道。这个问题是使用刀片代码,但在我的例子中,我不知道如何使用它,因为所选属性的列表在控制器中。我可以将其发送到视图,但您能帮我构建选择标记吗?我不清楚P@WereWolf-阿尔法好的,让我解释一下。我想创造一些东西。这种观点是有形式的。该表单有一个选择选项。当用户希望再次使用该表单时,select元素应将已选择的选项设置为禁用。您现在收到我的邮件了吗?您能通过var_dump($selectedAttributeNames)显示$selectedAttributeNames
中的内容吗?(selectedAttributeNames)?谢谢您的回答,现在我有点忙,今晚我会检查它,+1这对我很有帮助。行中有一个小的输入错误,用于设置select元素的name属性。它缺少结束双引号,应该是$html='@MattyB Good catch。谢谢