您可以使用“自定义表容器”(请参见此处:https://rstudio.github.io/DT/)和一些JS(jQuery)来修改表CSS样式。
# a custom table container
sketch = htmltools::withTags(table(
class = 'display',thead(
# Define the grouping of your df
tr(
th(colspan = 4,'Historical'),th(colspan = 4,'Current')
),# Repeat column names 8 times
tr(
lapply(paste0("Col ",1:8),th)
)
)
))
# Using JS for adding CSS,i.e.,coloring your heading
# Get the corresponding table header (th) from a table cell (td) and apply color to it
headjs <- "function(thead) {
$(thead).closest('thead').find('th').eq(0).css('background-color','#D9E1F2');
$(thead).closest('thead').find('th').eq(1).css('background-color','#8EA9DB');
}"
# Your data frame
df <- data.frame(1,2,3,4,5,6,7,8)
# Output DT with your custom header
datatable(df,container = sketch,options = list(
headerCallback = JS(headjs)
))
输出:
本文链接:https://www.f2er.com/2533113.html