0

在我的 Laravel 项目中,我有一个预约表格,患者可以在从下拉列表中选择一个分支后选择医生。我使用 AJAX 来完成功能。

现在,在任何验证失败后,所有恢复的表单值都接受医生。

但我想在验证失败后自动恢复选定的医生。

html表单

<select class="form-control appointment_form_searchField"  id="appointment_branch" name="appointment_branch" style="font-size:.7em;;width: 100%;">
    <option value="">Select Branch</option>

    @if($_SESSION['branch'] != null)

      @foreach($_SESSION['branch'] as $data)    
        <option value="{{ $data->id }}">{{ $data->name }}</option>
      @endforeach

   @endif                              
</select>

<select class="form-control" id="appointment_doctor" name="appointment_doctor" style="font-size:.7em;padding: 0.6500rem .75rem;width: 100%;">
    <option value="">Select Doctor</option>
</select>

AJAX

jQuery(".appointment_form_searchField").change(function() {        
    var branch_id = $("#appointment_branch").val();   
    var token = $('input[name=_token]').val();
    if(branch_id.trim() != '')
    {                
        jQuery.ajax({    
          url:"{{ url('/filter_doctor') }}",
          type: 'GET',
          data: {_token :token, branch_id : branch_id},
          success:function(msg){    
            $('#appointment_doctor option').remove();
            trHTML = '';
            trHTML += "<option value=''>Select Doctor</option>";
            msg.forEach(function(item){    
              trHTML += "<option value='"+item.id+"'>" + inputem.name + "</option>";
            });    
                $('#appointment_doctor').append(trHTML);
            }
        });
    }              
});

控制器

public function filter_doctor(Request $request)
{
    $branch_id = $request->branch_id;

    $query =  DB::table('service_doctors');
    $query->select('doctor_id','inactive_dates_in_this_month');

    if($branch_id != '0')
        $query->where('branch_id','=', $branch_id);

    $result_time = $query->get();
    $array_doctor_id = $result_time->pluck('doctor_id');
    $doctor = Doctor::whereIn('id', $array_doctor_id)->get();
    return $doctor;
}

有人帮忙吗?提前致谢

4

1 回答 1

0

如果你使用的是 Laravel 5,那么你可以做这样的事情。

在您的表格中添加缺少的详细信息约会分支和约会医生,如下所示。

选项从中加载以前发布的Request::old()值,因此您需要首先为appointment_branch'. Also temporary store旧的约会value in医生数据-oldid 选择旧值,以便脚本可以识别哪个是用户提交的旧值,并可以在文档准备好时加载此选择框。

<select class="form-control appointment_form_searchField"  id="appointment_branch" name="appointment_branch" style="font-size:.7em;;width: 100%;">
    <option value="">Select Branch</option>

    @if($_SESSION['branch'] != null)

      @foreach($_SESSION['branch'] as $data)    
        <option value="{{ $data->id }}" {{ Request::old()?(Request::old('appointment_branch')==$data->id?'selected="selected"':''):'' }}>{{ $data->name }}</option>
      @endforeach

   @endif                              
</select>

<select class="form-control" id="appointment_doctor" name="appointment_doctor" style="font-size:.7em;padding: 0.6500rem .75rem;width: 100%;"   data-oldid="{{ Request::old()?Request::old('appointment_doctor'):'' }}">
    <option value="">Select Doctor</option>
</select>

然后像下面这样编写你的脚本。

<script>
    jQuery(document).ready(function($){
        if(jQuery("#appointment_doctor").data('oldid')!='' && typeof(jQuery("#appointment_doctor").data('oldid'))!="undefined"){
            jQuery(".appointment_form_searchField").change();
        }
    });



    jQuery(".appointment_form_searchField").change(function() {        
        var branch_id = $("#appointment_branch").val();   
        var token = $('input[name=_token]').val();

        if(branch_id.trim() != '')
        {                
            jQuery.ajax({    
              url:"{{ url('/filter_doctor') }}",
              type: 'GET',
              data: {_token :token, branch_id : branch_id},
              success:function(msg){    
                $('#appointment_doctor option').remove();
                trHTML = '';
                trHTML += "<option value=''>Select Doctor</option>";
                msg.forEach(function(item){    
                  trHTML += "<option value='"+item.id+"'>" + inputem.name + "</option>";
                });    
                    $('#appointment_doctor').append(trHTML);
                }
                if(jQuery("#appointment_doctor").data('oldid')!='' && typeof(jQuery("#appointment_doctor").data('oldid'))!="undefined"){
                    jQuery('#appointment_doctor').val(jQuery("#appointment_doctor").data('oldid')); //select the old doctor id here
                }
            });
        }              
    });

</script>
于 2020-04-05T06:57:51.053 回答