在日期选择器中禁用所选日期的日期

时间:2020-05-09 17:05:45

标签: php jquery laravel laravel-5 datepicker

我的Laravel应用程序中有一个在线约会表格,其中有一些提交的Select Doctordatepicker

假设医生A将于星期六,星期一和星期三在诊所就诊。医生B将在星期日,星期二和星期四上班。因此,当任何患者从A字段中选择医生Select Doctor时,将在datepicker日历中激活星期六,星期一和星期三的所有日期,而其他日期将被停用。

我已经停用了datepicker日历的某些选定日期。但是不能在datepicker中的选定日期禁用日期。

html

<input class="form-control" id="appointment_datepicker" name="appointment_datepicker" type="text" placeholder="Select Date" value="{{ $user->appointment_datepicker or old('appointment_datepicker') }}">

ajax

    $.ajax({

      url:"{{ url('/filter_available_date') }}",
      type: 'GET',
      data: {_token :token, branch_id_appointment : branch_id_appointment, doctor_id_appointment : doctor_id_appointment},
      success:function(msg){

        $('#appointment_datepicker').datepicker('option', 'beforeShowDay', function(date){
                var day = jQuery.datepicker.formatDate('yy-mm-dd', date);
                return [ msg.indexOf(day) == -1 ] //here msg is the Array of selected Dates which are activated in the datepicker calendar
            });                    
    }
});

路线

Route::get('/filter_available_date','frontendAppointmentController@filter_available_date');

控制器

public function filter_available_date(Request $request)
{
    $branch_id = $request->branch_id_appointment;
    $doctor_id = $request->doctor_id_appointment;    
    $query =  DB::table('service_doctors');
    $query->select('doctor_id','inactive_dates_in_this_month');    
    if($branch_id != '0')
        $query->where('branch_id','=', $branch_id);

    if($doctor_id != NULL)
        $query->where('doctor_id','=', $doctor_id);

    $result_time = $query->get();    
    $result = [$result_time[0]->inactive_dates_in_this_month];    
    $final_result= explode(',',$result[0]);    
    return $final_result;
}

日历 enter image description here

如何解决此问题?有人帮忙吗?预先感谢。

2 个答案:

答案 0 :(得分:2)

乍一看,似乎是日期不匹配的问题。如果使用下面的示例,则可以看到来自API的数据的外观。

var msg = ["2020-05-11"];
$('#appointment_datepicker').datepicker({
  beforeShowDay: function(date) {
    var day = jQuery.datepicker.formatDate('yy-mm-dd', date);
    return [msg.indexOf(day) == -1]
  }
});
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js" integrity="sha256-eGE6blurk5sHj+rmkfsGYeKyZx3M4bG+ZlFyA7Kns7E=" crossorigin="anonymous"></script>
<input class="form-control" id="appointment_datepicker" name="appointment_datepicker" type="text" placeholder="Select Date">

答案 1 :(得分:0)

这里每天都有一个固定电话号码,例如

Sunday = 0
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6 

因此,一天的Array天( ex:var arr = [1、2、3]; )从控制器返回,并且该天将在{{1 }}。

只需在Ajax中更改以下代码

Ajax

datepicker

因此,var arr = [1, 2, 3] //these are the array of days numbers return from controller $('#appointment_datepicker').datepicker('option', 'beforeShowDay', function(date){ var day = date.getDay(); return [(arr.indexOf(day) != -1)]; }); 日历中仅MondayTuesdayWednesday处于活动状态。

enter image description here