【问题标题】:Select box with first option empty选择第一个选项为空的框
【发布时间】:2014-09-14 17:51:52
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    {{ 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>
    

    【讨论】:

    • “不支持的操作数类型”消息。
    • 当您使用标准列表输出值$levels=Level::all()-&gt;lists('level', 'id'); array_add 像 $levels = array_add($levels, '0', 'Please select'); 一样在末尾添加但找不到任何可以工作的 prepend 时会怎样?
    • @martyn,它返回错误可能是因为您将其组合的“数组”实际上不是数组而是集合。如果使用 L5.1 并在 Eloquent 上使用 lists() 获取数组,L5.1 lists 返回一个集合而不是数组。要使其返回一个数组,您需要使用 all() 方法:$users = [null=&gt;'Select' ] + User::lists('name','id')-&gt;all();。您可以在上述阶段或Form::select() 阶段添加默认选项(但请确保使用all() 方法。进一步阅读:laraveldaily.com/…
    【解决方案2】:

    我发现'default'=&gt;'Please select' 不适用于 HTML5 required 属性。 这确实有效:

    $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);
    

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

    【讨论】:

    • @Ryu_hayabusa PHP 5.4 是 Laravel 4.2 及更高版本的要求。
    • 这里的 $users 是什么?答案不完整:(
    • 这是一个用户列表@Gediminas,它是选择框的键/值数组。在这里,我们只是将 [null=>'Please Select'] 添加到数组的开头。 $users 的价值超出了问题的范围
    • @darkbluesun 我知道,但它应该添加到答案中才能完全完成!感谢您的理解:)
    【解决方案3】:

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

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

    并将 $users 应用为

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

    【讨论】:

      【解决方案4】:
      { !! Form::select('country', $country, 'GB', ['id' = > 'country', 'class' = > 'form-control select2me']) !!}
      

      这里$country 是一个包含多个国家/地区的数组,在这个数组中是"great britain",ID 为"GB",默认情况下会被选中。

      【讨论】:

        【解决方案5】:

        对于需要这种行为的任何人,这种方式都可以正常工作:

        控制器:

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

        查看:

        {!! Form::select('entity', $entityArray) !!}
        

        【讨论】:

        • 添加一个空键会将您的数组键重置为从 0 到 n。如果您这样做了$entityArray-&gt;prepend('Select', null);,那么您的 entityArray 数组的键值将重置为从 0 到 nth。
        【解决方案6】:

        在 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】:

          添加到 Laerte anwser

          您可以在Blade level 执行此操作,只需发出命令:

          {!! Form::select('entity', $entityArray) !!}
          

          【讨论】:

            【解决方案8】:

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

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

            【讨论】:

            • 是的,完美!这是我的应用程序中的一个示例:{{ Form::select('status', $statuses, null, ['placeholder' =&gt; 'Please select ...', 'class' =&gt; 'form-control']) }}
            • 以防万一这绊倒了其他人,“占位符”区分大小写..
            • 太糟糕了,这不会在默认选项上添加disabled 属性。
            • 没关系,我刚遇到这个问题:github.com/LaravelCollective/html/issues/148
            • 占位符是完美的。但是我需要使用一个额外的技巧,以便表单在创建新行时不会出错:尝试获取非对象的属性 'id'... -> {!!Form::select('field', App\Instance::pluck('name', 'id') , old('field', isset($field->id) ? $field->id : null), ['class' => 'form-control ', 'placeholder' => '请选择...'])!!}
            【解决方案9】:

            在控制器中

            $data['options']=Entity::pluck('name','id')->prepend('Please Select','');
            
            return view('your_view_blade',$data);
            

            视图刀片

            {!! Form::select('control_name',$options,null,['class'=>'your_class']) !!}
            

            【讨论】:

              【解决方案10】:

              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" 包

              【讨论】:

                【解决方案11】:

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

                或者要弄乱你可以在刀片@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']) }}
                

                【讨论】:

                  【解决方案12】:

                  在 laravel 5.2 中

                  这对我有用

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

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

                  【讨论】:

                    【解决方案13】:

                    这在 Laravel 5.4 上对我有用。

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

                    【讨论】:

                    • 我认为这是最好的解决方案
                    【解决方案14】:
                    {{ Form::select('parent_id', [null=>'Please Select'] + \App\Item::where('1','1')->pluck('name', 'id')->toArray()) }}
                    

                    【讨论】:

                      【解决方案15】:

                      在视图中

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

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

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 2018-04-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2020-04-27
                        • 1970-01-01
                        • 2018-02-12
                        • 2015-06-10
                        相关资源
                        最近更新 更多