How do I sort by a hidden column in DataTables?

拈花ヽ惹草 提交于 2021-01-29 07:20:56

问题


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!


回答1:


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/



来源:https://stackoverflow.com/questions/38079208/how-do-i-sort-by-a-hidden-column-in-datatables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!