Image popup on hover in DT in R

前端 未结 1 1707
伪装坚强ぢ
伪装坚强ぢ 2021-01-19 03:13

I have a DT within a Rmarkdown and I would like an image to pop up when hovering over table data.

What i have so for seems to work but it

相关标签:
1条回答
  • 2021-01-19 03:49

    Change your CSS to use display: none instead of visibility: hidden like so:

      .imgTooltip {
          display: none;
    }
    
      .ItemsTooltip:hover .imgTooltip {
          display: block;
    }
    

    If I were doing this I would probably use the datatable callback option instead of rendering the images in the cells, but I'd have to think about it some more.

    edit: Here is a cleaner version using columnDefs

    ---
    title: "Untitled"
    author: "CG"
    date: "6 September 2016"
    output: 
      html_document:
        md_extensions: +raw_html
    ---
    
    <style type="text/css"> 
    
    .imgTooltip {
          display: none;
    }
    
    .ItemsTooltip:hover .imgTooltip {
          display: block;
          position: absolute;
          z-index: 1;
    }
    
    </style>
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    library(DT)
    
    df <- data.frame(stringsAsFactors=FALSE,
                     a = rep("my stackoverflow Avatar",2),
                     b = rep("my stackoverflow Avatar",2))
    
    ```
    
    ```{r}
    datatable(df, options=list(columnDefs=list(list(
      targets=1:2,render=DT::JS(
        'function(data,row,type,meta) {
          return "<a class=\'ItemsTooltip\' href=\'www.example.com\' target=\'_blank\'><img class=\'imgTooltip\' src=\'https://i.stack.imgur.com/uSSEu.jpg\'/>" +
          data + "</a>";
        }'
        )
      ))))
    ```
    
    0 讨论(0)
提交回复
热议问题