<h2>Your seat reservations (<span data-bind="text: seats().length"></span>)</h2>
<h3 data-bind="visible: totalSurcharge() > 0 ">
Total surcharge: $<span data-bind="text: totalSurcharge().toFixed(2)">< /span>
</h3>
<table>
<thead><tr>
<th>Passenger name</th><th>Meal</th><th>Surc harge</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>
<!--<td data-bind="text: name"></td>
<td data-bind="text: meal().mealName"></td>
<td data-bind="text: meal().price"></td>-->
<td data-bind="text: formattedPrice"></td>
<td><a href="#" data-bind=" click: $root.removeSeat">Remove</a></ td>
</tr>
<tr><button data-bind=" click: addSeat, enable: seats().length < 5">Reserve another set</button></tr>
</tbody>
</table>
// Class to represent a row in the seat reservations grid
function SeatReservation(name, initialMeal) {
var self = this;
self.name = name;
self.meal = ko.observable(in itialMeal);
self.formattedPrice = ko.com puted(function() {
var price = self.meal().pric e;
return price ? "$" + price.t oFixed(2) : "None";
});
}
// 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 }
];
// Editable data
self.seats = ko.observableAr ray([
new SeatReservation("Steve", self.availableMeals[0]),
new SeatReservation("Bert", self.availableMeals[0])
]);
// Operations
self.addSeat = function() {
self.seats.push(new SeatRese rvation("", self.availableMeal s[0]));
};
self.removeSeat = function(s eat) {
self.seats.remove(seat);
};
//Total price
self.totalSurcharge = ko.com puted(function() {
var total = 0;
for (var i = 0; i < self.sea ts().length; i++) {
total += self.seats()[i].mea l().price
};
return total;
});
}
ko.applyBindings(new Reservati onsViewModel());
<h3 data-bind="visible: totalSurcharge() > 0 ">
Total surcharge: $<span data-bind="text: totalSurcharge().toFixed(2)"><
</h3>
<table>
<thead><tr>
<th>Passenger name</th><th>Meal</th><th>Surc
</tr></thead>
<!-- Todo: Generate table body -->
<tbody data-bind="foreach: seats">
<tr>
<td><input data-bind="value: name" /></td>
<td><select data-bind="
<!--<td data-bind="text: name"></td>
<td data-bind="text: meal().mealName"></td>
<td data-bind="text: meal().price"></td>-->
<td data-bind="text: formattedPrice"></td>
<td><a href="#" data-bind="
</tr>
<tr><button data-bind="
</tbody>
</table>
function SeatReservation(name,
var self = this;
self.name = name;
self.meal = ko.observable(in
self.formattedPrice = ko.com
var price = self.meal().pric
return price ? "$" + price.t
});
}
// 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 }
];
// Editable data
self.seats = ko.observableAr
new SeatReservation("Steve",
new SeatReservation("Bert",
]);
// Operations
self.addSeat = function() {
self.seats.push(new SeatRese
};
self.removeSeat = function(s
self.seats.remove(seat);
};
//Total price
self.totalSurcharge = ko.com
var total = 0;
for (var i = 0; i < self.sea
total += self.seats()[i].mea
};
return total;
});
}
ko.applyBindings(new Reservati
Комментариев нет:
Отправить комментарий