3zzy 3zzy - 2 months ago 6
Javascript Question

Giving custom classes to each TD for styling - Datatables & jQuery

I'm using datatables for displaying server-side data in tables.

I can't target and style individual cells (

<TD>
) though. I search a bit and found it might be possible with:

"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
....
}


... but I'm not quite sure how because I have a few table and not all have the same number of columns and rows. I want to give common class to all
TDs
of a 'column'.

Answer

You can use sClass parameter in Columns definition. For example, if you have 3 columns and want to pass custom class for second and third column, you can:

"aoColumns": [
    null,
    { "sWidth": "95px", "sClass": "datatables_action" },
    { "sWidth": "45px", "sClass": "datatables_action" }
]

You can check datatables documentation