带有占位符的 Laravel 数据库选择下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25222924/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:55:36  来源:igfitidea点击:

Laravel database select dropdown with placeholder

phpselectlaravelplaceholder

提问by 001221

Hi I'm populating a select dropdown with clients that exist in my database, however I want a placeholder first e.g. Please select a Client. Does anyone know the syntax?

嗨,我正在使用数据库中存在的客户端填充选择下拉列表,但是我首先想要一个占位符,例如Please select a Client. 有人知道语法吗?

This is what I have so far:

这是我到目前为止:

@if(count($client_options)>0)

    {{ Form::select('client', $client_options , Input::old('client'), array('placeholder' => 'Please select a client', 'id' => 'select_client', 'class' => 'chosen-select select', 'tabindex' => '2', )) }}

@endif 

The placeholder attribute but that doesn't work, does anyone know how? Thanks in advance

占位符属性但不起作用,有谁知道如何?提前致谢

回答by HADI

Assume that you are creating a dropdown list in Laravel form builder. Then the code should be like this -

假设您正在 Laravel 表单构建器中创建一个下拉列表。那么代码应该是这样的——

In controller -

在控制器中 -

$categories = Category::select('id', 'name')->lists('name', 'id')->prepend('Select a category', '')->toArray();

and in view -

并且认为——

{!! Form::select('cat_id', $categories, old('cat_id')) !!}

Tested with Laravel 5.x.

使用 Laravel 5.x 测试。

Or if you have an array like -

或者,如果你有一个像 -

$array = ['1' => 'lorem ipsum', '4' => 'Another text'];

And after passing this array to view -

并通过此数组查看后 -

{!! Form::select('cat_id', $array, old('cat_id')) !!}

There will be no placeholder. If you pass below array -

不会有占位符。如果您通过以下数组 -

$array = ['' => 'Select category', '1' => 'lorem ipsum category', '4' => 'Another category'];

or there is a collection which you want to pass in view to build select/dropdown list then

或者有一个你想要传递的集合来构建选择/下拉列表然后

$array = $collection->prepend('Select a category', '')->toArray();

You need to pass an array in order to build the dropdown list.

您需要传递一个数组才能构建下拉列表。

Note: array_unshiftor array_mergewill not work as expected!

注意:array_unshiftarray_merge不会按预期工作!

回答by Mike

@if(count($client_options)>0)

    $client_options[] = "Please select a Client";

    {{ Form::select('client', $client_options , Input::old('client'), array('placeholder' => 'Please select a client', 'id' => 'select_client', 'class' => 'chosen-select select', 'tabindex' => '2', )) }}

@endif

回答by The Alpha

Just prepare the select in your controller with the placeholder and pass it to the view like:

只需使用占位符在控制器中准备选择并将其传递给视图,如:

$client_options = ['Select a Client'] +  Client::lists('field_name', 'id');

Then in your view you may check if the $client_optionshas more than one item in it, for example:

然后在您的视图中,您可以检查其中是否$client_options包含多个项目,例如:

@if(count($client_options) > 1)
    {{
        Form::select(
            'client',
             $client_options ,
             Input::old('client'),
             array(
                 'id' => 'select_client',
                 'class' => 'chosen-select select',
                 'tabindex' => '2'
             )
        )
    }}
@endif

回答by mcmacerson

If anyone lands here trying to figure out how to set an item as selected, here's a hint.

如果有人来到这里试图弄清楚如何将项目设置为选中状态,这里有一个提示。

The thirdarray element in Form:select sets what item is selected:

Form:select 中的第三个数组元素设置选择的项目:

Form::select('number', [0, 1, 2], 1)

The third element can also be an array for setting multiple items as selected:

第三个元素也可以是一个数组,用于将多个项目设置为选中状态:

Form::select('number', [0, 1, 2], [1,2])

The placeholderis always array element 0. To set the placeholder as selected, make sure the Form:select array element 3 is zero or has zero in the array:

占位符是始终数组元素0。为了设置作为占位符选择,确保形式:选择阵列元件3是零或具有阵列中的零:

Form::select('number', [1, 2, 3], [0,1], ['class' => 'field', 'placeholder' => $var])