[英]Laravel Populate Select Box Based on Another Select Box
我不知道從哪里開始。 我正在使用Laravel和LaravelCollective表單字段。 我要做的是根據包含名稱的第一個選擇框patient
中的選擇填充第二個選擇框script
(包含日期)。 我假設它將以某種方式涉及JavaScript?
腳本模型包含一個patient_id
列,該列將其鏈接到Patient模型,因此可用於將腳本日期過濾為僅患者擁有的日期。
控制者
$patients_select = Patient::orderBy('last_name', 'asc')
->get()
->pluck('full_name', 'id');
$scripts_select = Script::orderBy('prescribe_date', 'desc')
// ->where('patient_id', $patient_id)
->pluck('prescribe_date', 'id')
->map(function ($date, $key) {
return date('m/d/Y', strtotime($date));
});
刀
<div class="form-group">
{{Form::label('patient', 'Patient')}}
{{Form::select(
'patient',
$patients_select,
null,
['class' => 'form-control', 'placeholder' => 'Select a Patient']
)}}
</div><!-- /form-group -->
<div class="form-group">
{{Form::label('script', 'Script')}}
{{Form::select(
'script',
$scripts_select,
null,
['class' => 'form-control', 'placeholder' => 'Select a Script']
)}}
</div><!-- /form-group -->
您將不得不在患者選擇的事件上收聽change
事件(是的,在javascript中)。 這里至少有兩種可能性:
將所有“腳本”數據存儲在javascript變量中,然后在腳本選擇中填充選項。
呈現.blade
文件中的所有可能選擇,並使用display: none
隱藏它們。 根據患者的選擇,您將只需要顯示一個腳本選擇。 提交表單時請注意不要全部發送給他們。
好吧,您也可以在選擇后重新加載頁面,但是我懷疑您是否想這樣做。 ;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.