gpt4 book ai didi

javascript - 我可以在knockout.js中动态更改datetimepicker中的 'required'绑定(bind)吗?

转载 作者:行者123 更新时间:2023-11-28 06:00:39 25 4
gpt4 key购买 nike

我有一个 datetimepicker,根据我的 View 模型中的 model.status 属性,应该需要它。我怎样才能收到这个?

<div class='input-group date' data-bind="dateTimePicker: model.dateClaimClosed, dateTimePickerOptions: { required: lookUpViewValue(model.status) == 'Closed' }">
<input type='text' class="form-control submittable" id="dateClaimClosed" name="dateClaimClosed" placeholder="Select date..." />
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</div>

这是我的绑定(bind)处理程序:

ko.bindingHandlers.dateTimePicker = {
init: function (element, valueAccessor, allBindingsAccessor) {

var options = {};

options.format = "DD/MM/YYYY";

$(element).datetimepicker(options);

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 != false ? 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 options = allBindings().dateTimePickerOptions || {};
var required = options.required || false;

valueAccessor().extend({ required: required });
ko.bindingHandlers.value.update(element, valueAccessor);

var picker = $(element).data("DateTimePicker");
if (picker) {
var koDate = ko.utils.unwrapObservable(valueAccessor());

if (koDate === undefined)
koDate = null;
else
koDate = (typeof (koDate) !== 'object') ? moment(koDate) : koDate;

picker.date(koDate);
}
}
};

最佳答案

找到解决方案:

if (required)
valueAccessor().extend({ required: required });
else
valueAccessor().rules.remove(function (item) {
return item.rule == "required";
});

关于javascript - 我可以在knockout.js中动态更改datetimepicker中的 'required'绑定(bind)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37295095/

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