Knockout JS中的数字输入框 [英] Number input box in Knockout JS
问题描述
我正在尝试创建一个只接受数字的数字输入框。
I'm trying to create a number input box which will accept numbers only.
我的初始值方法是替换值并将其重新设置为自身。
My initial value approach was to replace value and set it again to itself.
function vm(){
var self = this;
self.num = ko.observable();
self.num.subscribe(function(newValue){
var numReg = /^[0-9]$/;
var nonNumChar = /[^0-9]/g;
if(!numReg.test(newValue)){
self.num(newValue.toString().replace(nonNumChar, ''));
}
})
}
ko.applyBindings(new vm())
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<input type="text" data-bind="textInput: num" />
现在这种方法有效,但会添加另一个订阅事件周期,所以我尝试使用自定义绑定,这样我才能返回更新的值。新的,我尝试了一些但不知道该怎么做。以下是我的尝试,但它不起作用。它甚至没有更新observable。
Now this approach works but will add another cycle of subscribe event, so I tried to use a custom binding so that I can return updated value only. New to it, I tried something but not sure how to do it. Following is my attempt but its not working. Its not even updating the observable.
ko.bindingHandlers.numeric_value = {
update: function(element, valueAccessor, allBindingsAccessor) {
console.log(element, valueAccessor, allBindingsAccessor())
ko.bindingHandlers.value.update(element, function() {
var value = ko.utils.unwrapObservable(valueAccessor());
return value.replace(/[^0-9]/g, '')
});
},
};
function vm() {
this.num = ko.observable(0);
this.num.subscribe(function(n) {
console.log(n);
})
}
ko.applyBindings(new vm())
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div>
<input type="text" data-bind="number_value: num, valueUpdate:'keyup'">
<span data-bind="text: num"></span>
</div>
所以我的问题是,我们可以使用自定义绑定做到这一点吗?它是比订阅更好的方法吗?
So my question is, Can we do this using custom bindings and is it better approach than subscribe one?
根据 @ user3297291 的答案, ko.extenders
看起来更像是我的订阅方式的通用方式。我正在寻找一种方法(如果可能在Knockout中),它会在设置为可观察之前清除值。
As per @user3297291's answer, ko.extenders
looks more like a generic way for my subscribe approach. I'm looking for an approach (if possible in Knockout), which would clean value before it is set to observable.
我参考了以下文章:
- How to update/filter the underlying observable value using a custom binding?
- How can i update a observable in custom bindings?
注意:在第一个示例中,他们使用jQuery来设置值。我想避免它,只使用淘汰赛
Note: In the first example, they are using jQuery to set the value. I would like to avoid it and do it using knockout only
推荐答案
我赞成使用扩展器user3297291 的aswer。
I´m on favor of use extender as user3297291's aswer.
扩展器是格式化或验证可观察量的灵活方式,以及更多可重复使用。
Extenders are a flexible way to format or validate observables, and more reusable.
这是我对数字扩展器的实现
Here is my implementation for numeric extender
//Extender
ko.extenders.numeric = function(target, options) {
//create a writable computed observable to intercept writes to our observable
var result = ko.pureComputed({
read: target, //always return the original observables value
write: function(newValue) {
var newValueAsNum = options.decimals ? parseFloat(newValue) : parseInt(newValue);
var valueToWrite = isNaN(newValueAsNum) ? options.defaultValue : newValueAsNum;
target(valueToWrite);
}
}).extend({
notify: 'always'
});
//initialize with current value to make sure it is rounded appropriately
result(target());
//return the new computed observable
return result;
};
//View Model
var vm = {
Product: ko.observable(),
Price: ko.observable().extend({
numeric: {
decimals: 2,
defaultValue: undefined
}
}),
Quantity: ko.observable().extend({
numeric: {
decimals: 0,
defaultValue: 0
}
})
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
编辑
我明白你的观点,正则表达式自定义绑定怎样才能使它更具可重用性?
I get your point, what about and regular expression custom binding to make it more reusable?
这样的东西。
function regExReplace(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var observable = valueAccessor();
var textToReplace = allBindingsAccessor().textToReplace || '';
var pattern = allBindingsAccessor().pattern || '';
var flags = allBindingsAccessor().flags;
var text = ko.utils.unwrapObservable(valueAccessor());
if (!text) return;
var textReplaced = text.replace(new RegExp(pattern, flags), textToReplace);
observable(textReplaced);
}
ko.bindingHandlers.regExReplace = {
init: regExReplace,
update: regExReplace
}
ko.applyBindings({
name: ko.observable(),
num: ko.observable()
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<input type="text" data-bind="textInput : name, regExReplace:name, pattern:'(^[^a-zA-Z]*)|(\\W)',flags:'g'" placeholder="Enter a valid name" />
<span data-bind="text : name"></span>
<br/>
<input class=" form-control " type="text " data-bind="textInput : num, regExReplace:num, pattern: '[^0-9]',flags: 'g' " placeholder="Enter a number " />
<span data-bind="text : num"></span>
这篇关于Knockout JS中的数字输入框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!