简体   繁体   中英

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

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() {
    $('#example').DataTable({
        "order": [ 0, 'asc' ]
    });
});

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

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