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.
The end goal of today's Knockout.js tutorial is to have a reusable binding that can be implemented as follows: When we instantiate the date picker, this regular input field will be replaced with a nice calendar: To create the custom data binding I need to extend the ko.bindingHandlers and create a new datepicker function as follows: My init function has three inputs: Inside the init function I will create the jQuery datepicker with 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: I also sometimes use blur in combination with changeDate. And finally you can also set the date if your observable contains an existing value: Here is the final Knockout js custom data binding that implements the jQuery datepicker: Published on Feb 18, 2020 Tags: JavaScript
| jQuery Tutorial
| datepicker
| Knockout js Tutorial
| data binding
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.
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.
Custom data binding with Knockout js
<input type="text" id="myDateField" name="myDateField" data-bind="datepicker: myDateField"/>
ko.bindingHandlers.datepicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
}
};
Creating the jQuery datepicker or Bootstrap datepicker
//initialize datepicker with some optional options
var options = {
format: 'MM d, yyyy',
autoclose: true
};
$(element).datepicker(options);
//when a user changes the date
ko.utils.registerEventHandler(element, "changeDate", function (event) {
if (event.date !== null && event.date !== undefined)
// do some additional processing
});
$(element).datepicker('setDate', valueAccessor());
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) {
});
}
};
Related Posts
Tutorials
Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.