How do you change the style of cell in a JQuery.DataTable?

Sephrial picture Sephrial · May 6, 2010 · Viewed 73.4k times · Source

I have a question about setting the style attributes for a data cell in the jQuery.DataTable. I was able to set the width for each column when initializing the dataTable using the following code:

oTable = $('#example').dataTable( {
    "aoColumns" : [ 
        { sWidth: '40%' }, 
        { sWidth: '60%' }
    ]
} );

Now I want to change the alignment for the second column like so: style="text-align: right;".

I am adding rows dynamically using this code:

/* Global var for counter */
var giCount = 2;

function fnClickAddRow() {
    oTable.fnAddData( [
        'col_1', 
        'col_2' ] );

    giCount++;  
}

Can you tell me how can I select the second cell of the new row after it's been inserted OR how to set the style of the row before/during insertion?

Any help would be greatly appreciated!

Answer

Sephrial picture Sephrial · May 19, 2010

Cool, I am happy to report that I was able to answer my own question! I just defined a CSS style (alignRight), and added the style to the column like so:

<style media="all" type="text/css">
    .alignRight { text-align: right; }
</style>

oTable = $('#example').dataTable( {  
    "aoColumns" : [   
        { sWidth: '40%' },   
        { sWidth: '60%', sClass: "alignRight" }  
    ]   } );