Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a computed column with JSON
    primarykey
    data
    text
    <p>I am trying to make a computed column on data returned via JSON. I cannot get it to work. I either get a <code>NAN</code> value or it says the column does not exist.</p> <p>javascript:</p> <pre><code>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 &lt; 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(); </code></pre> <p>What I am trying to accomplish is for it to recalculate the CommissionAmount after I change the modifier or rate.</p> <p><strong>EDIT</strong></p> <p>If I manually map the fields I get the <code>NaN</code></p> <pre><code>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()); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload