如何将类添加到jquery.datatables列?

时间:2013-01-09 09:08:36

标签: javascript jquery html css datatables

我为jquery.datatables制作了一张大桌子,这对我很有用 但是我需要为每个td元素设置一个相对于其列的类名 例如,我想要一个列(包括th和所有td)都有一个class="volume" 有这个问题:
 我使用此代码初始化类,但它不起作用。

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

修改 我的表被创建并动态刷新。它是由一个js阵列组成,我宁愿不接触它,即。只是为了添加类名 修改
我使用这段代码来表示我的表:

$('#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" },
 ] 

2 个答案:

答案 0 :(得分:10)

我的猜测是"aoColumns": columnsHd, overrides the "aoColumnsDefs": [ { "sClass": "volume", "aTargets": [2] } , ],

尝试在代码中替换他们的订单

或者直接在"aoColumns": columnsHd,中添加课程 像这样"sClass": "volume"

完整代码:

尝试将{ "sTitle": "ip" },更改为{ "sTitle": "ip", "sClass": "volume" },

并删除

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

请注意,在datatables 1.10中,您应该使用aoColumnDefs

答案 1 :(得分:1)

问题的答案是

PIVOT

使用其他选项:(ref datatable

enter image description here