javascript - How to make custom date sort in datatables? -
i have table column of dates:
2015-03-03 20:14 2015-04-15 20:33 2015-04-20 09:34 2015-04-28 22:18
but cells have no date (i mark -
). when using general sorting function looks like:
— — — 2015-03-03 20:14 2015-04-15 20:33 2015-04-20 09:34 2015-04-28 22:18
but need:
2015-03-03 20:14 2015-04-15 20:33 2015-04-20 09:34 2015-04-28 22:18 — — —
actually, had sort 2 parameters , set rows without date in end of table. added 2 data attribute (status , date in timestamp format):
<tr> <td><span class="label label-warning" data-status="0" data-date="1397562467">status 0</span></td> <td>2015-03-03 20:14</td> </tr> <tr> <td><span class="label label-warning" data-status="1" data-date="1397567321">status 1</span></td> <td>2015-04-15 20:33</td> </tr> <tr> <td><span class="label label-warning" data-status="0" data-date="1397567746">status 0</span></td> <td>2015-04-20 09:34</td> </tr> <tr> <td><span class="label label-warning" data-status="0" data-date="1397567321">status 0</span></td> <td>2015-04-15 20:33</td> </tr>
i made new custom sort function:
jquery.fn.datatableext.osort['status-date-asc'] = function(x,y) { var xstatus, xdate, ystatus, ydate; xstatus = $(x).data('status'); ystatus = $(y).data('status'); xdate = $(x).data('date'); ydate = $(y).data('date'); return xstatus > ystatus ? 1 : xstatus < ystatus ? -1 : xdate > ydate ? 1 : xdate < ydate ? -1 : 0; };
and init row new sort function:
var currenttable = $('#current_table').datatable( { "aasorting": [ [5, 'asc'] ], "aocolumns": [ null, null, null, null, null, { "stype": 'status-date' }, null, null, null ], });
finally, looks:
- status 0, date = 2015-03-03 20:14
- status 0, date = 2015-04-15 20:33
- status 0, date = 2015-04-20 09:34
- status 0, date = 2015-04-28 22:18
- status 0, date = n/a
- status 0, date = n/a
- status 0, date = n/a
- status 1, date = 2015-03-03 20:14
- status 1, date = 2015-04-15 20:33
- status 1, date = 2015-04-20 09:34
- status 1, date = 2015-04-28 22:18
- status 1, date = n/a
- status 1, date = n/a
- status 1, date = n/a
Comments
Post a Comment