我试图为Flatpickr创建一个淘汰赛绑定但没有运气。我尝试从已接受的答案here调整代码,但无济于事。 Flatpickr已初始化,但它似乎没有使用我设置的默认选项,并且绑定的更新部分根本不起作用。
我的代码如下。我会创建一个jsFiddle,但它在工作中被阻止..
ko.bindingHandlers.datetimepicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
var options = allBindingsAccessor().datetimepickerOptions || { dateFormat: 'm-d-Y', enableTime: true };
var $el = $(element);
$(element).flatpickr(options);
//handle the field changing by registering datepicker's changeDate event
ko.utils.registerEventHandler(element, "onChange", function () {
var observable = valueAccessor();
observable($el.val());
});
//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$el.flatpickr("destroy");
});
$el.val(new Date(ko.utils.unwrapObservable(valueAccessor())));
},
update: function (element, valueAccessor, allBindingsAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
var $el = $(element);
// handle json date from microsoft
if (String(value).indexOf('/Date(') == 0) {
value = new Date(parseInt(value.replace(/\/Date\((.*?)\)\//gi, "$1")));
}
var current = new Date($el.val());
if (value - current !== 0) {
$el.flatpickr('setDate', value);
}
}
};
修改
关于我的情况的更多信息。我在顶部有一个带有寻呼机按钮的页面,这些按钮在我的viewmodel中循环显示一个数组。每次我打页时,一个displayedRecord属性被设置为数组中的下一个/ prev项,并且所有编辑字段都绑定到displayedRecord属性。除了那些使用@atitsbest提供给我的datetimepicker绑定之外,这对我的所有输入都适用。
第一次填充datetimepicker字段,但是一旦我翻过它们,该记录的viewmodel属性就会被设置为null。
编辑2
终于有了这个工作。这是最终产品。结束了删除init函数中的subscribe,因为它在尝试将它的observable设置为null时导致错误。还添加了flatpickr wrap功能,以便用户提供自己的清除,切换等按钮。
ko.bindingHandlers.flatpickr = {
init: function (element, valueAccessor, allBindingsAccessor) {
var options = $.extend({
dateFormat: 'm/d/Y H:i',
enableTime: true,
time_24hr: true,
minuteIncrement: 1
}, allBindingsAccessor().flatpickrOptions);
var $el = $(element);
var picker;
if (options.wrap) {
picker = new Flatpickr(element.parentNode, options);
} else {
picker = new Flatpickr(element, options);
}
// Save instance for update method.
$el.data('datetimepickr_inst', picker);
// handle the field changing by registering datepicker's changeDate event
ko.utils.registerEventHandler(element, "change", function () {
valueAccessor()(picker.parseDate($el.val()));
});
// handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$el.flatpickr("destroy");
});
},
update: function (element, valueAccessor, allBindingsAccessor) {
// Get datepickr instance.
var picker = $(element).data('datetimepickr_inst');
picker.setDate(ko.unwrap(valueAccessor()));
}
};
答案 0 :(得分:1)
我创建了jsfiddle并对您的绑定进行了一些更改:
新绑定:
ko.bindingHandlers.datetimepicker = {
init: function(element, valueAccessor, allBindingsAccessor) {
var options = $.extend({
dateFormat: 'm-d-Y H:i',
enableTime: true
}, allBindingsAccessor().datetimepickerOptions),
$el = $(element),
picker = new Flatpickr(element, options),
observable = valueAccessor();
//handle the field changing by registering datepicker's changeDate event
ko.utils.registerEventHandler(element, "change", function() {
observable(picker.parseDate($el.val()));
});
//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$el.flatpickr("destroy");
});
observable.subscribe(function(newVal) {
$el.val(picker.formatDate(options.dateFormat, newVal));
});
picker.setDate(ko.unwrap(observable));
}
};
希望有所帮助。
<强>更新强>
为了应对分页,我更改了绑定。这是更新的jsfiddle,用于测试与分页方案的绑定。 这里是新的绑定代码:
ko.bindingHandlers.datetimepicker = {
init: function(element, valueAccessor, allBindingsAccessor) {
var options = $.extend({
dateFormat: 'm-d-Y H:i',
enableTime: true
},
allBindingsAccessor().datetimepickerOptions),
$el = $(element),
picker = new Flatpickr(element, options);
// Save instance for update method.
$el.data('datetimepickr_inst', picker);
// handle the field changing by registering datepicker's changeDate event
ko.utils.registerEventHandler(element, "change", function() {
valueAccessor()(picker.parseDate($el.val()));
});
// handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$el.flatpickr("destroy");
});
// Update datepicker with new value from observable
valueAccessor().subscribe(newVal => $el.val(picker.formatDate(options.dateFormat, newVal)));
},
update: function(element, valueAccessor, allBindingsAccessor) {
// Get datepickr instance.
var picker = $(element).data('datetimepickr_inst');
picker.setDate(ko.unwrap(valueAccessor()));
}
};
希望有所帮助。