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

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