Alex Alex - 10 months ago 127
jQuery Question

How do I sort by a hidden column in DataTables?

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
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!


It's innecesary 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() {
        "order": [ 0, 'asc' ]