Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

css - Image popup on hover in DT in R

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 distorts the datatable.

It increases the table rows height to fit the image. I have tried to reduce the row sizes via css but with no luck.

This is the Rmarkdown I have so far:

---
title: "Untitled"
author: "dimitris_ps"
date: "3 September 2016"
output: html_document
---

<style type="text/css"> 

  .imgTooltip {
      visibility: hidden;
}

  .ItemsTooltip:hover .imgTooltip {
      visibility: visible;
}

  td {
      height: 14px;
}

</style>

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(DT)

df <- structure(list(a = c("<a class="ItemsTooltip" href="http://www.example.com" target="_blank"><img class="imgTooltip" src="https://i.stack.imgur.com/uSSEu.jpg"/>my stackoverflow Avatar</a>", 
"<a class="ItemsTooltip" href="http://www.example.com" target="_blank"><img class="imgTooltip" src="https://i.stack.imgur.com/uSSEu.jpg"/>my stackoverflow Avatar</a>"
), b = c("<a class="ItemsTooltip" href="http://www.example.com" target="_blank"><img class="imgTooltip" src="https://i.stack.imgur.com/uSSEu.jpg"/>my stackoverflow Avatar</a>", 
"<a class="ItemsTooltip" href="http://www.example.com" target="_blank"><img class="imgTooltip" src="https://i.stack.imgur.com/uSSEu.jpg"/>my stackoverflow Avatar</a>"
)), .Names = c("a", "b"), row.names = c(NA, -2L), class = "data.frame")
```

```{r}
datatable(df, escape=c(FALSE, FALSE))
```
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>";
    }'
    )
  ))))
```

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...