I have created a jQuery date range picker. I am able to select the dates ranges and show them in a textbox. Now what I need is, I need to disable certain dates and assign different classes to those dates.
E.g. I must be able to give class A to April 2, class B to April 4 etc.
I have tried the results I got while I googled but nothing helped. What I am trying to do is to pass some dates and classes that should be assigned to those dates. That class should be assigned to those dates plus I must have option to enable or disable those dates.
The next thing I am trying to do is if there is a disabled date in between 2 dates, then that range cannot be selected.
E.g. Suppose I want to select April 2 to April 7 and April 5 is a disabled date.
Then I could be able to select either April 2 to April 4 or April 6 to April 7.
That means only one range should be selectable. Please help to solve this.
Please find the fiddle here:
You have to create two arrays, one that maps the css Class with each Date you want to be disabled class:date, and one that maps the css class with the date you want to give a special class, and then:
beforeShowDay, check if the date is in the disabled dates array, and if you find it, use the associated class and return false, example: [false,"classA"], if not , check in the special array and return the class with true, else just do whatever you are doing right now.onSelect, check if the selected range includes any of the dates in the array and act accordinglyclass:date array, you can add any class:date you desire to be disabled with its own class, and the code will handle itFull working solution here.
Notes:
background-color on the td, it does't show properly, you will have to modify the picker's css for it to doYou need to implement beforeShowDay and onSelect functions as shown in the following demo:
http://jsfiddle.net/salman/H3ra3/
The code may look daunting but it is not. The basics are as follows:
The dateConfig variable
The date information is stored in an associative array in which we store whether a date is selectable AND/OR its CSS class name.
beforeShowDay
Let date be the date for which the function was called
Let date1 be the date inside start date textbox
Let date2 be the date inside end date textbox
date is unselectable then[false, date_class or ""]
date1
date2 and date is between date1 ...date2 then[true, selected_class]
date is less than date1 then[false, date_class or ""] (do not allow second date to be less than first date)date is equal to date1 then[true, selected_class] (allow second date to be same as first)[true, date_class or ""]
onSelect
Let date1 be the date inside start date textbox
Let date2 be the date inside end date textbox
date1 and date2 then
date1 ...date2 then throw an errorIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With