我有格式爲(YYYY-MM-DDTHH:mm:ss.000)的從我的數據庫讀取的日期時間變量「whenDateTime」。 我想設置一個datetimepicker與這個日期時間,然後能夠操縱它,當然寫回到我的分貝。如何使bootstrap-datetimepicker在Firefox上使用敲除工作
由於某些原因,datetimepicker不會觸發。我哪裏出錯了?
這是我的視圖模型:
function ViewModel() {
var self = this;
self.whenDateTime = ko.observable("2016-04-09T19:24:48.000");
self.viewWhenDateTime = ko.computed({
read: function() {
var mydate = self.whenDateTime();
if (!mydate) {
return null;
} else {
return moment(mydate).format("DD.MM.YYYY HH:mm");
}
},
write: function (newVal) {
var reversed = moment(newVal).format();
self.whenDateTime(reversed);
}
});
}
的bindingHandlers是這樣的:
ko.bindingHandlers.dateTimePicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
//initialize datepicker with some optional options
var options = allBindingsAccessor().dateTimePickerOptions || {};
$(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)) {
if (event.date != null && !(event.date instanceof Date)) {
value(event.date.toDate());
} else {
value(event.date);
}
}
});
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
var picker = $(element).data("DateTimePicker");
if (picker) {
picker.destroy();
}
});
},
update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
var picker = $(element).data("DateTimePicker");
//when the view model is updated, update the widget
if (picker) {
var koDate = ko.utils.unwrapObservable(valueAccessor());
//in case return from server datetime i am get in this form for example /Date(93989393)/ then fomat this
koDate = (typeof (koDate) !== 'object') ? new Date(parseFloat(koDate.replace(/[^0-9]/g, ''))) : koDate;
picker.date(koDate);
}
}
};
小提琴:http://jsfiddle.net/AsleG/v54hnxur/
你的jsfiddle的控制檯有錯誤 –
我看到了這些,但看不到是什麼造成了它們。 –
https://jsfiddle.net/9aLvd3uw/236/ –