2017-06-01 23 views
0

對於引導數據表,我有這種格式的日期列dd/mm/yyyy。我嘗試對它進行排序有問題。所以我試圖插入這個columndef。結果是一樣的排序不正確仍然。Datatable columndef不適用於dd/mm/yyyy

var table = $('#listTable').DataTable({ 
     responsive:true, 

     "columnDefs": [ 
     { type: 'date-euro', targets: 0 } 
     ] 
    }); 
+1

請按照這個鏈接中的步驟https:// datatables.net/plug-ins/sorting/date-eu' –

+0

所以我必須包含那個額外的插件,我讀過它不太清楚,你能幫我描述一下嗎? – user8012596

+0

如果您看到此鏈接,請輸入此代碼columnDefs:[ {type:'date-eu',targets:0} ] – user8012596

回答

0

你要添加這個插件這樣的:

請參考以下鏈接:click here

添加以下代碼:

jQuery.extend(jQuery.fn.dataTableExt.oSort, { 
    "date-eu-pre": function (date) { 
     date = date.replace(" ", ""); 

     if (! date) { 
      return 0; 
     } 

     var year; 
     var eu_date = date.split(/[\.\-\/]/); 

     /*year (optional)*/ 
     if (eu_date[2]) { 
      year = eu_date[2]; 
     } 
     else { 
      year = 0; 
     } 

     /*month*/ 
     var month = eu_date[1]; 
     if (month.length == 1) { 
      month = 0+month; 
     } 

     /*day*/ 
     var day = eu_date[0]; 
     if (day.length == 1) { 
      day = 0+day; 
     } 

     return (year + month + day) * 1; 
    }, 

    "date-eu-asc": function (a, b) { 
     return ((a < b) ? -1 : ((a > b) ? 1 : 0)); 
    }, 

    "date-eu-desc": function (a, b) { 
     return ((a < b) ? 1 : ((a > b) ? -1 : 0)); 
    } 
}); 

這是數據表定義:

$('#example').dataTable({ 
    columnDefs: [ 
     { type: 'date-eu', targets: 0 } 
    ] 
    }); 

你也可以從這個堆棧溢出鏈接中找到你的答案:click here

+0

我已經添加了這些代碼,並保存爲與我的jquery.dataTables.min.js和dataTables.bootstrap.min.js相同的date-eu.js文件夾。但它似乎沒有正常工作。 – user8012596

+0

你是在你的頁面中添加date-eu.js文件嗎? –

+0

你有檢查stackoverflow刷新鏈接? –

相關問題