【问题标题】:Reduce row height / margins in Datatable?减少数据表中的行高/边距?
【发布时间】:2019-10-06 15:04:45
【问题描述】:

我在一个闪亮的应用程序中使用了一个数据表,它对单元格进行了自定义着色。这是在 html 中完成的(每个单元格都是一个 div)并通过告诉 DT 不要转义这些特定的列。

使用 DT 看起来像这样: screenshot

我的问题是我希望着色占据每个单元格的整个高度,这样就没有边距。如果我可以让不同的单元格颜色相互接触,那就太好了。

我尝试添加margin: 0px; padding: 0px;,但没有任何区别。

我还尝试使用DT 中的formatstyle 来降低行高,如下所示: formatStyle( 0, target = 'row', lineHeight = '80%') 结果如下所示: screenshot 2

我目前正在尝试使用padding: 0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;,但效果并不好。 在我看来,这是来自DT 而不是我的div 的边距,因为无论我在 div 样式中尝试什么,颜色和行高限制之间的边距总是相同的。唯一的问题是我不知道如何控制它。

有人知道如何达到这样的结果吗? 提前感谢您的帮助。

使用的代码:

for (c in colnames(ranking)[10:13]) {
      ranking <- ranking %>%
        filter(param %in% input$param) %>%
        arrange_(.dots = c) %>%
        mutate(!!paste0(c, "_rk") := 1:nrow(ranking %>% filter(param %in% input$param)))
      tmp <- ranking %>%
        arrange_(.dots = c) %>%
        select_(.dots = c)

      max <- tmp %>% tidyr::drop_na() %>% .[, 1] %>% max()
      min <- tmp %>% tidyr::drop_na() %>% .[, 1] %>% min()
      range <- max - min
      brks <- vector(length = colors)

      for (i in 1:colors) {
        brks[i] <- i^pracma::bisect(function(x) range^(1/x) - (colors + 1), 1, 5)$root %>% round(2) + min - 2
      }

      tmp <- tmp %>% 
        mutate(brks = ifelse(is.na(tmp[, 1]),
                             NA,
                             cut(tmp %>% tidyr::drop_na() %>% .[, 1], brks)))
      colfunc <- colorRampPalette(c("#c31432", "#ffc500", "#edde5d", "white"))
      clrs <- colfunc(colors + 1)

      tmp_nrow <- tmp %>% nrow()

      for (i in 1:tmp_nrow) {
        row <- which(tmp[i, 1] == ranking[,c])

        r <- clrs[tmp[i, 2]] %>% col2rgb() %>% .[1]
        g <- clrs[tmp[i, 2]] %>% col2rgb() %>% .[2]
        b <- clrs[tmp[i, 2]] %>% col2rgb() %>% .[3]

        tmp[i, 1] <- paste0("<center><div style='background: ", "radial-gradient(rgba(", r, ",", g, ",", b, ",", "0), rgba(", r, ",", g, ",", b, ",", "0.25), rgba(", r, ",", g, ",", b, ",", "1)", ")", "; border: solid 0px;font-family: \"Interstate Black\";font-weight: bolder;padding: 0;margin: 0;'>",
                            tmp[i, 1],
                            "</div></center>")

        ranking[row,paste0(c, "_coloring")] <- tmp[i, 1]
      }
    }

        ranking_m <- as.matrix(ranking %>%
                             filter(param %in% input$param) %>%
                             select(4, 47, 40, 38, 31, 32, 41, 42, 43, 44))

    DT::datatable(ranking_m,
                  escape = c(TRUE, FALSE, rep(FALSE, 8)),
                  filter = 'top',
                  extensions = list('Responsive' = NULL),
                  options = list(pageLength = 25,
                                 lengthMenu = c(10, 25, 50, 100),
                                 columnDefs = list(list(width = '400px', targets = 0),
                                                   list(width = '25px', targets = 1),
                                                   list(className = 'dt-center', targets = 2:9)))) #%>%
      # formatStyle( 0, target = 'row', lineHeight = '80%')

【问题讨论】:

  • 您是否特别需要为此使用数据表?还有其他软件包可以做到这一点。
  • 我从 htmlTableWidget 移到了 DT,所以我只能使用 DT 来处理表格,以及非常棒的过滤和排序。通常情况下,htmlTableWidget 没有出现此边距问题,但无法进行排序或过滤。你心目中的套餐是什么?
  • 你是如何设置颜色的?你能提供你的代码吗?
  • 它主要是一个径向渐变,带有自定义颜色,全部采用 div 的样式。它看起来像这样:paste0("&lt;center&gt;&lt;div style='background: ", "radial-gradient(rgba(", r, ",", g, ",", b, ",", "0), rgba(", r, ",", g, ",", b, ",", "0.25), rgba(", r, ",", g, ",", b, ",", "1)", ")", "; border: solid 0px;font-family: \"Interstate Black\";font-weight: bolder;padding: 0px;margin:0px;'&gt;", tmp[i, 1], "&lt;/div&gt;&lt;/center&gt;") tmp[i,1] 是每个单元格中显示的数字。
  • 我在想tableHTML 和函数add_css_conditional_column。您可以完全控制着色(完全按照您的意愿进行排名 - 颜色跨越整个单元格),但它错过了排序和过滤的 JS 功能。如果排序/过滤不是问题,我可以给你看一个例子

标签: html r shiny datatables dt


【解决方案1】:

背景 CSS 必须设置为单元格,而不是单元格内容。这可以通过formatStyle 来实现。这是一个随机颜色的例子:

library(DT)
dat <- iris[1:5,]
ncols <- ncol(dat)

# background for column 1
r <- sample.int(256, 5, replace = TRUE) - 1L
g <- sample.int(256, 5, replace = TRUE) - 1L
b <- sample.int(256, 5, replace = TRUE) - 1L
dat$RGB1 <- sprintf("radial-gradient(rgba(%s,%s,%s,0),rgba(%s,%s,%s,0.25),rgba(%s,%s,%s,1))", 
                   r, g, b, r, g, b, r, g, b)

# background for column 2
r <- sample.int(256, 5, replace = TRUE) - 1L
g <- sample.int(256, 5, replace = TRUE) - 1L
b <- sample.int(256, 5, replace = TRUE) - 1L
dat$RGB2 <- sprintf("radial-gradient(rgba(%s,%s,%s,0),rgba(%s,%s,%s,0.25),rgba(%s,%s,%s,1))", 
                    r, g, b, r, g, b, r, g, b)

# background for column 4
r <- sample.int(256, 5, replace = TRUE) - 1L
g <- sample.int(256, 5, replace = TRUE) - 1L
b <- sample.int(256, 5, replace = TRUE) - 1L
dat$RGB4 <- sprintf("radial-gradient(rgba(%s,%s,%s,0),rgba(%s,%s,%s,0.25),rgba(%s,%s,%s,1))", 
                    r, g, b, r, g, b, r, g, b)

datatable(dat, 
          options = 
            list(
              columnDefs = 
                list(
                  list(visible = FALSE, targets = ncols + 1:3),
                  list(className = "dt-center", targets = 1:ncols)
                )
            )) %>%
  formatStyle(1, valueColumns = ncols+1, background = JS("value")) %>%
  formatStyle(2, valueColumns = ncols+2, background = JS("value")) %>%
  formatStyle(4, valueColumns = ncols+3, background = JS("value")) %>%
  formatStyle(1:ncols, `font-family` = "Interstate Black") %>% 
  formatStyle(1:ncols, fontWeight = "bolder")

【讨论】:

  • 感谢 Stéphane :) 我将自己测试它,因为它需要一些调整,以便我可以让它与我现有的代码一起工作,但这看起来像答案:)
  • 只是为了报告它按预期工作。我什至使用styleEqual 在一些formatStyle 中添加了条件格式。最后,代码比以前更容易理解,列类型现在是数字(应该如此),但它是我的旧代码的字符,对过滤不友好。再次感谢
猜你喜欢
  • 2017-06-25
  • 1970-01-01
  • 2011-05-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
  • 2012-11-01
  • 1970-01-01
相关资源
最近更新 更多