简体   繁体   English

使用 select 过滤 fullcalendar 事件

[英]Filter fullcalendar events using a select

I have a fullcalendar plugin to store doctor's appointments I want at the beginning to show all appointments, but I want the consulting room asistant can filter beetween doctors to display only its corresponding appointments using an html select我有一个 fullcalendar 插件来存储医生的约会 我想在开始时显示所有约会,但我希望咨询室助理可以使用 html select在医生之间进行过滤以仅显示其相应的约会

<label for="">Filter by doctor:</label>
<select class="form-control col-4 mb-4" name="select_doctor" id="select_doctor">
  <option value="1">Doctor 1</option>
  <option value="2">Doctor 2</option>
  <option value="3">Doctor 3</option>
</select>

this is the callendar definition这是 callendar 定义

var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
  themeSystem: 'bootstrap',
  titleFormat:{
    hour12: true
  },
  locale: 'es',
  bootstrapFontAwesome: false,
  headerToolbar: {
    left: 'prev,next today',
    center: 'title',
    right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
  },
  eventTimeFormat: {
    hour: '2-digit',
    minute: '2-digit',
    hour12: true
  },
  events:"{{ url('/agenda/show/') }}"
});
calendar.render();

this is the ajax call where is supposed to get the filtered data这是应该获取过滤数据的 ajax 调用

$('#select_doctor').change(function(){
  var id_doctor = $('#select_doctor').val();

  $.ajax({
    type:"GET",
    url:"{{url('/agenda/show/')}}"+'/'+id_doctor,
    success:function(response){
  
    },
    error: function (err) {

    }
  });
});

this is the route这是路线

Route::get('/agenda/show/{id?}', 'AppointmentController@show')->name('appointment-show');

this is the controller function这是 controller function

public function show($id){
  if($id != "null"){

    $data['calendar_doctors']=CalendarDoctor::where('doctor_id', $id)->get();

  }else{

    if(Auth::user()->hasRole('doctor')){

      $data['calendar_doctors']=CalendarDoctor::where('doctor_id', Auth::user()->id)->get();
    
    }else{
    
      $data['calendar_doctors']=CalendarDoctor::all();
    
    }

  }
        
  return response()->json($data['calendar_doctors']);
}

I don't know if this is the best option or there is some sort of client-side filtering or an easy way to filter我不知道这是最好的选择还是有某种客户端过滤或简单的过滤方法

This isn't the simplest way to structure your client-side code.这不是构建客户端代码的最简单方法。

As per fullCalendar's events as a function documentation, you can configure the event feed so that you can run a custom AJAX request, so it's easy to structure the URL the way you need it.根据 fullCalendar 的事件作为 function文档,您可以配置事件源,以便您可以运行自定义 AJAX 请求,因此很容易按照您需要的方式构建 URL。 This can be done whenever a request for events is made.只要发出事件请求,就可以执行此操作。

eg in your case:例如在你的情况下:

events: function(fetchInfo, successCallback, failureCallback) {
  var id_doctor = $('#select_doctor').val();

  $.ajax({
    type:"GET",
    url:"{{url('/agenda/show/')}}"+ (id_doctor != "" ? '/' + id_doctor : ""),
  }).done(function(data) {
    successCallback(data); //use the supplied callback function to return the event data to fullCalendar
  }).fail(jqXHR, textStatus, errorThrown) { 
    failureCallback(jqXHR);
  });
}

Then the "change" event handler for your select simply needs to tell the calendar to refresh the events from the server :然后,您的 select 的“更改”事件处理程序只需要告诉日历从服务器刷新事件

$('#select_doctor').change(function() {
  calendar.refetchEvents();
});

You also need to amend your HTML select so there's an "all" option, so that this can be the initial default, and so the user can toggle back to it if they want to:您还需要修改您的 HTML select 以便有一个“全部”选项,以便这可以是初始默认值,因此用户可以在需要时切换回它:

<label for="">Filter by doctor:</label>
<select class="form-control col-4 mb-4" name="select_doctor" id="select_doctor">
  <option value="">-- All --</option>
  <option value="1">Doctor 1</option>
  <option value="2">Doctor 2</option>
  <option value="3">Doctor 3</option>
</select>

You can do like this:你可以这样做:

           eventSources: [

                // your event source
                {
                url: '{{ url("/agenda/show/") }}',
                method: 'GET',
                extraParams: {
                    select_doctor: $('#select_doctor').val(),
                    custom_param2: 'somethingelse'
                },
                failure: function() {
                    alert('there was an error while fetching events!');
                },
                //color: 'yellow',   // a non-ajax option
                //textColor: 'black' // a non-ajax option
                }

                // any other sources...

            ],

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

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