2017-10-13 2 views
0

jQueryデータテーブルを使用して2つの日付列を持つHTMLテーブルを表示しています。各日付列には、日付がUNIXのタイムスタンプとして表示されます。私はこのdd-mm-yyyyを変更するためにrender関数とmoment.jsを使用します。しかし、これはうまくソートされないので、データオーダー属性を追加しました。残念ながら、これはレンダリング関数が "type"の型パラメータを取得するので、私のレンダリング関数を破壊します。私は以下のサンプルを含んでいます。誰も助けることができますか?jQueryデータ型日付データオーダーでソートしてレンダリングする

<!DOCTYPE html> 

<body> 
    <table class="tablesaw table-bordered table-hover table" id="list-tbl" data-tablesaw-mode="sort" data-tablesaw-sortable data-tablesaw-sortable-switch> 
     <thead> 
      <tr> 
       <th scope="col" data-tablesaw-sortable-col data-tablesaw-sortable-default-col data-tablesaw-priority="persist">Date 1</th> 
       <th scope="col">Text</th>  
       <th scope="col">Date 2</th> 
      </tr> 
     </thead> 

     <tbody> 
      <tr> 
       <td data-order="1510185600a">1510185600</td> 
       <td>4: 09-11-2017 --- </td> 
       <td data-order="1512777600">1512777600</td> 
      </tr> 
      <tr> 
       <td data-order="1491264000a">1491264000</td> 
       <td>1: 04-04-2017 --- </td> 
       <td data-order="1493856000">1493856000</td> 
      </tr> 
      <tr> 
       <td data-order="1504569600a">1504569600</td> 
       <td>3: 05-09-2017 ---</td> 
       <td data-order="1507161600">1507161600</td> 
      </tr> 
      <tr> 
       <td data-order="1498780800a">1498780800</td> 
       <td>2: 30-06-2017 --- </td> 
       <td data-order="1498780800">1498780800</td> 
      </tr> 
     </tbody> 
    </table> 
    <script> 
     $(document).ready(function() { 
      $('#list-tbl').DataTable({ 
       "columnDefs": [ 
        { 
         targets: [0], 
         //data: 0, 
         render: function (data, type, row) { 
          if (type === 'sort') 
           return data; 
          if (type === 'filter') 
          { 
           row[0].display='1'; 
           return 'frd'; 
          } 
          if (type === 'type' || type === 'sort' || type === 'filter') 
          { 
           row[0].display='1'; 
           return 'test'; 
          }         return 'kev'; 
          //if (type === 'type') 
          //{ 
           //row[0].display = moment.unix(row[0].display).format("DD-MM-YYYY"); 
           //row[2].display = moment.unix(row[2].display).format("DD-MM-YYYY"); 
           //var s = moment.unix(data).format("DD-MM-YYYY"); 
           //return s; 
          //} 
          //return data; 
          //return moment.unix(data).format("DD-MM-YYYY"); 
          if (type === 'display') 
           return 'kev'; 

         } 
        } 
       ], 
       "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]], 
       "pageLength": 25 
      }); 
     }); 
    </script> 
</body> 

答えて

0

データのソート属性を使用する代わりに、データの並べ替えのためにmoment.jsや他のプラグインを使用する必要はありません。あなたは

例えば以下のような数値にあなたの日付を変換する必要があります。

<td scope="row" data-sort="' + Date.parse(<your date>)).valueOf() + '">Your date </td>' 

これは正しく日付をソートします。私はちょうど同じを実装しました

関連する問題