我有一個對象,我喜歡從另一個observableArray讀取它的值作爲查找。這裏是它的鏈接http://jsfiddle.net/928FQ/。Knockout如何從另一個查找表打印值
我有一個正確填充的選擇列表,它顯示了基於行值的正確選擇,但是如何將「附加費」列從「0」更改爲「便宜」。它看起來像options
標籤僅適用於select
。任何幫助都會很棒。
HTML:
<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 data-bind="text: name"></td>
<td data-bind="text: meal().mealName"></td>
<td data-bind="text: meal().price"></td>
<td><select data-bind="options: $parent.priceLookup, optionsText: 'defenition', optionsValue: 'price', value: meal().price"></select>
</td>
</tr>
</tbody>
</table>
的JavaScript:
// Class to represent a row in the seat reservations grid
function SeatReservation(name, initialMeal) {
var self = this;
self.name = name;
self.meal = ko.observable(initialMeal);
}
// Overall viewmodel for this screen, along with initial state
function ReservationsViewModel() {
var self = this;
// Non-editable catalog data - would come from the server
self.availableMeals = [
{ mealName: "Standard (sandwich)", price: 0 },
{ mealName: "Premium (lobster)", price: 34.95 },
{ mealName: "Ultimate (whole zebra)", price: 290 }
];
self.priceLookup = ko.observableArray([
{ price:0, defenition: 'cheap'},
{ price:34.95, defenition: 'average'}
]);
// Editable data
self.seats = ko.observableArray([
new SeatReservation("Steve", self.availableMeals[0]),
new SeatReservation("Bert", self.availableMeals[0])
]);
}
ko.applyBindings(new ReservationsViewModel());
我已將小提琴的代碼添加到帖子中。請參閱[此meta post](http://meta.stackexchange.com/q/149890/153691)以獲取更多信息 – 2013-04-26 02:10:36