php Laravel Blade 多选选项中的旧值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35611945/
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
Old value in multiple select option in laravel blade
提问by Hymanaroo Ng
Here is my select option
这是我的选择
<select name="recomemded_food[]" value="" style="width:560px;" multiple class="chosen-select" >
<option value="American Black Bear">American Black Bear</option>
<option value="Asiatic Black Bear">Asiatic Black Bear</option>
<option value="Brown Bear">Brown Bear</option>
<option value="Giant Panda">Giant Panda</option>
</select>
And below is my code trying to use the foreach loop to get the array value. but I am receiving the following error:
下面是我尝试使用 foreach 循环获取数组值的代码。但我收到以下错误:
@foreach (explode(',',old('recomemded_food')) as $recomemded_food)
{{$recomemded_food}}
@endforeach
Error Message : explode() expects parameter 2 to be string
错误消息:explode() 期望参数 2 为字符串
采纳答案by Jilson Thomas
If you pass the select values from Controller:
如果您从 Controller 传递选择值:
$recommended_foods = ["American Black Bear",
"Asiatic Black Bear",
"Brown Bear",
"Giant Panda"];
and In the view:
并在视图中:
<select required="required" class="form-control" name="recommended_food">
@foreach ($recommended_foods as $key => $food)
<option value="{{ $food}}" {{ (old("recommended_food") == $food ? "selected":"") }}>{{ $food }}</option>
@endforeach
</select>
回答by Carlos Quinones
After Playing around a bit I came up with this and it seems to work just splendidly
在玩了一会儿之后,我想出了这个,它似乎工作得非常好
<select name="options[]" id="options" class="form-control" multiple>
@foreach($settings->includes->get('optionList') as $option)
<option value="{{ $option->id }}" {{ (collect(old('options'))->contains($option->id)) ? 'selected':'' }}>{{ $option->name }}</option>
@endforeach
</select>
I may be 100% wrong in leveraging the collect function but it works fine on many of my tests. I want to say thank you to MPS as it was your example that really made me try this as when there is no data in "recommended_food" you will pull an error because in_array requires an array and will not work well with null so I then came up with the idea of doing something like this.
我在利用 collect 函数时可能 100% 是错误的,但它在我的许多测试中都运行良好。我想对 MPS 说声谢谢,因为正是你的例子真正让我尝试了这个,因为当“recommended_food”中没有数据时,你会出现错误,因为 in_array 需要一个数组并且不能很好地使用 null 所以我来了有了做这样的事情的想法。
@if (old("options")){{ (in_array($option->id, old("options")) ? "selected":"") }}@endif
inline but man that looks ugly to me so long story short I am using the following instead
内联但对我来说看起来很丑的人长话短说我正在使用以下内容
{{ (collect(old('options'))->contains($option->id)) ? 'selected':'' }}
Hope this helps others!!
希望这对其他人有帮助!!
回答by Saroj
The question was asked for multiple chosen.
该问题被要求选择多项。
Assuming you have a field called designation and you need to choose multiple designation for adding 1 record and the field name is forWhom
假设您有一个名为 designation 的字段,并且您需要选择多个指定来添加 1 条记录,并且字段名称为forWho
During Add
添加期间
You need to add this {{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }}peace of code to your option tag
您需要添加此{{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }}代码和平到您的选项标签
which will look like
看起来像
<select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
<option value="">--- Select ---</option>
@foreach ($desgInfo as $key => $value)
<option value="{{ $key }}"
{{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }} />
{{ $value }}
</option>
@endforeach
</select>
During Edit
编辑期间
You need to add
你需要添加
{{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }}
{{ (in_array($key,$info->forWhom)) ? 'selected' : ''}}
which will look like
看起来像
<select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
<option value="">--- Select ---</option>
@foreach ($desgInfo as $key => $value)
<option value="{{ $key }}"
{{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }}
{{ (in_array($key,$info->forWhom)) ? 'selected' : ''}}
/>
{{ $value }}
</option>
@endforeach
</select>
Select all selected id's in a multiselect dropdown in laravel 5.4 with harvest chosen
在 Laravel 5.4 的多选下拉列表中选择所有选定的 id,并选择收获
Thanks
谢谢
回答by MPS
For me Jilson Thomas's answer did not work for multiple selectso I changed it to:
对我来说,吉尔森·托马斯的回答对多选不起作用,所以我把它改成了:
{{ in_array($food, old("recommended_food")) ? "selected":"") }}
回答by Leonardo Hessel
This is the way I do, very dynamic.
这就是我所做的,非常有活力。
<select id="gender" name="gender">
<option>Select</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
<script>
var currentGender = null;
for(var i=0; i!=document.querySelector("#gender").querySelectorAll("option").length; i++)
{
currentGender = document.querySelector("#gender").querySelectorAll("option")[i];
if(currentGender.getAttribute("value") == "{{ old("gender") }}")
{
currentGender.setAttribute("selected","selected");
}
}
</script>
回答by hamidreza ghanbari
I think the best answer to this question is this below
我认为这个问题的最佳答案是下面这个
@if(count($categories) > 0)
@foreach($categories as $category)
<option value="{{ $category->id }}" {{ (old('lawyer_fields') == null ? '' : in_array($category->id ,old('lawyer_fields')) ? "selected":"") }}>
{{ $category->name }}
</option>
@endforeach
@else
<option disabled>???? ??????</option>
@endif
回答by blamb
I was parsing JSON
data, and was faced with repopulating the multi-select as an array
, and only wanted one table, and wasn't using keys
since this array
was being passed back from the show
method in the controller
, so had to supply the values directly on the template, in my case as a crud resource on the edit.blade.php
template (also on the create
template).
我正在解析JSON
数据,并面临将多选重新填充为array
,并且只想要一个表,并且没有使用,keys
因为这array
是从 中的show
方法传回的controller
,因此必须直接在模板上提供值,在我的情况下作为edit.blade.php
模板上的 crud 资源(也在create
模板上)。
This worked for me, and was the cleanest I could get it on the view.
这对我有用,并且是我能看到的最干净的。
<select id="recommended_food" multiple="multiple" size=3 style='height: 100%;' name="recommended_food[]">
@if ($food->recomemded_food)
{{ ($val = 'Fries')
&& $chosen = in_array($val, $food->recommended_food)
? 'selected':null}}
<option value="{{$val}}" {{$chosen}}>{{$val}}</option>
{{ ($val = 'Hot Dogs')
&& $chosen = in_array($val, $food->recommended_food)
? 'selected':null}}
<option value="{{$val}}" {{$chosen}}>{{$val}}</option>
{{ ($val = 'Hamburgers')
&& $chosen = in_array($val, $food->recommended_food)
? 'selected':null}}
<option value="{{$val}}" {{$chosen}}>{{$val}}</option>
@endif
</select>