Enable the option to deactivate specific dates in the datepicker function

In my Laravel application, I have an online appointment form that includes a "Select Doctor" field and a "datepicker."

For example, if doctor "A" is available at the clinic on Saturday, Monday, and Wednesday, while doctor "B" is available on Sunday, Tuesday, and Thursday. When a patient selects doctor "A" from the drop-down menu, only the dates for Saturday, Monday, and Wednesday should be active in the datepicker calendar, with all other dates disabled.

Although I have successfully deactivated some selected dates in the datepicker calendar, I am facing difficulty disabling dates based on the selected days.

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

Route

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

Controller

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

Calendar https://i.stack.imgur.com/MRc2R.jpg

If you know how to solve this issue, please help. Thank you in advance!

Answer №1

Upon initial observation, it appears to be a situation involving date mismatch. To better understand how the data from the API should be structured, take a look at the example provided below:

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

Answer №2

Each day of the week is assigned a specific number:

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

The controller returns an Array containing the numbers corresponding to the active days (ex : var arr = [1, 2, 3];) for the datepicker.

To update the code in Ajax, modify as follows:

Ajax

var arr = [1, 2, 3] // array of day numbers returned from the controller

$('#appointment_datepicker').datepicker('option', 'beforeShowDay', function(date){
            var day = date.getDay();
            return [(arr.indexOf(day) != -1)];
    });

Now only Monday,Tuesday and Wednesday will be active in the datepicker calendar.

https://i.stack.imgur.com/ubsiQ.jpg

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

After performing a URL rewrite, the Yii Ajax save operation returns a 301 response

I've implemented an inline editor that utilizes AJAX to save content by calling a Yii controller. The process works perfectly fine. However, after shortening my URLs using .htaccess and Yii urlManager, I encountered a 301 response when trying to save ...

Once the page is refreshed, the checkbox should remain in its current state and

I have a challenge with disabling all checkboxes on my page using Angular-Js and JQuery. After clicking on a checkbox, I want to disable all checkboxes but preserve their state after reloading the page. Here is an example of the code snippet: $('# ...

Tips for enhancing the color saturations of cells that overlap in an HTML table

My goal is to enhance the color of overlapping cells in my HTML tables. For instance, when I click on cell 2, the .nextAll(':lt(5)') method will change the class of the next 4 cells. https://i.stack.imgur.com/mwv8x.png Next, clicking on cell 3 ...

Issues with posting incorrect values to ajaxurl when using jQuery's .each() function

Here is a modified version of pointer code that was originally sourced from this link. The original code was broken in the same way as shown here. I have multiple div elements on my web page that are generated using the given code snippet. Upon checking t ...

Challenge with form validation

Currently, I am utilizing the jQuery validation plugin to handle form validation. However, I have encountered an issue when using inline labels. Here is an example: <input type="text" name="myinput" value="Enter your ...."> In this specific scenar ...

I'm having trouble displaying my button after using the jQuery show method on it following it being hidden. What could be causing this issue?

I'm dealing with a situation on my page where 3 buttons trigger different grid sizes when clicked. However, the issue arises when I hide these buttons off-screen using $('.button').hide();. When the user clicks on text <p onclick="diffRes ...

HTML5 input placeholder adapts its size and position dynamically as data is being

During my interaction with the input fields on my bank's website, I noticed that the placeholder text undergoes a unique transformation. It shrinks in size and moves to the upper-left corner of the input field while I am entering information. Unlike t ...

Encountering an issue while attempting to send an image through JavaScript, jQuery, and PHP

I'm currently attempting to upload an image using JavaScript/jQuery, but I am unsure of how to retrieve the image in order to send it to a server (PHP). I have a form containing all the necessary information that I want to save in MySQL, and I use jQu ...

Show individual row information within a bootstrap modal upon clicking the edit button

As a beginner in programming, I am attempting to use bootstrap modal to showcase row data from a mysql table within the modal window. Following the advice found on stackoverflow regarding "Pull information from mysql table to bootstrap modal to edit" didn ...

Utilizing PHP to send arrays through AJAX and JSON

-I am facing a challenge with an array in my PHP file that contains nested arrays. I am trying to pass an integer variable (and may have to handle something different later on). -My objective is to make the PHP file return an array based on the incoming v ...

Choose the list item by identifying the corresponding unordered list

Is there a way to target the second li element within an ul list like this: HTML <ul> <li></li> <ul> <li>This one is what I need to select</li> </ul> </ul> ...

How do I use jQuery to animate a height increase where the added height is applied to the top of the element?

I am facing a simple issue that I need help resolving. The problem involves hidden content that, once expanded, needs to have a height of 99px. When collapsed, the container holding this content section#newsletter is set to be 65px in height. If you want ...

Use PHP to convert a string into an array and assign keys from a separate array

Is there a method to break down a string into an associative array using keys from another array? For instance, if I have the following array: $array = array('firstname' => 'john', 'lastname' => 'smith'); and ...

How to Retrieve a Specific Line with jQuery

Could jQuery be used to retrieve the offset of the first letter in the 5th visual line of a block's content? I am referring to the visual line determined by the browser, not the line seen in the source code. ...

The function $.post(...) is failing to detect the JSON content in the

I am attempting to send a POST request to the server using the following code: var body = { PatientAgeFilter: { CompareOperator: parseInt(self.patientAge()), MoreThanVal: { AgeSpecifier: 0, AgeValue: parseInt(se ...

How can I switch the visibility of two A HREF elements by clicking on one of them?

Let me break it down for you in the simplest way possible. First off, there's this <a href="#" id="PAUSE" class="tubular-pause">Pause</a> and then we have a second one <a href="#" id="PLAY" class="tubular-play">Play</a> Al ...

Permission for resizing images after they are uploaded to the platform

Currently, I am working on a user's profile page where the user can upload their profile picture using a file dialog. However, when the file is moved to my local server's folder, it only receives permissions as 0644. I am looking to resize this ...

The integration of the jQuery library within the server side of a Google Apps Container Bound Script

Can the jQuery library be utilized server-side in a Google Apps Script that is Container Bound to a Doc or Sheet? If so, what steps should be taken? In a previous inquiry on Stack Overflow, I sought guidance on incorporating jQuery into a container-bound ...

Determining the operational duration of a PHP script catering web content

I am currently conducting tests on the performance of various PHP scripts that are responsible for serving web content on my website. However, I have been encountering inconsistent results during these tests. To measure and log the execution time, I save ...

Deactivating the PHP URL does not have any effect on my XAMPP localhost, but it does work on the server

After using the code provided to eliminate php from URLs, I encountered an issue where it works perfectly on my online server but fails to work on XAMPP local host. RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRul ...