Reputation: 2570
I tried to color a whole row inside my datatable with the fnRowCallback argument, the effect works but other DT functions wont work anymore. Here's a simple example:
names <- c("a","b","c","d")
days <- c(10,5,2,1)
dat <- data.frame(name=rep(names,days),date=as.Date((Sys.Date()-sum(days)+1):Sys.Date(),origin="1970-01-01"),value=rep(days,days),value2=(rep(days*2,days)))
datatable(dat,options=list(
fnRowCallback = JS('function(row,data, iDisplayIndex, iDisplayIndexFull){
var d = data[2];
d = new Date(d);
c = new Date(+new Date - 12096e5);
if (d > c)
$(row).css("background-color", "blue");
}'),
rowCallback = JS(
'function(row, data) {
if (parseFloat(data[4]) > parseFloat(data[3]))
$("td:eq(4)", row).css("background-color", "green");
}')
)) %>% formatCurrency(c("value","value2"))
Both fnRowCallback and rowCallback will result in only the effect of fnRowCallback, also formatCurrency is ignored. If you comment out fnRowCallback formatCurrency will work fine together with the rowCallback argument.
This might be a problem with the DT package, but as I'm not that familiar with the javascript datatables library there might be a problem inside my code or an option I'm missing to enable..
Upvotes: 0
Views: 831
Reputation: 21425
fnRowCallback
is the Datatable v1.9 version of Datatable v1.10 rowCallback
(info here). You are essentially setting rowCallback
twice in your code which is probably causing the issue.
Try putting all your code in the rowCallback
:
datatable(dat,options=list(
fnRowCallback = JS('function(row,data, iDisplayIndex, iDisplayIndexFull){
}'),
rowCallback = JS(
'function(row, data) {
if (parseFloat(data[4]) > parseFloat(data[3]))
$("td:eq(4)", row).css("background-color", "green");
var d = data[2];
d = new Date(d);
c = new Date(+new Date - 12096e5);
if (d > c)
$(row).css("background-color", "blue");
}')
)) %>% formatCurrency(c("value","value2"))
The rowCallback
function is applied to each row before they are rendered (docs here).
Upvotes: 2