datatable sorting on string as number

前端 未结 3 1185
天命终不由人
天命终不由人 2020-12-20 22:40

i have datatable and i want to sort in as numeric it contains value like 1st,2nd...., here is my code when i sort it it sorts values like 1st,10th,2nd so on how to sort it p

3条回答
  •  既然无缘
    2020-12-20 23:24

    The simplest way I know of to do this is to use the Formatted Numbers plugin

    Here is an example:

    jQuery.extend( jQuery.fn.dataTableExt.oSort, {
        "formatted-num-pre": function ( a ) {
            a = (a === "-" || a === "") ? 0 : a.replace( /[^\d\-\.]/g, "" );
            return parseFloat( a );
        },
     
        "formatted-num-asc": function ( a, b ) {
            return a - b;
        },
     
        "formatted-num-desc": function ( a, b ) {
            return b - a;
        }
    } );
    
    $('#tbl_jaar').dataTable( {
         columnDefs: [
           { type: 'formatted-num', targets: 0 }
         ]
      } );
    
    
    
    
    Places
    1st
    2nd
    3rd
    4th
    5th
    6th
    7th
    8th
    9th
    10th

提交回复
热议问题