【问题标题】:How to solve shiny mis-alignment issue in datatable?如何解决数据表中闪亮的错位问题?
【发布时间】:2019-09-22 07:01:29
【问题描述】:

我正在尝试

  • 使数据表中的所有列宽度相同
  • 将数据表(标题及其内容)向左对齐
  • 达到主面板宽度后启用水平滚动

但我的数据表会自动以主面板为中心,其标题和内容也未对齐。

例子:

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)

2019 年 5 月 3 日更新:

我相信this question表示这个问题是由autoWidth = TRUE引起的,但是问题下没有解决方案,如果我们要调整列宽,我们也不能删除autoWidth = TRUE

【问题讨论】:

    标签: jquery css r datatable shiny


    【解决方案1】:

    对于对齐,您可以使用className = dt-left

    我猜参数 `fillContainer = T' 完成了滚动工作。

    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)
    

    【讨论】:

    • 虽然你设置的宽度是25px,但是整个宽度自动变成了datatable的宽度
    • 您可以通过将宽度更改为 50 px 来测试它
    猜你喜欢
    • 2020-04-06
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2020-09-07
    • 2018-08-03
    • 2017-11-29
    • 2019-10-09
    • 1970-01-01
    相关资源
    最近更新 更多