【问题标题】:Set column width while using scrollX in R在 R 中使用 scrollX 时设置列宽
【发布时间】:2021-04-16 16:02:51
【问题描述】:

我正在尝试更改 DT::datatable 中某些列的宽度,不幸的是,使用 columnDefs 选项似乎仅在数据中有少量列时才有效。

当我将所有列添加到我的数据时,列宽不再遵循我在 columnDefs 选项中输入的内容。

这是一个示例,您可以看到第一个表的宽度都是恒定的,而在第二个表中,我可以根据需要手动设置宽度。删除 scrollX 参数也不起作用,考虑到我的数据有多少列,我需要它。


library(MASS)
library(shiny)
library(DT)

ui <- fluidPage(
    mainPanel(
      DT::dataTableOutput("table1"),
      br(),
      br(),
      br(),
      DT::dataTableOutput("table2")
    )
  )

server <- function(input, output) {

  output$table1 <- DT::renderDataTable({
    DT::datatable(
      Cars93[,-(20:27)],
      rownames = FALSE,
      options = list(
        pageLength = 5,
        autowidth = TRUE,
        scrollX = TRUE,
        searching = TRUE,
        ordering = TRUE,
        paging = TRUE,
        columnDefs = list(list(width = "200px", targets = c(0:2)),
                          list(width = "20px", targets = 3),
                          list(width = "50px", targets = 4))
      )
    )
  })
    output$table2 <- DT::renderDataTable({
      DT::datatable(
        Cars93[,-(6:27)],
        rownames = FALSE,
        options = list(
          pageLength = 5,
          autowidth = TRUE,
          scrollX = TRUE,
          searching = TRUE,
          ordering = TRUE,
          paging = TRUE,
          columnDefs = list(list(width = "200px", targets = c(0:2)),
                            list(width = "20px", targets = 3),
                            list(width = "50px", targets = 4))
        )
      )
  })
  
}

shinyApp(ui, server)

我需要在我的代码中进行哪些更改才能设置第一个表中的列宽,就像我在第二个表中一样,同时拥有所有列和 scrollX?

谢谢

【问题讨论】:

    标签: r shiny datatables dt


    【解决方案1】:

    试试:

    ui <- fluidPage(
      tags$head(
        tags$style(
          HTML("table {table-layout: fixed;}")
        )
      ),
      ......
    

    并将autowidth 替换为autoWidth

    【讨论】:

      猜你喜欢
      • 2022-10-05
      • 2020-09-21
      • 2022-01-21
      • 1970-01-01
      • 2014-10-02
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      相关资源
      最近更新 更多