changing row color at rendering time based on column values
changing row color at rendering time based on column values
michael@hockstein.org
Posts: 7Questions: 0Answers: 0
is there and example of how to change the background color of an entire row, at the time of initial rendering, based on the initial values of the data in a row's columns?
This discussion has been closed.
Replies
Use
createdRow
for this. There is an example in the docs and a running example.Kevin
OK. I started with something simple as a test. This is buried in the body starting with $('#example').DataTable({...
This was supposed to change every column of every row red. However, it changes every column red on every row except the first column!
Once I figure this out I can use the data for some logic. It's probably a jQuery mistake. Any suggestions?
See if this thread helps.
Kevin
No doubt I'm moving forward. But still failing.
I added the following styles:
table#example.dataTable tbody tr.Highlight > .sorting_1 {
background-color: rgb(245, 10, 10);
}
table#example.dataTable tbody tr.Highlight {
background-color: rgb(245, 10, 10);
}
Unfortunately, even with the .addClass commented out (see below), the rows are now always red. Sort of odd as the style should only be invoked if there element has the Highlight class (which I never assigned). Interesting, if you change the style name from Highlight to myStyle, the color is not assigned suggesting that deep in the DataTable code the class Highlight is being assigned. Unfortunately, even with a name reassignment to the style, the addClass doesn't change the color.
Could be that you are using a styling framework and the CSS selectors might be something different. You can use Inspect to see what is being used. Or you can update the example or create your own or post a link to your page so we can take a look to help with suggestions.
Kevin
The class of the table:
<
table id='example' class='design' style='width: 50%;'>
If you remove class='design' then the classes I created and the addClass function work. Unfortunately you lose the rest of the basic style like every other row highlighting and row dividers
I haven't seen the class
design
before with Datatables. Where does it come from? Is it for a styling framework?Kevin
Sorry, display. Typing fast
There must be something else as the example in the other thread has
display
. Without a link to your page or a test case showing the issue it will be hard to offer suggestions.Did you use the browser's Inspect (right click on the row > Inspect) to see what is being applied and/or overridden?
Kevin
I did. You can see the styles applied all the way down to tr.odd and tr.even at the very inside of the table.
No worries. One day I'll figure it out. Thanks for all your help.
A new day and a new way.
Honestly, I can't tell what's different but it works like it the example. Thanks for your help
I did it using rowCallback. When adding the class, I specified background-color: #6BFFB5 !important; and everything works.
rowCallback: function ( row, data, cell ) {
// Set the checked state of the checkbox in the table
$('input.editor-set_h', row).prop( 'checked', data.kontragent.set_h == 1 );
if(data.kontragent.set_h == 1){
$(row).addClass('non');
} else {
$(row).removeClass('non');
}
$('input.editor-block', row).prop( 'checked', data.kontragent.block == 1 );
if(data.kontragent.block == 1){
$(row).addClass('block');
} else {
$(row).removeClass('block');
}
}
I am also using rowCallback for these purposes - and it always works. Just adapted the example to use it: http://live.datatables.net/jowoxuhu/1/edit
https://datatables.net/reference/option/rowCallback
And another, more complex example from my own coding to highlight how to use "data" and "row"