DT单元悬停显示隐藏列中基于单元的样本大小 [英] DT cell hover showing cell based sample sizes from hidden column

查看:70
本文介绍了DT单元悬停显示隐藏列中基于单元的样本大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾问过如何根据隐藏列中存储的颜色为单元格上色(链接)。我看到也可以通过此(DT)表应用悬停信息帖子。

I have previously asked how to colour cells based on colours stored in hidden columns (link). I saw that it is also possible to apply hover information for (DT) tables via this and this post.

我想扩展我的初始帖子,在其中添加悬停选项以显示与单个单元格相关的样本数量。这些样本数量未显示在表格中(即隐藏),仅在悬停时显示。我真的在推动我对Java的了解,以使这项工作得以实现。

I want to expand my initial post where I want to add the hover option to display the sample sizes related to the individual cells. These sample sizes are not shown in the table (i.e. hidden) but only display on hover. I am really pushing my knowledge of Java to make this work.

从我的第一篇文章开始,输入数据框可能看起来像:

Following on from my initial post the input data frame could look like:

dat <- iris[1:5,1:5]
colours2apply <- sample(x=c(rgb(1, 0, 0 ), rgb(1, 1, 0 ), rgb(0, 1, 1 )), 25, replace = T) %>% 
  matrix(nrow=5) %>% 
  data.frame()
set.seed(1234)
SampleSizesToShowInHover <- matrix(round(runif(n = 25, 10, 1000)), nrow=5)

  dat <- cbind(dat, colours2apply)
  dat <- cbind(dat, SampleSizesToShowInHover)
dat

从上一篇文章的答案中,这段代码添加了基于单元格的颜色:

From the answer in my previous post, this code adds the cell based colouring:

DT <- datatable(dat, 
                options = list(columnDefs = list(list(visible=FALSE, targets = 6:10))))
for(i in 1:5){
  DT <- DT %>%
    formatStyle(i, valueColumns = i+5, backgroundColor = JS("value"))
}
DT

如何在着色之外添加基于单元格的悬停信息?
非常感谢任何帮助!

How do I add the cell based hovering information in addition to the colouring? Any help very much appreciated!

致谢,

Luc

编辑:
在答案,以前的帖子和各种评论之后,我使用的最终解决方案是:

The final solution I used following the answer, previous posts and various comments was:

solution <- datatable(dat, 
                options = 
                  list(
                    columnDefs = list(
                      list(
                        visible=FALSE, 
                        targets = 6:15
                      )
                    ), 
                    rowCallback = JS(
                      "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
                      'for(i=0; i<5; i++ ){',
                      "var full_text = 'n = '+ aData[i+10];",
                      "$('td:eq('+i+')', nRow).attr('title', full_text).css('background-color', aData[i+5]);",
                      '}',
                      "}")


                  )
)
solution


推荐答案

您可以简单地向选项参数添加行回调,以从隐藏列中获取toopltip。像这样的东西:

You could simply add a rowcallback to option paramters to get the toopltip from hidden columns. Something like this:

DT <- datatable(dat, 
                options = list(columnDefs = list(list(visible=FALSE, targets = 6:10)), rowCallback = JS(
                  "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
                  "$('td:eq(1)', nRow).attr('title',aData[1+5]);",
                  "$('td:eq(2)', nRow).attr('title',aData[2+5]);",
                  "$('td:eq(3)', nRow).attr('title',aData[3+5]);",
                  "$('td:eq(4)', nRow).attr('title',aData[4+5]);",
                  "$('td:eq(5)', nRow).attr('title',aData[6+5]);",
                  "}")))

您可以循环执行以下操作:

You can do the same thing in loop as follows:

DT <- datatable(dat, 
                options = list(columnDefs = list(list(visible=FALSE, targets = 6:10)), rowCallback = JS(
                  "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
                  'for(i=0; i<5; i++ ){',
                  "$('td:eq('+i+')', nRow).attr('title',aData[i+5]);",
                  '}',
                  "}")))

希望它会有所帮助!

这篇关于DT单元悬停显示隐藏列中基于单元的样本大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆