当输入一个被解析为已经输入/解析的整数的floatnumber时,敲除绑定不会更新输入。
将输入更改为其他数字或清除输入有效。但我需要它适用于所有人。
非常感谢所有帮助
我也试过试过
.extend({notify: 'always'})
没有运气
第一次输入:11.11 - > 11
第二输入:11.20 - > 11.20
ko.bindingHandlers.integer = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var maxInt = 2147483647;
var bindings = allBindingsAccessor();
var valueUpdate = bindings.valueUpdate;
var defaultValue = null;
var observable;
var accessor = (bindings == undefined ? valueAccessor() : bindings.integer);
if (typeof (accessor) != "function" && typeof (accessor) != "number") {
observable = accessor.value;
defaultValue = accessor.default;
maxInt = accessor.maxValue != null ? accessor.maxValue : maxInt;
} else {
observable = accessor;
}
var interceptor = ko.computed({
read: function () {
var value = ko.isObservable(observable) ? observable() : observable;
if (value === null || value === undefined) return defaultValue;
return BindingHelpers.formatWithThousandSeparators(value);
},
write: function (newValue) {
newValue = newValue.replace(/\s/g, '');
var v = BindingHelpers.reverseFormat(newValue, defaultValue);
v = (v*1).toFixed(0);
if (v > maxInt)
v = maxInt;
observable(v);
}
});
if (element.tagName === 'INPUT')
ko.applyBindingsToNode(element, {
value: interceptor,
valueUpdate: valueUpdate
});
else
ko.applyBindingsToNode(element, {
text: interceptor
});
}
};
self.addEnergy= function () {
self.energy.push({
Source: ko.observable(""),
Target: ko.observable(""),
Effect: ko.observable(0)
.extend({ max: { params: 1500, message: '' } })
.extend({ min: { params: 1, message: ''} }),
Id: ko.observable(""),
Enabled: ko.observable(true)
});
};
//Code emited
<td class="text-center has-feedback" data-bind="css: {'has-error': !Effect.isValid()}">
<input type="text" class="edit form-control text-right" data-bind="integer: Effect, visible: Enabled" />
<label class="read" data-bind="integer: Effect, visible: !Enabled()"></label>
</td>
答案 0 :(得分:1)
尝试将interceptor.notifySubscribers();
添加到写函数的末尾。
var interceptor = ko.computed({
read: function () {
var value = ko.isObservable(observable) ? observable() : observable;
if (value === null || value === undefined) return defaultValue;
return BindingHelpers.formatWithThousandSeparators(value);
},
write: function (newValue) {
newValue = newValue.replace(/\s/g, '');
var v = BindingHelpers.reverseFormat(newValue, defaultValue);
v = (v*1).toFixed(0);
if (v > maxInt)
v = maxInt;
observable(v);
interceptor.notifySubscribers(); //<-- notifies the UI to update even if the value hasn't changed
}
});