简体   繁体   中英

DataTable Ascending order Not Working Properly

I have used following code for DataTables and result showing that is not in numeric ascending order.

For example:

I have order: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

DataTable Showing result: 1,10,11,2,3,4,5,6,7,8,9,

Please help me. Thanks.

<script type="text/javascript">
  $(function () {
    $("#example1").dataTable();
    $('#example2').dataTable({
      "bPaginate": true,
      "bLengthChange": false,
      "bFilter": false,
      "bSort": false,
      "bInfo": true,
      "bAutoWidth": false
    });
  });
</script>

According to this DataTables support thread , it looks like your column is probably set to a string type instead of a numeric/integer type.

Here are some solutions from that page:

I had a similar problem with id's I wanted to sort. So my id's were strings and order: [0, 'asc'] didn't work.

The solution was using Pre-deformatting method. It's used to convert the formatted data into orderable data. So it worked with order: [0, 'pre']

https://datatables.net/manual/plug-ins/sorting

In case the solutions above are still not working for you, check whether you're passing a language object to the DataTable constructor, for example this way :

$('#example').DataTable( {
    language: { url: '/localisation/fr_FR.json' }
} );

In my case, that's where the problem came from. The language object contained this line :

"decimal": ","

For some reason, that is what was causing the issue. By removing it, the numbers are now ordered properly.

Of course, this only works if you don't have a need for the decimal specification in your language object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM