javascript - Setting dropdown value attributes with knockout.js -


i have used example knockout tutorial stripped down essentials reproduce problem. cannot figure out how set value attribute of tags in items. added value each entry of self.availablemeals try add fails populate dropdowns @ all. when try add optionsvalue binding populates dropdowns doesn't select appropriate value. please help!

<h2>your seat reservations</h2>  <table>     <thead><tr>         <th>passenger name</th><th>meal</th><th>surcharge</th><th></th>     </tr></thead>     <!-- todo: generate table body -->     <tbody data-bind="foreach: seats">     <tr>         <td><input data-bind="value: name" /></td>         <td><select data-bind="options: $root.availablemeals, value: meal, optionstext: 'mealname'"></select></td>     </tr>          </tbody> </table>   // class represent row in seat reservations grid function seatreservation(name, initialmeal) {     var self = this;     self.name = name;     self.meal = ko.observable(initialmeal); }  // overall viewmodel screen, along initial state function reservationsviewmodel() {     var self = this;      // non-editable catalog data - come server     self.availablemeals = [         { mealname: "standard (sandwich)", price: 0 },         { mealname: "premium (lobster)", price: 34.95 },         { mealname: "ultimate (whole zebra)", price: 290 }     ];          // editable data     self.seats = ko.observablearray([         new seatreservation("steve", self.availablemeals[0]),         new seatreservation("bert", self.availablemeals[1])     ]); }  ko.applybindings(new reservationsviewmodel()); 

i figured out. don't know why couldn't add value attribute rendered html didn't need to. need retrieve selected item model examine that.

<h2>your seat reservations</h2> <table>     <thead><tr>         <th>passenger name</th><th>meal</th><th></th>     </tr></thead>     <tbody data-bind="foreach: seats">     <tr>         <td><input data-bind="value: name" /></td>         <!-- line works doesn't try populate value attribute of <option> -->         <td><select data-bind="options: $root.availablemeals, value: meal, optionstext: 'mealname'"></select></td>         <!-- line tries populate value attribute commented out in javascript doesn't work -->         <!--<td><select data-bind="options: $root.availablemeals, value: meal, optionstext: 'mealname', optionsvalue: 'val'"></select></td>-->     </tr>          </tbody> </table> <button data-bind="click: showval">show value</button>  // class represent row in seat reservations grid function seatreservation(name, initialmeal) {     var self = this;     self.name = name;     self.meal = ko.observable(initialmeal); }  // overall viewmodel screen, along initial state function reservationsviewmodel() {     var self = this;      // non-editable catalog data - come server     self.availablemeals = [         { val: 0, mealname: "standard (sandwich)" },         { val: 1, mealname: "premium (lobster)" },         { val: 2, mealname: "ultimate (whole zebra)" }     ];          // editable data     self.seats = ko.observablearray([         new seatreservation("steve", self.availablemeals[0]),         new seatreservation("bert", self.availablemeals[1])     ]);      self.showval = function() {         alert(self.seats()[1].meal().val);     } }  ko.applybindings(new reservationsviewmodel()); 

so can (for example) 1 of val values self.seats()[1].meal().val

i hope helps else same misunderstanding.

http://jsfiddle.net/bwbf3/5/


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -