1

Is it possible to highlight border of <tr> and can we append background-color to same <tr> inside the table?

Bhairav
  • 159
  • 3
  • 17

2 Answers2

1

According to this answer, you can style tr borders if border-collapse is set on the table.

CSS :

table {border-collapse:collapse;}
tr {border:2px solid black;}

HTML :

<table>
  <tr>
    <td>cell</td><td>cell</td><td>cell</td>
  </tr>
</table>

You can see the result in this fiddle.

I don't understand your question about tr bgcolors but maybe the fiddle above will give you the answer.

Community
  • 1
  • 1
Amessihel
  • 5,891
  • 3
  • 16
  • 40
  • ,Thanks for the response but when i am using the border-collapse:collapse; class i am getting some vertical lines on removing the class they are disappearing..if you provide solution for this that would be great. – Bhairav Jun 25 '15 at 11:34
0

Here is a "select" feature that highlights the entire row with a border - .odd / .even classes preserved :

table.dataTable tbody tr.selected td:first-child {
   border-left: 1px solid lime;
}
table.dataTable tr.selected td:last-child {
   border-right: 1px solid lime;
}
table.dataTable tbody tr.selected td {
   border-top: 1px solid lime;
   border-bottom: 1px solid lime; 
   background-color: yellow !important;
}
table.dataTable tbody tr.selected td:not(:first-child):not(:last-child) {
   border-left: 0px;
   border-right: 0px;  
}
$("#example").on('click', 'tbody td', function() {
    $(this).parent().toggleClass('selected');
});    

demo -> http://jsfiddle.net/z8bn8rj1/


Original answer. This is a little bit tricky since border-color not is supported by / valid for <tr>'s in CSS.

First remove the .odd / .even classes dataTables by default are adding to table rows, simply by giving asStripeClasses an empty array :

var table = $('#example').dataTable({
   asStripeClasses : []
});

Then, here an example of setting the border of <tr>'s lime, and the background-color yellow :

/* border-bottom of the headers, they appear as first row border-top */
table.dataTable thead th {
    border-bottom: 1px solid lime;
}    
/* set first column left border */
table.dataTable tbody td:first-child {
   border-left: 1px solid lime;
   border-top: 1px solid lime;
}
/* set last column right border */
table.dataTable td:last-child {
   border-right: 1px solid lime;
   border-top: 1px solid lime;
}
/* remove border between columns, alternatively set border as background-color */
table.dataTable tbody td:not(:first-child):not(:last-child) {
   border-top: 1px solid lime;
   border-left: 0x;
   border-right: 0px;  
}
/* set last row columns border-bottom */
table.dataTable tbody tr:last-child td {
    border-bottom: 1px solid lime;
}    
/* set all columns background-color */
table.dataTable tbody td {
    background-color: yellow;
} 

demo -> http://jsfiddle.net/jasjek8o/

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • Thanks david one small question like can we target it to alternate 's? – Bhairav Jun 26 '15 at 04:28
  • by default i need to show different background color for 's if i remove .odd /.even i cant show the different background color rather it would be same for all 's. – Bhairav Jun 26 '15 at 04:31
  • on target to particular 's i need to remove the .odd/.even class and append the new classes to highlight the 's border and background-color. so when i am doing that due to the presence of border-collapse attribute the class which i am trying to append is not affecting. when i am adding that attribute then i am getting vertical lines in the application – Bhairav Jun 26 '15 at 04:36
  • @Bhairav, OK - now I understand (I think) - have updated the answer with an example of selecting / unselecting entire rows by click, odd / even persists. You just have to change the colors. – davidkonrad Jun 26 '15 at 06:40
  • @Bhairav, did it help or should I delete the answer? – davidkonrad Aug 14 '15 at 23:08