2016-11-24 2 views
1

PHPを使用してテーブルを作成しました& DatatablesプラグインでMySQLを作成しましたが、DD.MM.YYYYで列を並べ替えようとしています。フォーマットはそれが私がそれを望むように動作しないようだ。最初の番号だけを使ってソートしています。 は、ここに私のコードここ(DD.MM.YYYY。)形式のデータ型の並べ替え

$(document).ready(function() { 
    var currentDate = new Date() 
    var day = currentDate.getDate() 
    var month = currentDate.getMonth() + 1 
    var year = currentDate.getFullYear() 
    var datum = day + "." + month + "." + year + "."; 
    var doc = day + month + year; 
    $(document).ready(function() { 

     $.fn.dataTable.moment('DD.MM.YYYY.'); 

     $('#pogled_ispu').removeAttr('width').DataTable({ 
      dom: 'Bfrtip', 
      scrollY: "500px", 
      scrollX: true, 
      scrollCollapse: true, 
      paging: false, 
      select: true, 
      columnDefs: [{ 
       width: 10, 
       targets: 8 
      }], 
      fixedColumns: { 
       leftColumns: 0 
      }, 
      buttons: [ 
       'colvis', 
       'pageLength', { 
        extend: 'excelHtml5', 
        text: '<i class="fa fa-file-excel-o"></i>', 
        titleAttr: 'Preuzimanje u excel formatu', 
        title: 'Tablični prikaz_' + datum, 
        exportOptions: { 
         columns: ':visible' 
        } 
       }, { 
        extend: 'pdfHtml5', 
        orientation: 'landscape', 
        text: '<i class="fa fa-file-pdf-o"></i>', 
        titleAttr: 'Preuzimanje u PDF formatu', 
        title: 'Prostorno planska dokumentacija', 
        message: 'Prostorno planska dokumentacija ' + datum, 
        pageSize: 'A3' 
       } 
      ], 
      language: { 
       "url": "js/Croatian.json", 
       buttons: { 
        colvis: '<i class="fa fa-eye"></i>', 
        pageLength: 'Broj redova' 
       } 
      } 
     }); 
    }); 
}); 

は、表データの画像です: enter image description here

そしてここでは、日付時刻の順序のためのプラグインです:

$.fn.dataTable.moment = function (format, locale) { 
    var types = $.fn.dataTable.ext.type; 

    // Add type detection 
    types.detect.unshift(function (d) { 
     return moment(d, format, locale, true).isValid() ? 
      'moment-'+format : 
      null; 
    }); 

    // Add sorting method - use an integer for the sorting 
    types.order[ 'moment-'+format+'-pre' ] = function (d) { 
     return moment(d, format, locale, true).unix(); 
    }; 
}; 

は、それが(DD.MM.で問題となっていますYYYY。)形式。私は最後のドットを省略すべきですか?

+0

を私は、それは確かに問題ではないあなたがデータテーブル –

答えて

-1

あなたのMySQLステートメントでORDER BYを使ってみましたか?

私は、MySQL SELECTステートメント内で 'ORDER BY date DESC'を使用してカラムを日付別にソートすることができました。これは選択しているデータを並べ替えるため、データをデータテーブルに渡すと、データは日付順に並べ替えられます。

例:

SELECT column1, column2, datecolumn, column4 
FROM table 
ORDER BY datecolumn DESC 
+0

に文字列として日時書式を渡すと思いますが、それにもかかわらず、ありがとうございましたあなたの努力:) – Svinjica

0

この日付フォーマットコードしてみてください:

$('#table').DataTable({ 
"order": [[0, "desc"]], //order by date when 1st time table are loaded, based on your date column 
"aoColumnDefs": [ 
    { sType: "date-custom", aTargets: [0] } //targets are based on your date column 
], 
"aoColumns": [ 
    {"mDataProp": "date" //your date column name in array 
     ,"mRender": function(data, type, full) 
     { 
      var getData = new Date(data); 
      var month = getData.getMonth() + 1; 
      var date = getData.getDate(); 
      var year = getData.getFullYear(); 

      return (date > 9 ? date : "0" + date) + "." + 
        (month > 9 ? month : "0" + month) + "." + 
        year; 
     } 
    } 
] 
}); 

$.extend(jQuery.fn.dataTableExt.oSort, { 
"date-custom-pre": function(a) { 
var x; 
try { 
    var dateA = a.replace(/ /g, '').split("."); 


    var day = parseInt(dateA[0], 10); 
    var month = parseInt(dateA[1], 10); 
    var year = parseInt(dateA[2], 10); 
    var date = new Date(year, month - 1, day); 
    x = date; 
} 
catch (err) { 
    x = new Date(); 
} 
    return x; 
}, 
"date-custom-asc": function(a, b) { 
    return a - b; 
}, 
"date-custom-desc": function(a, b) { 
    return b - a; 
}}); 
関連する問題