I am using this Bootstrap DateTime Picker: http://eonasdan.github.io/bootstrap-datetimepicker/
I have created a Custom Binder for DateTime called datepicker:
ko.bindingHandlers.datepicker = {
init: function(element, valueAccessor, allBindingsAccessor) {
//initialize datepicker with some optional options
var options = allBindingsAccessor().datepickerOptions || {format: 'DD/MM/YYYY HH:mm'};
$(element).datetimepicker(options);
//when a user changes the date, update the view model
ko.utils.registerEventHandler(element, "dp.change", function(event) {
var value = valueAccessor();
if (ko.isObservable(value)) {
value(event.date);
}
});
},
update: function(element, valueAccessor) {
var widget = $(element).data("datepicker");
//when the view model is updated, update the widget
if (widget) {
widget.date = ko.utils.unwrapObservable(valueAccessor());
if (widget.date) {
widget.setValue();
}
}
}
};
And I My Model is in JSON, as I am converting C# dateTime to JSON I get this Date: "/Date(1427368178393)/"
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Quote Date</label>
<div class='input-group date dateTimes'>
<input type="text" class="form-control" data-bind="datepicker: QuoteDateTime" />
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
</div>
</div>
</div>
But the Problem is that the first time the Page Loads even though the QuoteDateTime has a value it is not showing in the DateTimePicker.
Here is the JSFiddle:
https://jsfiddle.net/mdawood1991/ezv7qxbt/
I need the First Value to Show in the DateTime Picker.
EDIT
The thing is that my ViewModel has a List of Accommodations
This is the Console Log of my viewModel
I could convert every dateTime to a moment as you suggester, but then I shouldn't use the knockout Mapping Plugin: http://knockoutjs.com/documentation/plugins-mapping.html
Is this the only solution to loop through my viewModel and then converting alld Datetimes to a moment object
I think you need to use the default date option.
Updated JSFiddle: https://jsfiddle.net/3jqL8s1m/
Your update function doesn't work at all. Making it work will fix your issue and cause updates to the observable to be reflected in the control.
$(element).data("DateTimePicker")
date()
function.Code:
Edit -- Option 2: Keep everything as a moment.js object
An issue with your code however is that a string is passed into the observable, but whenever this custom binding updates that observable it makes it a moment.js object (
input -> string
andoutput -> Moment
). To fix this, I would recommend having the user pass in a moment.js object to the observable (input -> Moment
andoutput -> Moment
).First, convert your c# string to a moment.js object when you fill your view model:
Second, change your update function to handle moment.js objects instead of strings:
You can see this working just fine in this jsfiddle:
https://jsfiddle.net/ezv7qxbt/21/
After help From @David and @Bryant I changed my code to update viewModel with moment object date.
The Problem was that the Date was only posting back correctly after selecting a Date from the DateTime Picker. So I changed code so when Initialized I update the viewModel. And it is working now.
This is the DateTime Picker
http://eonasdan.github.io/bootstrap-datetimepicker/
I used the following datepicker binding.
JSFiddle Working: http://jsfiddle.net/mdawood1991/cL9k2sbe/
My Solution: