jQuery Datepicker with a Knockout js custom data binding jQuery Datepicker with a Knockout js custom data binding

Whether you are using the Bootstrap datepicker with a JavaScript array or the jQuery datepicker, they both use the same underlying JavaScript library. So with today's example, let's explore implementing the jQuery library with an added bonus of creating a Knockout.js custom binding.

The end goal of this custom data bind jQuery tutorial is to create a form input field data bound not to the standard value binding, but instead to the custom one appropriately named: datepicker.


Custom data binding with Knockout js

The end goal of today's Knockout.js tutorial is to have a reusable binding that can be implemented as follows:


<input type="text" id="myDateField" name="myDateField" data-bind="datepicker: myDateField"/>

When we instantiate the date picker, this regular input field will be replaced with a nice calendar:

jquery datepicker

To create the custom data binding I need to extend the ko.bindingHandlers and create a new datepicker function as follows:


ko.bindingHandlers.datepicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
}
};

My init function has three inputs:

  1. element: This is the jquery HTML template of the form input element.
  2. valueAccessor: This is the observable variable that will be set when a user picks a date.
  3. allBindingsAccessor: This is an object that contain additional options to pass to the custom data binding. This is useful to create the jQuery datepicker with custom options.

Creating the jQuery datepicker or Bootstrap datepicker

Inside the init function I will create the jQuery datepicker with options:


//initialize datepicker with some optional options
var options = {
format: 'MM d, yyyy',
autoclose: true
};
$(element).datepicker(options);

Of course you can update the options with any values the jQuery datepicker supports. All of the code will also work seamlessly with the Bootstrap datepicker.

On occasion you may wish to apply custom logic when the user selects a date. I accomplish this by using ko.utils.registerEventHandler to listen for the event changeDate as follows:


//when a user changes the date
ko.utils.registerEventHandler(element, "changeDate", function (event) {
if (event.date !== null && event.date !== undefined)
// do some additional processing
});

I also sometimes use blur in combination with changeDate. And finally you can also set the date if your observable contains an existing value:


$(element).datepicker('setDate', valueAccessor());

Here is the final Knockout js custom data binding that implements the jQuery datepicker:


ko.bindingHandlers.datepicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
//initialize datepicker with some optional options
var options = {
format: 'MM d, yyyy',
autoclose: true
};
$(element).datepicker(options);
$(element).datepicker('setDate', valueAccessor());
//when a user changes the date
ko.utils.registerEventHandler(element, "changeDate", function (event) {
});
ko.utils.registerEventHandler(element, "blur", function(event) {
});
}
};

Published on Feb 18, 2020

Tags: JavaScript | jQuery Tutorial | datepicker | Knockout js Tutorial | data binding

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.