javascript - Setting observable from select list in knockout.js -


i'm trying use value of select list calculation can't retrieve number when it's selected.

when return selected quantity returns javascript instead of number

selectedquantity value i'm trying retrieve.

<table border="1">     <thead><tr>         <th>id</th><th>inventory</th><th>quantity</th><th>price</th><th>total</th><th></th>     </tr></thead>     <tbody data-bind="foreach: itemnumbers">         <tr>             <td data-bind="text: id"></td>             <td data-bind="text: inventory"></td>             <td><select data-bind="quantitydropdown: inventory, value: selectedquantity"></select></td>             <td data-bind="text: formattedprice"></td>             <td data-bind="text: itemtotal"></td>             <td><a href="#" data-bind="click: $root.removeitem">remove</a></td>         </tr>         </tbody> </table> 

here's javascript

function itementry(id, inventory, price) {     var self = this;     self.id = id;     self.inventory = inventory;     self.price = price;      self.selectedquantity = ko.observable(); //returned value (trying @ least!)      self.itemtotal = ko.computed(function() {         var price = self.price;         var quantity = self.selectedquantity;         return quantity; //just returning quantity verify correct value selected     });       self.formattedprice = ko.computed(function() {         var price = self.price;         return price ? "$" + price.tofixed(2) : "none";             }); }  function entryviewmodel(newitem) {     var self = this;     self.newitem = newitem;       //start array items     self.itemnumbers = ko.observablearray([         new itementry("1", 20, 22.50) //id, inventory, price      ]);      // computed data    self.totalcost = ko.computed(function() {        var total = 0;         (var = 0; < self.itemnumbers().length; i++) {             total += number(self.itemnumbers()[i].price);         }        return total;     });          self.removeitem = function(item) { self.itemnumbers.remove(item) } }  //populate select list values number in inventory (ex, if inventory 3, fill 0-7) ko.bindinghandlers.quantitydropdown = {     update: function(quantitydropdown, inventory, entryviewmodel) {         var quantity = ko.utils.unwrapobservable(inventory());         for(var = 0; <= inventory(); i++){             $(quantitydropdown).append('<option value="' + + '">' + + '</option>');         }     } };    ko.applybindings(new entryviewmodel()); 

looks self.selectedquantity defined (set to) ko.observable. observables functions, in order retrieve value, should call function:

self.itemtotal = ko.computed(function() {     var price = self.price;     //notice parentheses here execute selectedquantity observable , extract variable.     var quantity = self.selectedquantity();       return quantity; //just returning quantity verify correct value selected }); 

also, it's understand how ko.computed functions work. ko.computed value automatically update reflect changes ko.observables referenced therein. however, dependency tracking mechanism uses retrieval of observable values detect observable being used , should tracked.

in other words, if want ko.computed update whenever value changes, need reference value executing observable variable in code sample above.

the knockout documentation says lot better can: http://knockoutjs.com/documentation/observables.html http://knockoutjs.com/documentation/computedobservables.html.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -