R DT表格顶部的水平滚动条

5

我有一个宽而长的DT表格,使用shiny技术。默认情况下,我希望在表格上方显示水平滚动条。是否有方法可以实现这一点?我的当前DT定义如下:

DT::datatable(dt, rownames = FALSE,
                    filter = fbox,
                    style = "bootstrap",
                    options = list(
                      dom = dom,
                      scrollX = TRUE,
                      columnDefs = list(list(orderSequence = c('desc', 'asc'), targets = "_all")),
                      processing = FALSE,
                      pageLength = 500,
                      lengthMenu = list(c(500, 1000, 5000), c("500","1000","5000"))
                    ),
                    callback = DT::JS("$(window).unload(function() { table.state.clear(); })")
 ) %>% DT::formatStyle(., cn_cat,  color = "black", backgroundColor = "#dee6ea",fontWeight = "bold")

提前感谢您。

2个回答

10

翻转应用中所有数据表的滚动条

您可以添加一些 CSS 样式来翻转包含滚动条/表格的 DIV 元素,然后再翻转回来表格内容,具体方法请参考此答案

.dataTables_scrollBody {
    transform:rotateX(180deg);
}
.dataTables_scrollBody table {
    transform:rotateX(180deg);
}

针对特定数据表翻转滚动条

如果你只想在一个表格上翻转滚动条,你可以选择特定的表格:

#flipped > .dataTables_wrapper.no-footer > .dataTables_scroll > .dataTables_scrollBody {
    transform:rotateX(180deg);
}
#flipped > .dataTables_wrapper.no-footer > .dataTables_scroll > .dataTables_scrollBody table{
    transform:rotateX(180deg);
}

示例 在此输入图像描述

这段内容展示了一个图片,点击图片可以跳转至一个链接。
library(shiny)
library(DT)

css <- HTML(
    "#flipped > .dataTables_wrapper.no-footer > .dataTables_scroll > .dataTables_scrollBody {
        transform:rotateX(180deg);
    }
    #flipped > .dataTables_wrapper.no-footer > .dataTables_scroll > .dataTables_scrollBody table{
        transform:rotateX(180deg);
    }"
)

ui <- fluidPage(
    tags$head(tags$style(css)),
    fluidRow(column(width = 6,
                    h4("Flipped Scrollbar"),
                    br(),
                    DT::dataTableOutput("flipped")
                    ),
             column(width = 6,
                    h4("Regular Scrollbar"),
                    br(),
                    DT::dataTableOutput("regular")
                    )
             )
)

server <- function(input, output, session) {
    output$flipped <- DT::renderDataTable({
        DT::datatable(mtcars, rownames = FALSE,
                      options = list(
                          scrollX = TRUE
                      )
        )
    })
    output$regular <- DT::renderDataTable({
        DT::datatable(mtcars, rownames = FALSE,
                      options = list(
                          scrollX = TRUE
                      )
        )
    })
}

shinyApp(ui, server)

嗨@HallieSwan,我在几个浏览器上尝试了这个示例,但我没有在顶部看到滚动条。 - Sebastian Zeki
如果您将“Scroller”扩展添加到DT中,则此处的CSS解决方案似乎具有一些意外的副作用。 - TTS

1

我按照@HallieSwam的建议将滚动条置于顶部,但是查看了源HTML代码以了解需要旋转哪些部分。
对我有用的方法:

tags$head(tags$style(HTML( 
   "#Table1 .dataTables_scrollBody {transform:rotate(180deg);}
    #Table1 .dataTables_scrollHead {transform:rotate(180deg);}
    #Table1 .dataTables_scroll table {transform:rotate(180deg);}
   "
)))

scrollBody将整个表格包括滚动条一起滚动,然后需要使用scrollHead来对齐最终表格中的滚动条和表头。滚动表格仅滚动表格内容,将滚动条留在顶部。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接