[英]Select box with first option empty
How can I set the first option in my select box to an empty value?如何将选择框中的第一个选项设置为空值?
I'm getting the data from my DB, and I would like to set the option by default as "Please select one option".我正在从我的数据库中获取数据,并且我想将选项默认设置为“请选择一个选项”。
I found that 'default'=>'Please select'
doesn't work with the HTML5 required attribute.我发现'default'=>'Please select'
不适用于 HTML5 required 属性。 This does work:这确实有效:
$listOfValues = [1 => 'Choice 1'];
Form::select('fieldname',[null=>'Please Select'] + $listOfValues);
If you don't like modern PHP syntax,如果你不喜欢现代 PHP 语法,
$listOfValues = array(1 => 'Choice 1');
$listOfValues[null] = 'Please Select';
Form::select('fieldname', $listOfValues);
But the point is to have a label for the null value.但关键是要为空值设置一个标签。
如果您使用LaravelCollective 的 HTML 包,请执行以下操作。
Form::select('size', array('L' => 'Large', 'S' => 'Small'), null, ['placeholder' => 'Pick a size...']);
There are 2 methods to do this:有两种方法可以做到这一点:
{{ Form::select('user', array('default' => 'Please select one option') + $users, 'default') }}
Or或者
<select>
<option selected disabled>Please select one option</option>
@foreach($users as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</select>
For anyone that needs this behavior, this way is working fine:对于需要这种行为的任何人,这种方式都可以正常工作:
Controller:控制器:
$entityArray = Entity::lists('name', 'id');
$entityArray->prepend('Select', 'Select');
View:看法:
{!! Form::select('entity', $entityArray) !!}
This worked for me on Laravel 5.4.这在 Laravel 5.4 上对我有用。
{{ Form::select('agency', $agency, null, [
'placeholder' => 'Please select ...',
'class' => 'form-control'
]) }}
in controller在控制器中
$data['options']=Entity::pluck('name','id')->prepend('Please Select','');
return view('your_view_blade',$data);
in view blade在视图刀片
{!! Form::select('control_name',$options,null,['class'=>'your_class']) !!}
100% result: 100% 结果:
In controller:在控制器中:
$users = App\User::get()->lists('full_name', 'id')->prepend('Select user','');
return view('name of view')->with('users', $users);
In view:鉴于:
{!! Form::select('who', $users, null, ['class' => 'form-control inline']) !!}
I´m using "laravelcollective/html":"^5.3.0" package我正在使用 "laravelcollective/html":"^5.3.0" 包
In laravel 5.2在 laravel 5.2 中
This worked for me这对我有用
{!! Form::select('user', $users, null, array('class'=>'form-control', 'placeholder' => 'Please select')) !!}
as I just add placeholder
and it made the trick因为我只是添加了placeholder
,它就成功了
For a Laravel 5 collection, you may need to convert the collection to array first.对于 Laravel 5 集合,您可能需要先将集合转换为数组。
<?php
$defaultSelection = [''=>'Please Select'];
$users = $defaultSelection + $users->toArray();?>
and apply $users as并将 $users 应用为
{!! Form::select('user', $users); !!}
In Laravel 5.1 i solved it by doing在 Laravel 5.1 我通过做
$categories = [''=>''] + Category::lists('name', 'id')->toArray();
return view('products.create', compact('categories'));
Or或者
$categories = [''=>''] + Category::lists('name', 'id')->all();
return view('products.create', compact('categories'));
In the view在视图中
{!! {!! Form::select('qualification_level', $qualification , old('Qualification_Level'), ['class' => 'form-control select2', 'placeholder' => '' ]) !!} Form::select('qualification_level', $qualification , old('Qualification_Level'), ['class' => 'form-control select2', 'placeholder' => '' ]) !!}
In the Controller $qualification = \App\Qualification::pluck('name','id') ->prepend('Please Select');在控制器中 $qualification = \App\Qualification::pluck('name','id') ->prepend('Please Select');
Adding to Laerte anwser添加到 Laerte anwser
You can do it at the Blade level
just by issuing a command:您只需发出命令即可在Blade level
执行此操作:
{!! Form::select('entity', $entityArray) !!}
You need to manipulate the array before the view您需要在视图之前操作数组
or to be messy you could do it in blade @php tags或者弄乱你可以在刀片@php标签中做到这一点
$users= [null => 'Empty'];
$dbusers= User::pluck('id', 'name');
$users= array_merge($users, $dbusers->toArray());
return view('myview', compact('users'))
and then you can do the following in the view然后您可以在视图中执行以下操作
{{ Form::select('user',$users, ['class' => 'form-control']) }}
{{ Form::select('parent_id', [null=>'Please Select'] + \App\Item::where('1','1')->pluck('name', 'id')->toArray()) }}
{ !! Form::select('country', $country, 'GB', ['id' = > 'country', 'class' = > 'form-control select2me']) !!}
这里$country
是包含许多国家的数组,在这个数组中是由 id "GB"默认选择的"great britain" 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.