我有一个datetimepicker,这取决于我的视图模型中的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>
这是我的bindingHandler:
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 - 我可以动态地在敲门声中更改datetimepicker中的“必需”绑定(bind)吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37295095/