javascript - jquery Datatables eu-date not working as expected -
i trying sort eu date (dd/mm/yyyy) using data tables it's not working expected , not ordering date should be,
js
$.extend($.fn.datatableext.osort, { "date-eu-pre": function (date) { date = date.replace(" ", ""); if (!date || date == "-") { return -1; } 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) { if (a == '-') return 1; else if (b == '-') return -1; else return ((a < b) ? -1 : ((a > b) ? 1 : 0)); }, "date-eu-desc": function (a, b) { if (a == '-') return 1; else if (b == '-') return -1; else return ((a < b) ? 1 : ((a > b) ? -1 : 0)); } }); $('#table').datatable({ "paging": false, "searching": false, "footer": false, "info": false, "aocolumns": [null, { type: 'date-eu', targets: 0 }] });
html
<table id=table> <thead> <tr> <th>normal</th> <th>date-eu</th> </tr> </thead> <tbody> <tr> <td>5</td> <td>29/10/2011</td> </tr> <tr> <td>-</td> <td>-</td> </tr> <tr> <td>15</td> <td>03/12/2035</td> </tr> <tr> <td>4</td> <td>20/01/2014</td> </tr> <tr> <td>-5</td> <td>03/12/2015</td> </tr> <tr> <td>10</td> <td>04/05/2015</td> </tr> <tr> <td>20</td> <td>-</td> </tr> <tr> <td>-</td> <td>-</td> </tr> <tr> <td>50</td> <td>-</td> </tr> </tbody> </table>
i want display "-" @ bottom times also.
fixed it, few other answers on incorrect,
fixed js
$.extend($.fn.datatableext.osort, { "date-eu-asc": function (a, b) { if (a == '-') return 1; else if (b == '-') return -1; else { var aday = a.split('/')[0] var amonth = a.split('/')[1] var ayear = a.split('/')[2] var = new date(amonth + "/" + aday + "/" + ayear).gettime(); var bday = b.split('/')[0] var bmonth = b.split('/')[1] var byear = b.split('/')[2] var b = new date(bmonth + "/" + bday + "/" + byear).gettime(); return ((a < b) ? -1 : ((a > b) ? 1 : 0)); } }, "date-eu-desc": function (a, b) { if (a == '-') return 1; else if (b == '-') return -1; else { var aday = a.split('/')[0] var amonth = a.split('/')[1] var ayear = a.split('/')[2] var = new date(amonth + "/" + aday + "/" + ayear).gettime(); var bday = b.split('/')[0] var bmonth = b.split('/')[1] var byear = b.split('/')[2] var b = new date(bmonth + "/" + bday + "/" + byear).gettime(); return ((a < b) ? 1 : ((a > b) ? -1 : 0)); } } }); $('#table').datatable({ "aocolumns": [null, { type: 'date-eu', targets: 0 }] });
make sure add reference latest version of datatables make work properly.
Comments
Post a Comment