2015-02-07 45 views
3

問題jQuery的數據表排序:嗨,我是使用下面的代碼進行排序,美國在表列中的日期,但它不工作...美國的日期格式自定義排序是使用J-查詢的數據表不工作

jQuery.fn.dataTableExt.oSort['custom_us_date-asc'] = function(x, y) { 
    var xVal = getCustomEuroDateValue(x); 
    var yVal = getCustomEuroDateValue(y); 

    if (xVal < yVal) { 
     return -1; 
    } else if (xVal > yVal) { 
     return 1; 
    } else { 
     return 0; 
    } 
} 

jQuery.fn.dataTableExt.oSort['custom_us_date-desc'] = function(x, y) { 
    var xVal = getCustomEuroDateValue(x); 
    var yVal = getCustomEuroDateValue(y); 

    if (xVal < yVal) { 
     return 1; 
    } else if (xVal > yVal) { 
     return -1; 
    } else { 
     return 0; 
    } 
} 


    function getCustomEuroDateValue(strDate) { 
    var frDatea = $.trim(strDate).split(' '); 
    var frTimea = frDatea[1].split(':'); 
    var frDatea2 = frDatea[0].split('/'); 

    var x = (frDatea2[2] + frDatea2[1] + frDatea2[0] + frTimea[0] + frTimea[1]); 
    x = x * 1; 

    return x; 
} 



function tableDef(){ 
    oTable = j$('#example').dataTable({ 
      "bPaginate":false, 
      "bJQueryUI": true,   
      "aoColumns": [ 
       null, 
       null, 
       null, 
       {"stype" :"custom_us_date", 
        "fnRender" : function(obj, val) 
       { 
        if(j$(val).text() != '') 
        { 
         var d = new Date(j$(val).text()); 

         var month = d.getMonth() + 1; 
         var sMonth = ''; 
         if(month < 10) sMonth = '0' + month; else sMonth =      month; 
         var day = d.getDate(); 
         var sDay = ''; 
         if(day < 10) sDay = '0' + day; else sDay = day; 
         var sYear = d.getFullYear(); 
         var hour = d.getHours(); 
         var sHour = ''; 
         if(hour < 10) sHour = '0' + hour; else sHour = hour; 
         var minute = d.getMinutes(); 
         var sMinute = ''; 
         if(minute < 10) sMinute = '0' + minute; else sMinute =      minute; 
         var finalString = sMonth + '/' + sDay + '/' + sYear + ' ' + sHour + ':' + sMinute; 

         return finalString; 
           } 

但它不做自定義排序(日期),它只是做字符串排序。它根本沒有考慮日期的自定義排序。我將fnRender中的日期格式化爲美國日期格式,然後使用sType將值返回到自定義排序函數。

我很新的J-查詢,如果你能更新我的代碼,這將是高度讚賞....

感謝您尋找....

回答

0

嘿,我嘗試用這一些定製我的代碼,希望這可以幫助您:

$.extend(jQuery.fn.dataTableExt.oSort, { 
"date-custom-pre": function(a) { 
var x; 
try { 
    var dateA = a.replace(/ /g, '').split("-"); 
    var Datetime = a.replace(/ /g, '').split(","); 
    var timeA = Datetime[1];//time hour:minute 
    var timeB = timeA.replace(/ /g, '').split(":");// split hour & minute 
    var hour = timeB[0]; 
    var minute = timeB[1].replace(/\D/g, '');// remove string after "hour:minute" 

    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, hour, minute); 
    x = date.getTime(); 
} 
catch (err) { 
    x = new Date().getTime(); 
} 

return x; 
}, 
    "date-custom-asc": function(a, b) { 
    return a - b; 
}, 
    "date-custom-desc": function(a, b) { 
    return b - a; 
}}); 

如何使用:

$('#table').DataTable({ 
"aaData": data, 
"order": [[0, "desc"]], //default sort column on first page loaded 
"aoColumnDefs": [ 
    { sType: "date-custom", aTargets: [0] } //based on your date column 
], 
"aoColumns": [ 
    {"mDataProp": "date_save" 
     ,"mRender": function(data, type, full) 
     { 
      var getData = new Date(data); 
      var month = getData.getMonth() + 1; 
      var date = getData.getDate(); 
      var year = getData.getFullYear(); 
      var hour = getData.getHours(); 
      var minute = getData.getMinutes(); 

      return (date > 9 ? date : "0" + date) + "-" + 
        (month > 9 ? month : "0" + month) + "-" + 
        year + 
        ", " + 
        (hour > 9 ? hour : "0" + hour) + ":" + 
        (minute > 9 ? minute : "0" + minute); 
     } 
    }, 
    {"mDataProp": "source_page"}, 
    {"mDataProp": "sub_status"} 
]}); 

它會顯示日期這樣的墊子:

15-12-2016, 12:33 -> (dd-mm-yyyy, hh:mm) 
相關問題