简体   繁体   English

R闪亮:数据表行的鼠标悬停文本

[英]R Shiny: Mouse Hover Text for Datatable Rows

Is there a way to display mouseover text upon hovering over a row (record) in datatable display? 有没有一种方法可以将鼠标悬停在数据表显示中的行(记录)上来显示文本? After going through some similar questions on StackOverflow, I found 2 example codes, one that displays hover text for a column cell and one that highlights the entire row on mouse hover. 经过关于StackOverflow的类似问题之后,我找到了2个示例代码,其中一个显示列单元格的悬停文本,另一个突出显示鼠标悬停时的整个行。

Example code for displaying column cell hover text: 用于显示列单元格悬停文本的示例代码:

library(shiny)
library(DT)

shinyApp(
  ui = fluidPage(

    DT::dataTableOutput("table2")

  ),
  server = function(input, output) {

    output$table2<-DT::renderDataTable({
      responseDataFilter2_home<-iris[,c(4,3,1)]
      displayableData<-DT::datatable(responseDataFilter2_home,options = list(rowCallback = JS(
        "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
        "var full_text = aData[1] + ','+ aData[2]",
        "$('td:eq(1)', nRow).attr('title', full_text);",
        "}")
      ))#, stringAsFactors = FALSe, row.names = NULL)
    },server = TRUE, selection = 'single', escape=FALSE,options=list(paging=FALSE,searching = FALSE,ordering=FALSE,scrollY = 400,scrollCollapse=TRUE,
                          columnDefs = list(list(width = '800%', targets = c(1)))),rownames=FALSE,colnames="Name")

    }
 )

在此处输入图片说明

I also found another code that highlights the entire row upon hover: 我还找到了另一个代码,该代码在悬停时突出显示了整个行:

Example code for highlight row on mouse hover 鼠标悬停时高亮行的示例代码

#rm(list = ls())
library(shiny)
library(DT)

ui <- basicPage(
  tags$style(HTML('table.dataTable.hover tbody tr:hover, table.dataTable.display tbody tr:hover {background-color: pink !important;}')),
  mainPanel(DT::dataTableOutput('mytable'))
)

server <- function(input, output,session) {

  output$mytable = DT::renderDataTable(    
    datatable(mtcars)
  ) 
}
runApp(list(ui = ui, server = server))

在此处输入图片说明

In my case, I wish to display text upon mouse hover over a row of the datatable. 就我而言,我希望在鼠标悬停在数据表的一行上时显示文本。 How shall I do that? 我该怎么办?

Here you go: 干得好:

library(shiny)
library(DT)

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

        output$table <- DT::renderDataTable({
            DT::datatable(iris, rownames = FALSE,
                          options = list(rowCallback = JS(
            "function(row, data) {",
            "var full_text = 'This rows values are :' + data[0] + ',' + data[1] + '...'",
            "$('td', row).attr('title', full_text);",
            "}")))
        })
    }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM