简体   繁体   English

Laravel根据另一个选择框填充选择框

[英]Laravel Populate Select Box Based on Another Select Box

I am not sure where even to start with this. 我不知道从哪里开始。 I am using Laravel and LaravelCollective form fields. 我正在使用Laravel和LaravelCollective表单字段。 What I am looking to do is populate a second select box script (containing dates) based off of the selection in the first select box patient containing names. 我要做的是根据包含名称的第一个选择框patient中的选择填充第二个选择框script (包含日期)。 I'm assuming it will involve JavaScript somehow? 我假设它将以某种方式涉及JavaScript?

The Script model contains a patient_id column that links it to the Patient model, so that can be used to filter the script dates to only those that the patient owns. 脚本模型包含一个patient_id列,该列将其链接到Patient模型,因此可用于将脚本日期过滤为仅患者拥有的日期。

Controller 控制者

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

Blade

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

You're gonna have to listen to change event on event the patient select (yes, in javascript). 您将不得不在患者选择的事件上收听change事件(是的,在javascript中)。 There are at least two possibilities here: 这里至少有两种可能性:

  1. Store all the "script" data in a javascript variable and, then populate options in the script select. 将所有“脚本”数据存储在javascript变量中,然后在脚本选择中填充选项。

  2. Render all the possible selects in the .blade file and hide them with display: none . 呈现.blade文件中的所有可能选择,并使用display: none隐藏它们。 Depending on the patient selection you will have to show only one script select. 根据患者的选择,您将只需要显示一个脚本选择。 Be careful not to send them all when submitting the form. 提交表单时请注意不要全部发送给他们。

Well you could also reload the page after selecting, but I doubt you wanna do that. 好吧,您也可以在选择后重新加载页面,但是我怀疑您是否想这样做。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM