javascript - 如何指定行/列以应用 R Shiny 的回调函数?
问题描述
我想指定我想应用我的回调函数的列/行(简而言之,我试图为我的数据表中的特定单元格添加一个工具提示)。这是我试图解决此问题的代码的一部分:
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"))
})
}
解决方案
我正在尝试仅为我的数据表中的特定单元格添加工具提示
你可以这样做:
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]);",
" }",
"}")))
)
推荐阅读
- scala - 如何添加自定义指标来激发 mlib 评估器?
- android - 如何创建具有指定边框线宽度的布局?
- java - Java Stream Filter 问题将流式值与所有列表值进行比较
- shell - 在 shell 中复制查找操作的结果
- python - 为什么这个 for 循环在删除列表中的所有项目之前停止?
- sql - SQL查询查找事实表中的趋势
- javascript - 如何更改对象?
- python - 如何迭代列表列表并更改元素值?
- copy-protection - 我们应该添加到 UWF 写入过滤器例外列表中的所有 NGen 工件的列表是什么?
- c# - Crystal Reports - 如何在 C# 中将公式字段文本传递给子报表?