Nadrieci
Nadrieci

Reputation: 66

Hover header instead of fixedHeader

I'm dealing with this issue. I had tried some css solutions but i didn't manage to solve it.

I finally decided to hover each cell in order to display rownames(first column) and colnames(header).

The following code does the trick 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(', ');
    });",
                      "}")
                    )
                    )
    })
  }
)

Unfortunately this seems to be incompatible with 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')))
    })
  }
)

When I perform that extra step, I get neither an error nor the datatable rendered.

I'm looking for either: be able to mix rowCalllback and Styleinterval or in general, a better solution to let the user know where does exactly he is on a large datatable.

Thank you in advance.

Upvotes: 0

Views: 45

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

I would do the tooltips in this way:

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

Upvotes: 1

Related Questions