sorting - Custom sort durations using jQuery DataTables -


i need sort column in jquery datatables. tried using moment plugin without success.

the column contains call duration it's not there use n/a those. column data looks like:

2m 45s 1m 32s n/a 45s 1m 

i need able sort these n/a valuing less 0 , rest in logical order

i use jquery datatables 1.10.6, moment 2.9.0 , have datatables plugins. use data-stype th in header of table. use no config datatable init looks that

// create datatables user     table = $('#summary-table').datatable({         'language'  : { "url": paths.lang_{{laravellocalization::getcurrentlocale()}} },         'responsive':         {             'details':             {                 'type': 'inline'             }         },         'order': [[(nbcat + 5), 'desc']],         'dom': '<"row"<"col-sm-12 before-table                "<"table_controls">>r><"row"<"col-sm-12"t>><"row"<"col-sm-12"ipl>>',         'lengthmenu': [[20, 50, 100, -1], [20, 50, 100, transall]],     }); 

solution

use code below:

jquery.extend(jquery.fn.datatableext.osort, {     "duration-pre": function (s) {                 var duration;          s = s.tolowercase();          if(s === 'n/a'){              duration = -1;          } else {                         d = s.match(/(?:(\d+)m)?\s*(?:(\d+)s)?/);             duration = parseint(d[1] ? d[1] : 0) * 60 + parseint(d[2] ? d[2] : 0);         }          return duration;     } });  $(document).ready(function (){     var table = $('#summary-table').datatable({        columndefs: [            { targets: 0, type: 'duration' }        ]              }); }); 

change 0 in targets: 0 index of column containing duration. omitted other table options sake of simplicity.

demo

see this jsfiddle code , demonstration.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -