How to solve shiny mis-alignment issue in datatable?

拜拜、爱过 提交于 2021-01-29 09:25:16

问题


I am trying to

  • make all columns in the datatable the same width
  • align the datatable (both header and its content) to the left
  • enable horizontal scrolling once it reaches mainPanel width

but my datatable gets automatically centered to the mainPanel, its header and content are also misaligned.

Example:

library(shiny)
library(dplyr)
library(DT)

ui <- fluidPage(

   titlePanel("Test Example"), 

   mainPanel(
     width = 10, 
     dataTableOutput("cars.table")
   )
)

server <- function(input, output) {
   output$cars.table <- renderDataTable({
      t(cars[1:10, ]) %>% 
       datatable(class = "compact small", 
                 options = list(columnDefs = list(list(width = "25px", targets = "_all")), scrollX = TRUE, autoWidth = TRUE, 
                                paging = FALSE, ordering = FALSE, searching = FALSE))
   })
}

shinyApp(ui = ui, server = server)

Update 2019/05/03:

I believe this question states that such issue was caused by autoWidth = TRUE, but there is no solution under the question, and if we want to adjust column width, we can't delete autoWidth = TRUE as well.


回答1:


For alignment you can use className = dt-left.

And I guess the argument `fillContainer = T' does the job for scrolling.

library(shiny)
library(dplyr)
library(DT)

ui <- fluidPage(

  titlePanel("Test Example"), 

  mainPanel(
    width = 10, 
    dataTableOutput("cars.table")
  )
)

server <- function(input, output) {
  output$cars.table <- renderDataTable({
    t(cars[1:10, ]) %>% 
      datatable(class = "compact small", fillContainer = T, 
                options = list(columnDefs = list(list(className = 'dt-left', width = "25px", targets = "_all")), scrollX = TRUE, 
                               paging = FALSE, ordering = FALSE, searching = FALSE))
  })
}

shinyApp(ui = ui, server = server)


来源:https://stackoverflow.com/questions/55974761/how-to-solve-shiny-mis-alignment-issue-in-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!