Laravel 选项选择 - 默认问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26990844/
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
Laravel Option Select - Default Issue
提问by SA__
Here is my Select Box, Here All Company will be loaded,
这是我的选择框,这里将加载所有公司,
But i want to display the Particular company as default selectedwhich i have it in session.
但我想将特定公司显示为我在会话中选择的默认公司。
Here is my Code
这是我的代码
$sessioncompany = 'ABCcompany'
$comp[''] = 'Company';
@foreach($company_list as $row)
<?php
$comp[$row->CompanyID] = $row->CompanyName;
?>
@endforeach
{{ Form::select('CompanyID', $comp, '', array('id' => 'seCompanyID')); }}
What i tried is
我试过的是
{{ Form::select('CompanyID', $comp, '', array('id' => 'seCompanyID'), array(id=>$sessioncompany)); }}
But i ended with failure
但我以失败告终
What is the mistake i am doing and How can i fix that ?
我在做什么错误,我该如何解决?
Note : I want it in the native laravel method
注意:我希望它在本地 Laravel 方法中
回答by Jarek Tkaczyk
$selectedId = ... // get the id from the session
{{ Form::select('CompanyID', $comp, array($selectedId), array('id' => 'seCompanyID')) }}
回答by alexrussell
The Form::select()
method has the following parameters:
该Form::select()
方法具有以下参数:
$name
- thename
HTML attribute(array) $list
- the list of options, key-value pairs for actual value => displayed value$selected
- the selected item's value(array) $options
- any extra HTML attributes, as per other Form/HTML helper methods
$name
-name
HTML 属性(array) $list
- 选项列表,实际值的键值对 => 显示值$selected
- 所选项目的值(array) $options
- 任何额外的 HTML 属性,如其他 Form/HTML 辅助方法
So, the third parameter $selected
is the one you want to pass your session value to. Copying your example and replacing:
因此,第三个参数$selected
是您要将会话值传递给的参数。复制您的示例并替换:
{{ Form::select('CompanyID', $comp, Session::get('my_session_var'), array('id' => 'seCompanyID')); }}
Where 'my_session_var'
is the name of the session variable you store the value of the item that should be selected by default.
'my_session_var'
会话变量的名称在哪里存储默认情况下应该选择的项目的值。
The great thing about using Laravel's Form helpers is that you get the 'old' input for free. For example, if you set a default here and the user changes it, submits the form and there's a validation error. When the form is redisplayed (assuming you redirected back with return Redirect::back()->withInput();
) the value the user selected, rather than your explicit default will be set.
使用 Laravel 的 Form 助手的好处是你可以免费获得“旧”输入。例如,如果您在此处设置默认值并且用户对其进行更改,则提交表单时会出现验证错误。当表单重新显示时(假设您使用 重定向回来return Redirect::back()->withInput();
),将设置用户选择的值,而不是您的显式默认值。
(Thanks to @JarekTkaczyk for putting me right on the old input thing.)
(感谢@JarekTkaczyk 把我放在旧的输入上。)
回答by Riad
You are passing wrong parameters. max parameters should be 4. Try this:
您正在传递错误的参数。最大参数应该是 4。试试这个:
{{ Form::select('CompanyID', array('default' => $sessioncompany)+$comp, 'default') }}
回答by TeeJay
Just beware, if you're on a page with the form with the select box, make sure that with each page refresh while testing you do a full page refresh without using cache - Ctrl + Shift + R
, otherwise the last selected value may remain selected.
请注意,如果您在带有选择框的表单的页面上,请确保在测试时每次刷新页面时都进行整页刷新而不使用缓存 - Ctrl + Shift + R
,否则最后选择的值可能会保持选中状态。