set class on th cell?

set class on th cell?

mihomesmihomes Posts: 165Questions: 23Answers: 0

Doing some revamping of my code. I previously did something like this :

<table class="table table-striped table-bordered" id="datatable">
    <thead>
        <tr>                                    
            <th class="text-center">One</th>
            <th>Two</th>
            <th>Three</th>
            <th>Four</th>
            <th>Five</th>
        </tr>
    </thead>
    <tbody></tbody>
</table>

Now, I am declaring those titles in my js and eliminating the html contents of the table :

<table class="table table-striped table-bordered" id="datatable"></table>

then in js :

"columns": [ 
    {
        "title": "One",
        "data": null,
        "class": "text-center dtCheck",
        "searchable": false,
        "orderable": false,
        "defaultContent": '<i class="fa fa-square-o dt-checkboxes"></i>'
    },
        ... and so on...

The problem I have run into is I need to set classes on my thead cells, in this case, I need to apply 'text-center', but it could be anything. class only applies to the tbody cells that display data results... is there a way to define a class on the header cell similar to class?

This is server-side obviously.

Answers

  • allanallan Posts: 63,696Questions: 1Answers: 10,500 Site admin

    Use a little bit of jQuery in initComplete to select the cells and add the classes, or use column().header() to get the cell and then a bit of jQuery to add the class.

    Allan

  • mihomesmihomes Posts: 165Questions: 23Answers: 0
    edited August 2015

    Thanks Allan... I was thinking there might be something built-in, but couldn't find anything. I was trying to trim code rather than add more... I am just keeping the table outline in the html for now. The only reason I was going to do this in the first place was it would eliminate editing the html when I made changes and could just do so directly in the js.

This discussion has been closed.