DT datatable formatStyle when rownames = T, custom formatStyle is not working

DT datatable formatStyle when rownames = T, custom formatStyle is not working

我正在尝试使用数据表中的自定义容器为数据的最后一行着色。

只有在 datatable(dat,rownames = T) 中出现 rownames = T 时,我才有些期待,但这不是我需要的,我需要与 rownames = F 相同的结果,但这似乎没有任何作用.

请帮我解决下面的问题

The below code somewhat works, refer screenshot enter image description here

set.seed(1)
dat <- matrix(sample(c(0:9999), size = 54, replace = TRUE), nrow = 6) %>% as.data.frame()

sketch = htmltools::withTags(table(
  class = 'display',
  thead(
    tr(
      th(rowspan = 1, 'Customer'),
      th(colspan = 1, 'Transaction'),
      th(colspan = 11, 'Numbers')
    ),
    tr(
      lapply(colnames(dat), th)
    )
  )
))

datatable(dat,container = sketch,rownames = T,options = list(pageLength = 10, dom = 't')) %>%   # Works
  formatStyle(names(dat),
              background = styleInterval(c(0,5,10,50,100,500,1000,5000),
                                         c("#ffffff", "#f2fbd2", "#c9ecb4", "#93d3ab", "#35b0ab"
                                           ,"#E5B9ADFF","#E5B9ADFF","#D98994FF","#D0587EFF"))) %>%
  formatStyle(.,
              columns = 1:ncol(dat),
              valueColumns = 0,
              target = 'cell',
              backgroundColor = styleEqual(6, 'white'))

This doesn't work with the below code with rownames = F being the only change enter image description here

datatable(dat,container = sketch,rownames = F,options = list(pageLength = 10, dom = 't')) %>%   #Doesnt work

谢谢。

这很正常。 rownames 列是 1,2,3,4,5,6,您可以使用 rownames = FALSE 抑制此列。解决方案包括添加隐藏列 1,2,3,4,5,6。

set.seed(1)
dat <- matrix(sample(c(0:9999), size = 54, replace = TRUE), nrow = 6) %>% as.data.frame()
dat <- cbind(V0 = 1:6, dat) # added column V0 = 1:6

sketch = htmltools::withTags(table(
  class = 'display',
  thead(
    tr(
      th(colspan = 1, ''),
      th(colspan = 1, 'Customer'),
      th(colspan = 1, 'Transaction'),
      th(colspan = 7, 'Numbers')
    ),
    tr(
      lapply(colnames(dat), th)
    )
  )
))

datatable(
  dat,
  container = sketch,
  rownames = FALSE,
  options = list(
    pageLength = 10, 
    dom = 't',
    columnDefs = list(
      list(targets = 0, visible = FALSE) # hide column V0
    )
  )
) %>% 
  formatStyle(
    names(dat),
    background = styleInterval(c(0,5,10,50,100,500,1000,5000),
                               c("#ffffff", "#f2fbd2", "#c9ecb4", "#93d3ab", "#35b0ab"
                                 ,"#E5B9ADFF","#E5B9ADFF","#D98994FF","#D0587EFF"))) %>%
  formatStyle(columns = 2:ncol(dat),
              valueColumns = 1,
              target = 'cell',
              backgroundColor = styleEqual(6, 'white'))