How do I style table cells (by column)?

How do I style table cells (by column)?

rgavrilovrgavrilov Posts: 13Questions: 6Answers: 0
edited January 2011 in General
I need to style columns, specifically I'm trying to replace my existing rule:
[code]
table#my-table td.column-2 { text-align: center; }
[/code]

Since is being generated for me by the plugin, I can't figure out how to target second column cells.

My current solution is to wrap every cell content into [code][/code] but there got to be better way.

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    There certainly is :-) How about this:

    [code]
    $('#example').dataTable( {
    "aoColumnDefs": [
    { "sClass": "column-2", "aTargets": [ 1 ] }
    ]
    } );
    [/code]
    That will add the class "column-2" to the TD elements in the second column (i.e. index 1).

    Regards,
    Allan
  • webguruwebguru Posts: 16Questions: 0Answers: 0
    how would one add the same class to both the td and th of the same column?
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    To add a class to the TH element you need to use standard jQuery or DOM methods ( $('th:eq(2)').addClass() for example).

    Allan
  • webguruwebguru Posts: 16Questions: 0Answers: 0
    ok I figured. I just wanted to make sure there wasn't a "correct" way of doing it first. Thanks :)
This discussion has been closed.