简体   繁体   中英

How to change first column background color in Datatables

Hello I have below jsp,

<div id="dynamic">
        <table cellpadding="0" cellspacing="0" border="0" class="display"
            id="example">
            <thead>
                <tr>
                    <th width="10%">First Name</th>
                    <th width="10%">Last Name</th>
                    <th width="10%">Address 1</th>
                    <th width="10%">Address 2</th>
                </tr>
            </thead>
        </table>
    </div>

The above code is synced with JQuery Data tables. When I am running it the sorted column is having different color. But I want it to change when I select a row. Could you please help in this?

Good contribution from wi7sonjoseph it was perfect

     "rowCallback": function( row, data ) {
        $('td', row).eq(2).addClass('warningcolumn');
        $('td', row).eq(3).addClass('warningcolumn');
        $('td', row).eq(15).addClass('warningcolumnok');
        $('td', row).eq(16).addClass('warningcolumnok');
if ( data.OPERATIVIDAD == 1) {
  $('td:eq(6)', row).html('<span class="label label-success">OPERATIVO</span>');
}
else {
  $('td:eq(6)', row).html('<span class="label label-danger">INOPERATIVO</span>');
}}

在此处输入图片说明

There is also a good contribution here. https://jsfiddle.net/jlujan/r041f7mw/

First column with different color is due to the class display used by datatable. Remove class display from table will do the trick

Refer Datatable Styling to know more about its styling

To change the color of the first colunm, try this:

$(document).ready(function(){
    $("#example tr").children("td").first().css({"background-color":"#C94BCB"}); // Choose your color!
});

About hover ... It's unclear.

Just use CSS:-

td:first-child {  
  /* your stuff here */
  background-color:#ffffff;    
}

If you need for nth-child you can specify like this

td:nth-child(n) {  
  /* your stuff here */
  background-color:#ffffff;
}

Reference Link:- CSS First-child

Use td and th first-child style

th:first-child,td:first-child {  
  background-color:red;    
}

 <!DOCTYPE html> <html> <body> <style> th:first-child,td:first-child { background-color:red; } </style> <div id="dynamic"> <table cellpadding="0" cellspacing="0" border="0" class="display" id="example"> <thead> <tr> <th width="10%">First Name</th> <th width="10%">Last Name</th> <th width="10%">Address 1</th> <th width="10%">Address 2</th> </tr> <tr> <td width="10%">First Name</td> <td width="10%">Last Name</td> <td width="10%">Address 1</td> <td width="10%">Address 2</td> </tr> <tr> <td width="10%">First Name</td> <td width="10%">Last Name</td> <td width="10%">Address 1</td> <td width="10%">Address 2</td> </tr> <tr> <td width="10%">First Name</td> <td width="10%">Last Name</td> <td width="10%">Address 1</td> <td width="10%">Address 2</td> </tr> </thead> </table> </div> </body> </html>

Use rowCallBack (Use aData if you want to check some specific field to change color)

"rowCallback" : function(nRow, aData, iDisplayIndex) {
            if (aData != null && aData != "") {
                  $('td', nRow).eq(0).css({color: "#f91212"});
            }
          },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM