Control row stripe color in DT datatable output

I haven't got a good handle on what the "best" way is to set a custom row stripe / row striping color in datatable (DT) .

I'm using datatable (DT) to create interactive tables (e.g html via rmarkdown), with download ability etc. That's why I'm not using huxtable or any other table formatting package, and ideally I don't have to style first in another package before sending to datatable - though open to this if necessary/best available option.

Here is an self contained example of row striping, modified from example shown here, answer#1 ( One issue is that the hover color no longer works, though it is meant to be a tint applied to whatever is there? Another is that the row color stays with the row after re-ordering by column - not sure if this default behavior is what I want - is it easy to change?

  DT::datatable(head(iris, 20),
                extensions = 'Buttons',
                options=list(dom = 'Bfrtip',
                             buttons = c('excel','csv','print'),
                               'function(row,data) {
     if($(row)["0"]["_DT_RowIndex"] % 2 <1) 

There's an option here to color background of cells by value, which is not what I'm after:

Datatables(dot)net has an example stylesheet that creates a whole pile of CSS (357 lines of code), which presumably could be used in a style.css file to do what is required (row striping, custom color, but which seems overkill to me, when presumably the secret sauce (for my purpose) is just this line (below). Note, default behavior of the datatable example has striping independent of the initial row number (c.f code above where striping stays with the original row data). Can I get the code below into the r datatable code directly, e.g. like the example above?

table.dataTable.stripe tbody tr.odd, table.dataTable.display tbody tr.odd {`
background-color` `: ` `#f9f9f9` `; }`

Here's the answer:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.