7

我为 jquery.datatables 制作了一个大表,这对我来说非常有用。
但我需要为每个 td 元素相对于其列设置一个类名。
例如,我想要一个列(包括 th 和所有 td)有一个class="volume".
有这个问题:
我使用这个代码来初始化类,但它不工作。

"aoColumnsDefs": [
    { "sClass": "volume", "aTargets": [2] }
]

编辑: 我的表被创建并动态刷新。它是由一个 js-array 组成的,我不想碰它。只是添加类名
编辑:
我使用此代码来初始化我的表:

$('#dataTable').dataTable({
    "aaData": dataCnt,
    "aoColumnsDefs": [
        { "sClass": "volume", "aTargets": [2] }
    ],
    "aoColumns": columnsHd,
    "bStateSave": true,//saving status in coockie
    "iCookieDuration": 10,//coockie life duration in seconds
    "sScrollX": "100%",
    "sScrollY": (winHei-200),
    "sDom": '<"H"RCfrl>t<p"F"i>',
    "oColVis": {
        "buttonText": "&nbsp;",
        "bRestore": true,
        "sAlign": "left"
    },
    "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
});

我希望它有所帮助 * EIDT: *
columnsHd是一个从我的 json 标头动态创建的数组,现在正是:

[
{ "sTitle": "macaddr" },
{ "sTitle": "lat" },
{ "sTitle": "ip" },
{ "sTitle": "clientname" },
{ "sTitle": "relay0mask" },
{ "sTitle": "relay0stat" },
{ "sTitle": "relay1stat" },
{ "sTitle": "clientid" },
{ "sTitle": "bldname" },
{ "sTitle": "uptime" },
{ "sTitle": "current" },
{ "sTitle": "temperature" },
{ "sTitle": "softver" },
{ "sTitle": "volume" },
{ "sTitle": "hardver" },
{ "sTitle": "relay1mask" },
{ "sTitle": "pic" },
{ "sTitle": "comment" },
{ "sTitle": "lon" },
{ "sTitle": "rtt" },
{ "sTitle": "bldaddr" },
 ] 
4

2 回答 2

10

My guess is that "aoColumns": columnsHd, overrides the "aoColumnsDefs": [ { "sClass": "volume", "aTargets": [2] } , ],

Try to replace their order in code

Or just add the class directly in "aoColumns": columnsHd, like this "sClass": "volume"

complete code :

try changing { "sTitle": "ip" }, into { "sTitle": "ip", "sClass": "volume" },

and remove the

"aoColumnsDefs": [
    { "sClass": "volume", "aTargets": [2] }
],

Note that in datatables 1.10 you should use aoColumnDefs

于 2013-01-09T09:48:31.690 回答
1

这个问题的答案是

"aoColumnsDefs": [
    { "sClass": "classname", "aTargets": [whatever target you want to apply] }
]

使用其他选项:(参考数据表

在此处输入图像描述

于 2017-10-26T06:47:38.510 回答