Reputation: 501
I'd like to specify the columns/rows that I want to apply my callback function (In short, I'm trying to add a tooltip only for specific cells in my Datatable). Here's a part of the code where I'm trying to address this issue:
output[[(paste("table_", name, sep=""))]] <- DT::renderDataTable({
DT::datatable(content[[name]]$data,
container = sketch,
rownames = FALSE,
filter = "top",
options = list(
columnDefs = list(list(
targets = rules_indexes[[name]]-1, #I would like to specify this for the callback below
render = JS(
"function(data, type, row, meta) {",
"return type === 'display' && ",
"'<span title=\"' + 'span' + '\">' + data +'</span>';",
"}") #this approach doesn't work when the elements have no content (nothing to hover)
))
),
callback = JS("
table.on('mouseenter', 'tbody td', function() {
var column = $(this).index();
var row = $(this).parent().index();
if ( row==0){
this.setAttribute('title', 'callback');}
});
return table;
") #this approach works but I don't know how to specify the rows/columns to apply this callback to.
)%>%
formatStyle(columns = rules_indexes[[name]],backgroundColor = styleEqual("","crimson"))
})
}
Upvotes: 0
Views: 302
Reputation: 84529
I'm trying to add a tooltip only for specific cells in my Datatable
You can do like this:
library(DT)
# we will place a tooltip at cell(1,1) and at cell(3,2)
rows <- "[1,3]"
cols <- "[1,2]"
tooltips <- "['foo','bar']"
datatable(head(iris),
options=list(initComplete = JS(c(
"function(settings){",
sprintf(" var rows = %s;", rows),
sprintf(" var cols = %s;", cols),
sprintf(" var tooltips = %s;", tooltips),
" var table = settings.oInstance.api();",
" for(var i=0; i<rows.length; i++){",
" var cell = table.cell(rows[i],cols[i]);",
" cell.node().setAttribute('title', tooltips[i]);",
" }",
"}")))
)
Upvotes: 1