gpt4 book ai didi

javascript - 自定义 Flatpickr Knockout 装订

转载 作者:行者123 更新时间:2023-12-03 05:16:20 26 4
gpt4 key购买 nike

我正在尝试为Flatpickr创建一个 knockout 绑定(bind)但没有运气。我尝试根据已接受的答案 here 调整代码无济于事。 Flatpickr 已初始化,但它似乎没有使用我设置的默认选项,并且绑定(bind)的更新部分根本不起作用。

我的代码如下。我会创建一个 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);
}
}
};

编辑

关于我的情况的更多信息。我的页面顶部有寻呼机按钮,这些按钮在我的 View 模型中的数组中循环。每次我翻页时, displayedRecord 属性都会设置为数组中的下一个/上一个项目,并且所有编辑字段都会绑定(bind)到 displayedRecord 属性。这对于我的所有输入都适用,除了使用 @atitsbest 提供给我的 datetimepicker 绑定(bind)的输入。

第一次填充 datetimepicker 字段,但是一旦我翻阅它们,该记录的 viewmodel 属性就会以某种方式设置为 null。

编辑2

终于可以工作了。这是最终产品。最终删除了 init 函数中的 subscribe,因为它在尝试将其 observable 设置为 null 时导致错误。还添加了 flatpickr 换行功能,供用户提供自己的清除、切换等按钮。

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()));
}
};

最佳答案

我创建了一个jsfiddle并对您的绑定(bind)进行了一些更改:

  • 将事件名称从“onChange”更改为“change”;现在它开火了。
  • 创建了一些默认选项,即使设置了自定义选项;对于默认的dateFormat很有用。
  • 用订阅更新功能取代了“更新”功能可观察到的;.
  • 绑定(bind)值现在始终是日期。

新绑定(bind):

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));
}
};

希望有帮助。

更新

为了应对分页,我更改了绑定(bind)。这是更新后的jsfiddle测试与分页场景的绑定(bind)。这里是新的绑定(bind)代码:

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()));
}
};

希望有帮助。

关于javascript - 自定义 Flatpickr Knockout 装订,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41596915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com