knockoutjsちゅーとりある:データをテーブルに出力して、編集可能にする
- <!DOCTYPE html>
- <html lang="ja">
- <head>
- <meta charset="UTF-8">
- <title>knockoutjsちゅーとりある:データをテーブルに出力して、編集可能にする</title>
- <script type="text/javascript" src="jquery-1.5.1.min.js"></script>
- <script type="text/javascript" src="knockout-2.1.0beta.js"></script>
- <script type="text/javascript">
- $(function () {
- function SeatReservation(name, initialMeal) {
- var self = this;
- self.name = name;
- self.meal = ko.observable(initialMeal);
-
- self.formattedPrice = ko.computed(function() {
- var price = self.meal().price;
- return price ? "$" + price.toFixed(2) : "None";
- });
- }
- function ReservationsViewModel() {
- var self = this;
- self.availableMeals = [
- { mealName: "Standard (sandwich)", price: 0 },
- { mealName: "Premium (lobster)", price: 34.95 },
- { mealName: "Ultimate (whole zebra)", price: 290 }
- ];
- self.seats = ko.observableArray([
- new SeatReservation("Steve", self.availableMeals[0]),
- new SeatReservation("Bert", self.availableMeals[1])
- ]);
- self.addSeat = function() {
- self.seats.push(new SeatReservation("", self.availableMeals[0]));
- }
- }
- ko.applyBindings(new ReservationsViewModel());
- })();
- </script>
- </head>
- <body>
- <table>
- <thead><tr>
- <th>Passenger name</th><th>Meal</th><th>Surcharge</th>
- </tr></thead>
- <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: formattedPrice"></td>
- </tr>
- </tbody>
- </table>
- <button data-bind="click: addSeat">Reserve another seat</button>
- <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
- <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
- <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
- </div>
- <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
- Copyright © 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
- </div>
- </body>
- </html>