将十进制格式添加到淘汰号码数据绑定中 [英] Adding decimal formatting to Knockout number data bindings
本文介绍了将十进制格式添加到淘汰号码数据绑定中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用敲除法在表格中填充数字值:
I am using knockout to populate number values inside a table:
HTML:
<tbody data-bind="foreach: commision">
<tr>
<td>
<span>R </span><span data-bind="text: Amount"></span>
</td>
</tr>...
</tbody>
与数量和佣金有关的Knockout.js部分
Knockout.js part pertaining to amount and commision
var vm = {
commision: ko.observable(),
futurecommision: ko.observable(),
commisionpaid: ko.observable(),
totals: ko.observable(),
commisionPaid: ko.observable(ko.utils.unwrapObservable(ko.mapping.fromJS({
Date: null,
Amount: 0
}))),
...
现在如何显示以十进制格式填充的数字,例如.00? 例如. 10.00
Now how do i show the number being populated in decimal fomat, say .00 ? eg. 10.00
当前仅显示数字,例如10
Currently it is only showing the number, eg 10
推荐答案
您可以考虑编写一个自定义扩展器来处理此问题.
You could look into writing a custom extender to handle this.
基本扩展器是这样的:
ko.extenders.numeric = function(target, precision) {
var result = ko.computed({
read: function() {
return target().toFixed(precision);
},
write: target
});
result.raw = target;
return result;
};
然后将其用于您的可观察对象,例如:
This is then used on your observables like:
var commissionPaid = ko.observable(whatever your value is).extend({numeric: 2});
这篇关于将十进制格式添加到淘汰号码数据绑定中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文