クリックしたカラムでソート4

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>クリックしたカラムでソート4</title>
  6. <script type="text/javascript" src="jquery-1.5.1.min.js"></script>
  7. <script type="text/javascript" src="knockout-2.1.0beta.js"></script>
  8. <script type="text/javascript" src="jquery.common.js"></script>
  9. <script type="text/javascript">
  10.   $(document).ready(function () {
  11.     function viewModel() {
  12.       var self = this;
  13.       
  14.       // 表示するデータ群をいれる変数(?)
  15.       self.tourism = ko.observableArray();
  16.       
  17.       // 表示するデータ
  18.       self.tourism([
  19.         { id: 60, local: '九州地方', area: '太宰府', kind: '歴史遺産', description: '太宰府天満宮、大宰府政庁跡。', },
  20.         { id: 12, local: '東北地方', area: '大仙市', kind: '文化・自然・温泉', description: '大曲花火大会、秋ノ宮温泉郷など。', },
  21.         { id: 20, local: '北海道', area: '七飯', kind: '自然・保養', description: '大沼・小沼などの大沼国定公園、流山温泉。', },
  22.         { id: 25, local: '沖縄地方', area: '恩納', kind: '保養地・自然', description: 'ビーチリゾート、万座毛。', },
  23.         { id: 15, local: '北海道', area: '旭川', kind: '文化', description: 'アイヌ文化、旭山動物園など。', },
  24.         { id: 16, local: '九州地方', area: '指宿', kind: '保養・自然', description: '指宿温泉、開聞岳、池田湖、長崎鼻。', },
  25.         { id: 30, local: '九州地方', area: '鹿児島', kind: '歴史遺産・自然', description: '桜島、磯庭園、城山など。', },
  26.         { id: 3,  local: '関東地方', area: '箱根', kind: '保養', description: '箱根温泉、芦ノ湖、箱根駅伝。', },
  27.         { id: 40, local: '中部地方', area: '黒部', kind: '自然・温泉', description: '宇奈月温泉などの温泉、黒部峡谷。', },
  28.         { id: 25, local: '中部地方', area: '名古屋', kind: '歴史遺産・文化', description: '名古屋城、熱田神宮、徳川美術館', },
  29.         { id: 32, local: '四国地方', area: '大洲', kind: '町並・歴史遺産', description: 'おはなはん通り、大洲城、鵜飼など。', },
  30.         { id: 36, local: '中国地方', area: '米子', kind: '保養・歴史遺産', description: '皆生温泉、米子城跡と城下町', },
  31.         { id: 13, local: '中国地方', area: '出雲市', kind: '自然・歴史遺産', description: '出雲大社、日御碕、立久恵峡など。', },
  32.         { id: 10, local: '沖縄地方', area: '南城市知念地区', kind: '歴史遺産', description: '世界遺産である斎場御嶽。', },
  33.       ]);
  34.       
  35.       // ソート
  36.       self.sortObj = function (key, order) {
  37.         // key   : 並び替えの対象のキー
  38.         // order : true - 昇順(正順:小さいモノ順) / false - 降順(逆順:大きいもの順)
  39.         
  40.         $.arySort2(self.tourism(), key, order);
  41.         self.tourism(self.tourism());
  42.       }
  43.     }
  44.     
  45.     ko.applyBindings(new viewModel());
  46.   });
  47. </script>
  48. <style type="text/css">
  49. tr:hover{
  50.   background-color:#ffffcc;
  51. }
  52. th, td{
  53.   border:1px solid #ccc;
  54.   font-size:small;
  55. }
  56. </style>
  57. </head>
  58. <body>
  59. <h1>クリックしたカラムでソート4</h1>
  60. <table>
  61.   <thead>
  62.     <tr>
  63.       <th>id
  64.         <a href="#" title="正順(小さい順)" data-bind="text: '▲', click: function(){$root.sortObj('id', true);}"></a>
  65.         <a href="#" title="逆順(大きい順)" data-bind="text: '▼', click: function(){$root.sortObj('id', false);}"></a>
  66.       </th>
  67.       <th>地方
  68.         <a href="#" title="正順(小さい順)" data-bind="text: '▲', click: function(){$root.sortObj('local', true);}"></a>
  69.         <a href="#" title="逆順(大きい順)" data-bind="text: '▼', click: function(){$root.sortObj('local', false);}"></a>
  70.       </th>
  71.       <th>地域
  72.         <a href="#" title="正順(小さい順)" data-bind="text: '▲', click: function(){$root.sortObj('area', true);}"></a>
  73.         <a href="#" title="逆順(大きい順)" data-bind="text: '▼', click: function(){$root.sortObj('area', false);}"></a>
  74.       </th>
  75.       <th>種別
  76.         <a href="#" title="正順(小さい順)" data-bind="text: '▲', click: function(){$root.sortObj('kind', true);}"></a>
  77.         <a href="#" title="逆順(大きい順)" data-bind="text: '▼', click: function(){$root.sortObj('kind', false);}"></a>
  78.       </th>
  79.       <th>説明
  80.         <a href="#" title="正順(小さい順)" data-bind="text: '▲', click: function(){$root.sortObj('description', true);}"></a>
  81.         <a href="#" title="逆順(大きい順)" data-bind="text: '▼', click: function(){$root.sortObj('description', false);}"></a>
  82.       </th>
  83.     </tr>
  84.   </thead>
  85.   <tbody data-bind="foreach: tourism">
  86.     <tr>
  87.       <td style="white-space:nowrap" data-bind="text: id"></td>
  88.       <td style="white-space:nowrap" data-bind="text: local"></td>
  89.       <td style="white-space:nowrap" data-bind="text: area"></td>
  90.       <td style="white-space:nowrap" data-bind="text: kind"></td>
  91.       <td data-bind="text: description"></td>
  92.     </tr>
  93.   </tbody>
  94. </table>
  95. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  96. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  97. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  98. </div>
  99. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  100. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  101. </div>
  102. </body>
  103. </html>