-2

如何在 Knockout koGrid 中将数字格式化为货币(带逗号)?

<div style="height: 200px" data-bind="
  koGrid: {
    data: $parent.gridLoanDetails, 
    columnDefs: [{ 
      field: 'MemoAvailability', displayName: 'Memo Availability', width: 150
    }]
  }
">

编辑视图模型:

 var vm = {
        gridLoanDetails: gridLoanDetails,
        SearchLCLookup: function (tab, IsAffiliate) {

                var HasRecs = false;
                gridLoanDetails([]);

                if (vm.LCID() != '') {
                    HasRecs = LCDataService.GetLCAppsJSON(gridLoanDetails, '', tab.ClientNum(), tab.BeneficiaryName(), '', '',
                        '', 0, 0, '', '', vm.LCID());
                } //           clientsObservable, ClientName, ClientNum, BeneficiaryName, UserID_CA, UserID_RM, UserID_LPM, ApplicationAmountFROM, ApplicationAmountTO, CreationDateFROM, CreationDateTO

                return HasRecs;

        }
          }
 return vm

数据服务:数据服务方法是对返回 json 的 web api 的简单 ajax 调用。

         var GetLCAppsJSON = function (clientsObservable, ClientName, ClientNum, BeneficiaryName, UserID_CA, UserID_RM, UserID_LPM, ApplicationAmountFROM, ApplicationAmountTO, CreationDateFROM, CreationDateTO, LCID) {

        var LCAppsModel = function (ClientAffilname, AffiliateOrClient, ClientNum, Rating, ClientStatus, ContractExpirationDate, UserID_LPM, UserID_RM, UserID_CA, AffiliateName, CAFullName, LPMFullName, RMFullName, Comments, ClientAvailability, LCLoan, ABLoan, LCID, ApplicationAmount, Tolerance, LCExpireDate, UserID, EmailComments, BeneficiaryName, MemoAvailability, AppID) {
            var self = this;

            self.ClientAffilname = ko.observable(ClientAffilname);
            self.AffiliateOrClient = ko.observable(AffiliateOrClient);
            self.ClientNum = ko.observable(ClientNum);
            self.Rating = ko.observable(Rating);
            self.ClientStatus = ko.observable(ClientStatus);
            self.ContractExpirationDate = ko.observable(ContractExpirationDate);
            self.UserID_LPM = ko.observable(UserID_LPM);
            self.UserID_RM = ko.observable(UserID_RM);
            self.UserID_CA = ko.observable(UserID_CA);
            self.AffiliateName = ko.observable(AffiliateName);
            self.CAFullName = ko.observable(CAFullName);
            self.LPMFullName = ko.observable(LPMFullName);
            self.RMFullName = ko.observable(RMFullName);
            self.Comments = ko.observable(Comments);
            self.ClientAvailability = ko.observable(ClientAvailability);

            self.LCLoan = ko.observable(LCLoan);
            self.ABLoan = ko.observable(ABLoan);
            self.LCID = ko.observable(LCID);
            self.ApplicationAmount = ko.observable(ApplicationAmount);
            self.Tolerance = ko.observable(Tolerance);
            self.LCExpireDate = ko.observable(LCExpireDate);
            self.UserID = ko.observable(UserID);
            self.EmailComments = ko.observable(EmailComments);
            self.BeneficiaryName = ko.observable(BeneficiaryName);
            self.MemoAvailability = ko.observable(MemoAvailability);
            self.AppID = ko.observable(AppID);
        };

        //                  ClientAvailability,
        //          Rating, ClientStatus, UserID, UesrID_LPM, UesrID_RM, UesrID_CA, StatusCode

        $.ajax({
            type: "post",
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            url: "/api/LCPosting/PostLCAppsJSON/",
            data: JSON.stringify({ ClientName: ClientName, ClientNum: ClientNum, BeneficiaryName: BeneficiaryName, UserID_CA: UserID_CA, UserID_RM: UserID_RM, UserID_LPM: UserID_LPM, ApplicationAmountFROM: ApplicationAmountFROM, ApplicationAmountTO: ApplicationAmountTO, CreationDateFROM: CreationDateFROM, CreationDateTO: CreationDateTO, LCID: LCID }),
            cache: false,
            traditional: true,
            async: false,
            success: function (dataIn) {
                newJ = $.parseJSON(dataIn);

                newJ.forEach(function (p) {
                    var clients = new LCAppsModel(p.ClientAffilname, p.AffiliateOrClient, p.ClientNum, p.Rating, p.ClientStatus, p.ContractExpirationDate, p.UserID_LPM, p.UserID_RM, p.UserID_CA, p.AffiliateName, p.CAFullName, p.LPMFullName, p.RMFullName, p.Comments, p.ClientAvailability, p.LCLoan, p.ABLoan, p.LCID, p.ApplicationAmount, p.Tolerance, p.LCExpireDate, p.UserID, p.EmailComments, p.BeneficiaryName, p.MemoAvailability, p.AppID);

                    clientsObservable.push(clients);
                });

            },
            error: function (xhr, textStatus, errorThrown) {
                var err = eval("(" + xhr.responseText + ")");
                amplify.store("ErrorDetails", err.exceptionMessage);


                router.map([
                    { route: 'ErrorPage', moduleId: 'ErrorPage', title: 'ErrorPage', title: 'ErrorPage', nav: false }
                ]).activate

                router.navigate('ErrorPage');

            }
        });

        if (clientsObservable().length > 0) {
            return true;
        } else {
            return false;
        }


    };

我需要将 MemoAvailability 字段在视图中格式化为货币。

4

1 回答 1

1

您需要格式化 viewModel 中的字段,knockoutJS 提供了扩展器,因此您可以向 observable 添加额外的功能。您可以使用非常常见的Money Observable

或者您可以创建自己的扩展器一个基本示例

ko.extenders.currency = function(target, precision) {
var result = ko.computed({
    read: function() {
        return target().toFixed(precision); 
    },
    write: target 
});

result.raw = target;
return result;
};

然后像这样在你的viewModel中使用它

var MemoAvailability = ko.observable(MemoAvailability).extend({currency: 2});

您可以进一步改进扩展器。

另一种方法是,由于您从服务器返回 Json,您可以在返回客户端之前在服务器中格式化此值。

于 2014-07-01T15:01:12.293 回答