[英]Laravel Form Facade For Select with extra Tag on one option
我在html表單中選擇了這個選項。
<select name="position" id="position" class='position metadata form-control' data-parsley-required data-parsley-required-message="Please select the one position">
<option value="">Select Position</option>
{{ $options = App\Metadata::all()->where('category','position') }}
@foreach($options as $option)
<option value="{{$option->item}}">{{$option->item}}</option>
@endforeach
<option value="1" mytag='position'>Define New</option>
</select>
表格立面就像這樣
$options = App\Metadata::where('category', 'department')->orderBy('item')->pluck('item', 'item');
$options->prepend('Define New', '1');
$options->prepend('Select department', '0');
?>
{!! Form::select('department', $options , null, ['class' => 'department metadata form-control', 'id'=>'department']) !!}
問題是如何添加mytag $ options-> prepend('Define New','1'); 到窗體外觀中選擇的最后一個選項
使用push()
而不是prerend。
推()
push方法將項附加到集合的末尾。
你的代碼:
$options = App\Metadata::where('category', 'department')->orderBy('item')->pluck('item', 'item');
$options->push('Define New', '1');
$options->push('Select department', '0');
?>
{!! Form::select('department', $options , null, ['class' => 'department metadata form-control', 'id'=>'department']) !!}
作為額外的定義變量,在視圖上進行查詢,調用模型或應用復雜的邏輯是一種非常糟糕的做法,應該在Controller本身上完成。
您應該在控制器中執行此操作:
$metaDataOptions = App\MetaData::orderBy('item')->pluck('item');
//This will be the first option
$metaDataOptions->prepend('Select department', '0');
//This will be the last option
$metaDataOptions->push('Define New', '1');
然后使用$metaDataOptions
返回您的視圖:
return view('insert_your_view')->withOptions($metaDataOptions);
並在您的視圖中傳遞$options
到Form::select()
:
{!! Form::select('department', $options , null, ['class' => 'department metadata form-control', 'id'=>'department']) !!}
您需要為Form
定義自己的"type"
元素。 這可以通過使用Form::macro
來實現: https : //laravelcollective.com/docs/5.2/html#custom-macros
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.