2016-11-24 129 views
1

我使用PHP創建了表格& MySQL與Datatables插件,我試圖用DD.MM.YYYY排序我的列。格式,但它似乎不工作,因爲我喜歡它。它僅使用第一個數字排序。 這裏是我的代碼Datatables日期排序(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; 
}});