Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add class to jquery.datatables columns?

I made a large table for jquery.datatables which is works great for me.
but i need a class name set to each td element relative to its column.
fo example i want a column (including th and all td's) have a class="volume".
there is this issues:
i use this code to initialize the class but it is not working.

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

EDIT: my table is created and refeshes dynamically. and it is made of a js-array which i prefer not to touch it ie. just to add class names
EDIT:
iuse this code to itialize my table:

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

i hope it helps *EIDT: *
columnsHd is an array which is created dynamicaly from my json headers and now is exactly:

[
{ "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" },
 ] 
like image 395
Homam Avatar asked Jan 09 '13 09:01

Homam


2 Answers

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

like image 188
Daniel Avatar answered Oct 23 '22 15:10

Daniel


The answer for the question is

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

to use other options: (ref datatable)

enter image description here

like image 39
3 rules Avatar answered Oct 23 '22 16:10

3 rules