选择第一个选项为空的框

时间:2014-07-23 13:49:42

标签: php laravel drop-down-menu laravel-4 options

有人知道如何在我的选择框中设置第一个清空值的选项吗?

我从我的数据库中获取数据,我想将该选项默认设置为“请选择一个选项”。

15 个答案:

答案 0 :(得分:45)

我发现'default'=>'Please select'无法使用HTML5必需属性。 这确实有效:

$listOfValues = [1 => 'Choice 1'];
Form::select('fieldname',[null=>'Please Select'] + $listOfValues);

如果您不喜欢现代PHP语法,

$listOfValues = array(1 => 'Choice 1');
$listOfValues[null] = 'Please Select';
Form::select('fieldname', $listOfValues);

但关键是要为空值设置标签。

答案 1 :(得分:24)

如果您使用的是HTML package by LaravelCollective,请执行以下操作。

Form::select('size', array('L' => 'Large', 'S' => 'Small'), null, ['placeholder' => 'Pick a size...']);

答案 2 :(得分:19)

有两种方法可以做到这一点:

{{ Form::select('user', array('default' => 'Please select one option') + $users, 'default') }}

<select>
     <option selected disabled>Please select one option</option>
     @foreach($users as $user)
     <option value="{{ $user->id }}">{{ $user->name }}</option>
     @endforeach
</select>

答案 3 :(得分:13)

对于任何需要此行为的人来说,这种方式运行正常:

控制器:

Resource.first.attributes.slice("foo", "bar", "baz")
# with .where
Resource.where(foo: 1).select("foo, bar, baz").map(&:attributes)

查看:

$entityArray = Entity::lists('name', 'id');
$entityArray->prepend('Select', 'Select');

答案 4 :(得分:5)

这对Laravel 5.4来说很有用。

{{ Form::select('agency', $agency, null, [
    'placeholder' => 'Please select ...',
    'class' => 'form-control'
]) }}

答案 5 :(得分:3)

100%结果:

在控制器中:

$users = App\User::get()->lists('full_name', 'id')->prepend('Select user','');
return view('name of view')->with('users', $users);

在视图中:

{!! Form::select('who', $users, null, ['class' => 'form-control inline']) !!}

我正在使用“laravelcollective / html”:“^ 5.3.0”包

答案 6 :(得分:2)

在Laravel 5.1中,我通过

解决了这个问题
$categories = [''=>''] + Category::lists('name', 'id')->toArray();
return view('products.create', compact('categories'));

或者

$categories = [''=>''] + Category::lists('name', 'id')->all();
return view('products.create', compact('categories'));

答案 7 :(得分:1)

对于Laravel 5集合,您可能需要先将集合转换为数组。

<?php
$defaultSelection = [''=>'Please Select'];
$users = $defaultSelection + $users->toArray();?> 

并将$ users用作

{!! Form::select('user', $users); !!}

答案 8 :(得分:1)

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']) !!}

答案 9 :(得分:0)

添加到Laerte anwser

只需发出命令即可在$.ajax({ type: 'POST', url: '@Url.Action("Customer", "temp")', // don't hard code contentType: "application/json;charset=utf-8" data: JSON.stringify({ "ReferenceId": ..... "Address": [{ "AddressLine1": "" ... }]}), .... 执行此操作:

self.navigationController.navigationBar.topItem.title=@"";//make the left bar button title empty.
self.navigationItem.title=strNavigtionTitle;//make the centered navigation bar title.

答案 10 :(得分:0)

您需要在视图之前操作数组

你可以在刀片@php标签

中做到这一点
    $users= [null => 'Empty'];

    $dbusers= User::pluck('id', 'name');

    $users= array_merge($users, $dbusers->toArray());

    return view('myview', compact('users'))

然后您可以在视图中执行以下操作

{{ Form::select('user',$users, ['class' => 'form-control']) }}

答案 11 :(得分:0)

laravel 5.2

这对我有用

{!! Form::select('user', $users, null, array('class'=>'form-control', 'placeholder' => 'Please select')) !!}

因为我只是添加placeholder并且它成功了

答案 12 :(得分:0)

{{ Form::select('parent_id', [null=>'Please Select'] + \App\Item::where('1','1')->pluck('name', 'id')->toArray()) }}

答案 13 :(得分:0)

在视图中

{!!形式:: select('qualification_level',$ qualification,old('Qualification_Level'),['class'=>'form-control select2','placeholder'=>``]))!! }

在控制器中 $ qualification = \ App \ Qualification :: pluck('name','id')-> prepend('Please Select');

答案 14 :(得分:-1)

{ !! Form::select('country', $country, 'GB', ['id' = > 'country', 'class' = > 'form-control select2me']) !!}

此处$country是包含多个国家/地区的数组,此数组中的“英国”由ID “GB”默认选中

相关问题