-1

我正在尝试对通过 JSON 返回的数据创建一个计算列。我无法让它工作。我要么得到一个NAN值,要么它说该列不存在。

javascript:

function commissionsViewModel() {
    var self = this;

    self.commissions = ko.observableArray();
    ko.applyBindings(self, $("#commissions").get(0));

    function LoadcommissionsFromServer() {
        $.post(
            '/commissions/data',
            function (data) {
                var results = ko.observableArray();
                ko.mapping.fromJS(data, {}, results);
                for (var i = 0; i < results().length; i++) {
                    self.commissions.push(results()[i]);
                };
            },
            'json'
        )
    }

    LoadcommissionsFromServer();

    self.ComissionAmount = ko.computed(
        function () {
            return this.CommissionableAmount * this.Rate * this.Modifier;
        });
    return self;
}

var viewModel = commissionsViewModel();

我想要完成的是在我更改修改器或费率后重新计算 CommissionAmount。

编辑

如果我手动映射我得到的字段NaN

function Commission(data) {
    this.Status = ko.observable(data.Status);
    this.OriginalCommissionCode = ko.observable(data.OriginalCommissionCode);
    this.NewCommissionCode = ko.observable(data.NewCommissionCode);
    this.InvoiceNumber = ko.observable(data.InvoiceNumber);
    this.CustomerID = ko.observable(data.CustomerID);
    this.CommissionableAmount = ko.observable(data.CommissionableAmount);
    this.Modifier = ko.observable(data.Modifier);
    this.Rate = ko.observable(data.Rate);
    this.SalesAmount = ko.observable(data.SalesAmount);
    this.NonSalesAmount = ko.observable(data.NonSalesAmount);
    this.CommissionAmount = ko.computed(function () {
        return this.CommissionableAmount * this.Rate * this.Modifier;
    });
}
function commissionsViewModel() {
    var self = this;

    self.commissions = ko.observableArray();
    $.post(
        '/commissions/data',
        function (allData) {
            var mappedTasks = $.map(allData, function (item) { return new Commission(item) });
            self.commissions(mappedTasks);
        },
        'json'
    );
}
ko.applyBindings(new commissionsViewModel());
4

1 回答 1

0

我假设 Rate 和 Modifier 是可观察的(你没有提供他们的声明)所以你需要使用 this.Rate() 和 this.Modififer() 符号。它们是函数,而不是属性。

于 2012-04-27T14:11:42.833 回答