本文介绍了将十进制格式添加到 Knockout 数字数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用淘汰赛来填充表格中的数值:

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.

一个基本的扩展器是这样的:

A basic extender is something like:

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});

这篇关于将十进制格式添加到 Knockout 数字数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:34