0

When I apply DataTables to the following:

<td class="years"><?php $years."-years" ?></td>
<td class="..." ...
... other <td> ...

my table displays as follows:

10-years ... ... ...
10-years ... ... ...
5-years  ... ... ...
7-years  ... ... ...
9-years  ... ... ...

because of the alphabetic ordering. I need 10-years to appear at the bottom. To do that I added <td class="hidden"><?php $years ?></td> right after <td class="years"><?php $years."-years" ?></td> and added "order": [ 1, 'asc' ] to the datatable initialization:

$(".table-rates").DataTable( {
   "order": [ 1, 'asc' ]
});

after which it stopped working and started reporting an error in the console: "Cannot read property 'mData' of undefined".

Can someone explain how I can sort by a hidden column in my DataTables? I looked up online, but the solutions did not work for me. Even worse, the syntax is extremely confusing and hard to follow. Any help would be appreciated. Thanks!

Alex
  • 3,719
  • 7
  • 35
  • 57

1 Answers1

8

It's unnecessary to add other column, you can use data-attributes of datatable, add in your html code data-order:

<td class="years" data-order="<?php $years ?>"><?php $years."-years" ?></td>

And your code JS:

$(document).ready(function() {
    $('#example').DataTable({
        "order": [ 0, 'asc' ]
    });
});

Result: https://jsfiddle.net/cmedina/7kfmyw6x/69/

ihightower
  • 3,093
  • 6
  • 34
  • 49
CMedina
  • 4,034
  • 3
  • 24
  • 39