Display cell value in tooltip after hovering over a cell in DT::datatable

ぃ、小莉子 提交于 2019-12-01 13:21:18

问题


How do I utilize javascript to display the cell value in a tooltip after hovering over a particular cell in a DT::datatable? I decided to hide the long text after a certain width is reached (overflow-x: hidden; white-space: nowrap;) to maintain a clean format, and I would like the user to be able to see the full text if they choose to hover over a given cell.

datatable(df,
          class="compact",
          selection="none",
          rownames=F,
          colnames=NULL,
          options=list(dom="t",
                       pageLength=10
          ),
          escape=F)

回答1:


Could you try this:

datatable(head(iris), 
          options=list(initComplete = JS("function(settings) {var table=settings.oInstance.api(); table.$('td').each(function(){this.setAttribute('title', $(this).html())})}")))

This sets a tooltip for every cell.

To set the tooltip for a single specific cell:

datatable(head(iris), 
          options=list(initComplete = JS(
            "function(settings) {
            var table = settings.oInstance.api();
            var cell = table.cell(2,2);
            cell.node().setAttribute('title', cell.data());
            }")))



回答2:


Here is a solution with the newly available plugin ellipsis.

library(DT) # version 0.5

dat <- data.frame(
  A = c("fnufnufroufrcnoonfrncacfnouafc", "fanunfrpn frnpncfrurnucfrnupfenc"),
  B = c("DZDOPCDNAL DKODKPODPOKKPODZKPO", "AZERTYUIOPQSDFGHJKLMWXCVBN")
)

datatable(
  dat, 
  plugins = "ellipsis",
  options = list(
    columnDefs = list(list(
      targets = c(1,2),
      render = JS("$.fn.dataTable.render.ellipsis( 17, false )")
    ))
  )
)

Documentation of the plugin: https://datatables.net/plug-ins/dataRender/ellipsis



来源:https://stackoverflow.com/questions/52022957/display-cell-value-in-tooltip-after-hovering-over-a-cell-in-dtdatatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!