css - DT Shiny 不同的自定义列标题按列

标签 css r shiny dt htmltools

我的 CSS 技能非常有限,但假设有以下示例:

  sketch = htmltools::withTags(table(
     class = 'display',
     thead(
        tr(
           th(rowspan = 2, 'Species'),
           th(colspan = 2, 'Sepal'),
           th(colspan = 2, 'Petal')
        ),
        tr(
           lapply(rep(c('Length', 'Width'), 2), th)
        )
     )
  ))
  datatable(head(iris, 10), 
     container = sketch, options = list(
     initComplete = JS(
        "function(settings, json) {",
        "$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
        "}")
  ))

我如何将前两列标题的颜色编码更改为蓝色,以便列标题 Sepal,LengthSepal,Width 的两行都是蓝色,但将以下结构保留为另一种颜色 Petal,LengthPetal,Width

在 Stephane 的初步回答之后,我添加了一个示例。

example

最佳答案

您可以使用选项 headerCallback

datatable(head(iris, 10), 
          container = sketch, options = list(
            headerCallback = JS(
              "function( thead, data, start, end, display ) {
      $(thead).closest('thead').find('th').eq(3).css('color', 'red');
      $(thead).closest('thead').find('th').eq(4).css('color', 'red');
      $(thead).closest('thead').find('th').eq(5).css('color', 'blue');
      $(thead).closest('thead').find('th').eq(6).css('color', 'blue');
              }"
            ),
            initComplete = JS(
              "function(settings, json) {",
              "$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
              "}")
          ))

.closest('thead') 当 header 有多行时需要。

是你想要的吗?我不确定我是否正确理解了您的要求。

enter image description here


改变背景颜色:

library(DT)

sketch = htmltools::withTags(table(
  class = 'display',
  thead(
    tr(
      th(rowspan = 2, 'Species'),
      th(colspan = 2, 'Sepal'),
      th(colspan = 2, 'Petal')
    ),
    tr(
      lapply(rep(c('Length', 'Width'), 2), th)
    )
  )
))

headerCallback <- "function( thead, data, start, end, display ) {
  $(thead).closest('thead').find('th').eq(0).css('background-color', 'green');
  $(thead).closest('thead').find('th').eq(1).css('background-color', 'red');
  $(thead).closest('thead').find('th').eq(2).css('background-color', 'blue');
  $(thead).closest('thead').find('th').eq(3).css('background-color', 'red');
  $(thead).closest('thead').find('th').eq(4).css('background-color', 'red');
  $(thead).closest('thead').find('th').eq(5).css('background-color', 'blue');
  $(thead).closest('thead').find('th').eq(6).css('background-color', 'blue');
}"

datatable(head(iris, 10), 
          container = sketch, options = list(
            headerCallback = JS(headerCallback)
          )
)

enter image description here

关于css - DT Shiny 不同的自定义列标题按列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52151312/

相关文章:

javascript - Jquery 调整 li 大小以适应 ul

r - 使用圆形包创建半个极坐标图(玫瑰图)

r - 通过 Shiny 在布局之间进行响应式(Reactive)切换

css - `vp-preview-invisible` 类被添加到 Safari 中的预览缩略图,但不是在 Chrome 中的 vimeo 嵌入视频

html - 使用 Twig 将自定义 HTML 附加到与 form_row 相同的 div

jquery - 自动循环旋转图像

r - 更改 R 中时间序列图中的注释

r - 选择一个数据框中与另一数据框中的行部分匹配的行

R Plotly - 按标记的大小分配其颜色

r - 部署使用 fileInput 上传数据的 Shiny 应用程序时出错