悬停头代替fixedHeader

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

我处理这个issue。我曾尝试一些CSS解决方案,但我并没有设法解决它。

我最终决定每个小区悬停为了显示行的名称(第一列)和列(标题)。

下面的代码做的伎俩Working hover

shinyApp(
  ui = fluidPage(
    DT::dataTableOutput("mtcarsTable")
  ),
  server = function(input, output) {

    output$mtcarsTable <- DT::renderDataTable({
      DT::datatable(datasets::mtcars[,1:3], 
                    extensions = c('FixedColumns'), selection=list(mode="single", target="cell"), class = 'cell-border stripe', escape = F ,
                    options = list(rowCallback = JS(
                      "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
                      "var full_text = aData[0] + ','+ aData[1];",
                      "var headers = Array.prototype.slice.apply(document.querySelectorAll('th'));",
                      "$('td', nRow).each(function(i) {
                      this.title = [aData[0], headers[i].innerText].filter(Boolean).join(', ');
    });",
                      "}")
                    )
                    )
    })
  }
)

不幸的是,这似乎是与formatStyle不兼容:

shinyApp(
  ui = fluidPage(
    DT::dataTableOutput("mtcarsTable")
  ),
  server = function(input, output) {

    output$mtcarsTable <- DT::renderDataTable({
      DT::datatable(datasets::mtcars[,1:3], 
                    extensions = c('FixedColumns'), selection=list(mode="single", target="cell"), class = 'cell-border stripe', escape = F ,
                    options = list(rowCallback = JS(
                      "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
                      "var full_text = aData[0] + ','+ aData[1];",
                      "var headers = Array.prototype.slice.apply(document.querySelectorAll('th'));",
                      "$('td', nRow).each(function(i) {
                      this.title = [aData[0], headers[i].innerText].filter(Boolean).join(', ');
    });",
                      "}")
                    )
                    ) %>%
        formatStyle(columns = 1,  backgroundColor = styleInterval(c(19,20,22), c('red','green','yellow', 'black')))
    })
  }
)

当我执行额外的步骤,我没有得到一个错误,也没有渲染的数据表。

我在寻找之一:能够混合rowCalllback和Styleinterval或一般,更好的解决方案,让用户知道不正是他在一个大的数据表。

先感谢您。

javascript r shiny dt
1个回答
1
投票

我会做这样的提示:

library(DT)

datatable(head(iris), 
          options=list(initComplete = JS(c(
            "function(settings){",
            "  var table = settings.oInstance.api();",
            "  var ncols = table.columns().count();",
            "  var nrows = table.rows().count();",
            "  for(var i=0; i<nrows; i++){",
            "    var rowname = table.cell(i,0).data();",
            "    for(var j=1; j<ncols; j++){",
            "      var headerName = table.column(j).header().innerText;",
            "      var cell = table.cell(i,j);",
            "      cell.node().setAttribute('title', rowname + ', ' + headerName);",
            "    }",
            "  }",
            "}")))
)

enter image description here

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