jquery - DataTables custom date sort function -


i know http://www.datatables.net/plug-ins/sorting please not post not assist me.

i looking create custom date sorting system use our cms. issue customer can choose date format , preventing proper sorting. have started working datatables , first attempt @ custom sorting system. code below gathers correct information on click not resort field.

$.fn.datatable.ext.order['date-custom']=function(settings,col){return this.api().column(col,{order:'index'}).nodes().map(function(td,i){      function splitdate(str){         if(str.indexof("-") >= 0){ str=str.split('-'); }         else if(str.indexof("/") >= 0){ str=str.split('/'); }         else if(str.indexof(".") >= 0){ str=str.split('.'); }         else{str='';}         return str;     }      // needs pull attribute  "data-format" table                 if($(td).parent().parent().parent().attr('data-date-format') !== undefined  && $(td).parent().parent().parent().attr('data-date-format')!=''){           var format= $(td).parent().parent().parent().attr('data-date-format');           var = $(td).text();          var p = splitdate(a);          var result ='10000000000000';         if(p.length==3){             switch(format){                 case 'm-d-y':result = p[2]+p[0]+p[1]; break;                 case 'm-d-y':result = p[2]+p[0]+p[1]; break;                 case 'm/d/y':result = p[2]+p[0]+p[1]; break;                 case 'm/d/y':result = p[2]+p[0]+p[1]; break;                 case 'm.d.y':result = p[2]+p[0]+p[1]; break;                 case 'm.d.y':result = p[2]+p[0]+p[1]; break;                 case 'y-m-d':result = p[0]+p[1]+p[2]; break;                 case 'y-m-d':result = p[0]+p[1]+p[2]; break;                 case 'y/m/d':result = p[0]+p[1]+p[2]; break;                 case 'y/m/d':result = p[0]+p[1]+p[2]; break;                 case 'y.m.d':result = p[0]+p[1]+p[2]; break;                 case 'y.m.d':result = p[0]+p[1]+p[2]; break;                 case 'd-m-y':result = p[2]+p[1]+p[0]; break;                 case 'd-m-y':result = p[2]+p[1]+p[0]; break;                 case 'd/m/y':result = p[2]+p[1]+p[0]; break;                 case 'd/m/y':result = p[2]+p[1]+p[0]; break;                 case 'd.m.y':result = p[2]+p[1]+p[0]; break;                 case 'd.m.y':result = p[2]+p[1]+p[0]; break;             }         }      }else{  var result='10000000000000';}     return result;  });} 

table code

$("#table").datatable({     columns:[ null,null,null,{ "orderdatatype": "date-custom" }]; }  

solution #1

  • you should add -pre name of custom sorting function, see ordering plug-in development - pre-deformatting more information.

    $.extend($.fn.datatableext.osort, {     "date-custom-pre":function(a){         // ... skipped ...     } }); 
  • there issues splitdate function. correct code

    function splitdate(str){     if(str.indexof("-") >= 0){ str = str.split('-'); }     else if(str.indexof("/") >= 0){ str = str.split('/'); }     else if(str.indexof(".") >= 0){ str = str.split('.'); }     else{ str='';}     return str; } 
  • you won't able access table $(this).parent().parent() this refers window object. hard-coded date format until workaround available.

demo

see this jsfiddle code , demonstration.

solution #2

use custom data source ordering access table inside sorting function.

$.fn.datatable.ext.order['date-custom'] = function  ( settings, col ) {      var api = this.api();      return api.column( col, {order:'index'} ).nodes().map( function ( td, ) {         function splitdate(str){             if(str.indexof("-") >= 0){ str = str.split('-'); }             else if(str.indexof("/") >= 0){ str = str.split('/'); }             else if(str.indexof(".") >= 0){ str = str.split('.'); }             else{ str='';}             return str;         }          var format = $(api.table().node()).attr('data-format');                          var = $(td).text();          // collect date field         var p = splitdate(a);         var result ='10000000000000';         if(p.length==3){             switch(format){                 case 'm-d-y':result = p[2]+p[0]+p[1]; break;                 case 'm-d-y':result = p[2]+p[0]+p[1]; break;                 case 'm/d/y':result = p[2]+p[0]+p[1]; break;                 case 'm/d/y':result = p[2]+p[0]+p[1]; break;                 case 'm.d.y':result = p[2]+p[0]+p[1]; break;                 case 'm.d.y':result = p[2]+p[0]+p[1]; break;                 case 'y-m-d':result = p[0]+p[1]+p[2]; break;                 case 'y-m-d':result = p[0]+p[1]+p[2]; break;                 case 'y/m/d':result = p[0]+p[1]+p[2]; break;                 case 'y/m/d':result = p[0]+p[1]+p[2]; break;                 case 'y.m.d':result = p[0]+p[1]+p[2]; break;                 case 'y.m.d':result = p[0]+p[1]+p[2]; break;                 case 'd-m-y':result = p[2]+p[1]+p[0]; break;                 case 'd-m-y':result = p[2]+p[1]+p[0]; break;                 case 'd/m/y':result = p[2]+p[1]+p[0]; break;                 case 'd/m/y':result = p[2]+p[1]+p[0]; break;                 case 'd.m.y':result = p[2]+p[1]+p[0]; break;                 case 'd.m.y':result = p[2]+p[1]+p[0]; break;             }         }          return result;     } ); };  $(document).ready(function (){     var table = $('#example').datatable({        columndefs: [            { targets: 4, orderdatatype: 'date-custom', type: 'string' }        ]     }); }); 

please note need add column definition columns or columndefs , use following options orderdatatype: 'date-custom', type: 'string'.

demo

see this jsfiddle code , demonstration.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -