如何按DataTables中的隐藏列排序? [英] How do I sort by a hidden column in DataTables?

查看:68
本文介绍了如何按DataTables中的隐藏列排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将DataTables应用于以下内容时:

When I apply DataTables to the following:

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

我的表显示如下:

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

由于字母顺序.我需要10-years出现在底部.为此,我在<td class="years"><?php $years."-years" ?></td>之后添加了<td class="hidden"><?php $years ?></td>并将"order": [ 1, 'asc' ]添加到了数据表初始化中:

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' ]
});

之后,它停止工作并开始在控制台中报告错误:无法读取未定义的属性'mData'".

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

有人可以解释如何对DataTables中的隐藏列进行排序吗?我在网上查找,但解决方案对我不起作用.更糟糕的是,语法极其混乱且难以遵循.任何帮助,将不胜感激.谢谢!

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>

还有您的代码JS:

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

结果: https://jsfiddle.net/cmedina/7kfmyw6x/69/

这篇关于如何按DataTables中的隐藏列排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆