gpt4 book ai didi

knockout.js - 向 Knockout 数字数据绑定(bind)添加十进制格式

转载 作者:行者123 更新时间:2023-12-04 00:06:06 24 4
gpt4 key购买 nike

我正在使用 knockout 来填充表格中的数值:

HTML:

<tbody data-bind="foreach: commision">
<tr>
<td>
<span>R </span><span data-bind="text: Amount"></span>
</td>
</tr>...
</tbody>

Knockout.js 部分涉及金额和佣金
 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

目前它只显示数字,例如 10

最佳答案

看看这个 KO Money Extender:

http://jsfiddle.net/digitalbush/R6MPU/

(function(){
var format = function(value) {
toks = value.toFixed(2).replace('-', '').split('.');
var display = '$' + $.map(toks[0].split('').reverse(), function(elm, i) {
return [(i % 3 === 0 && i > 0 ? ',' : ''), elm];
}).reverse().join('') + '.' + toks[1];

return value < 0 ? '-' + display : display;
};

ko.subscribable.fn.money = function() {
var target = this;

var writeTarget = function(value) {
var stripped=value
.replace(/[^0-9.-]/g, '');

target(parseFloat(stripped));
};

var result = ko.computed({
read: function() {
return target();
},
write: writeTarget
});

result.formatted = ko.computed({
read: function() {
return format(target());
},
write: writeTarget
});

result.isNegative = ko.computed(function(){
return target()<0;
});

return result;
};
})();

//Wire it up
$(function() {
var viewModel = {
Cash: ko.observable(-1234.56).money(),
Check: ko.observable(2000).money(),
showJSON: function() {
alert(ko.toJSON(viewModel));
}
};


viewModel.Total = ko.computed(function() {
return this.Cash() + this.Check();
}, viewModel).money();
ko.applyBindings(viewModel);
});

关于knockout.js - 向 Knockout 数字数据绑定(bind)添加十进制格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16009493/

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