如何指定行/列应用[R闪亮的回调函数?

问题描述 投票:0回答:1

我想指定列/行,我想申请我的回调函数(总之,我想在我的DataTable中添加工具提示只为特定的细胞)。这里就是我试图解决这个问题的代码的一部分:

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"))
})

}

javascript r callback shiny dt
1个回答
1
投票

我想在我的DataTable中添加工具提示只为特定的细胞

你可以这样做:

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]);",
            "  }",
            "}")))
)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.