【问题标题】:Positioning DataTables elements with DOM option使用 DOM 选项定位 DataTables 元素
【发布时间】:2018-02-28 18:01:29
【问题描述】:

我无法使用shiny 选项将shiny 中的DT::datatable 输出中的length 更改和filtering 输入分别正确定位到右上角和左下角。代码:

library(shiny)
library(DT)

set.seed(2282018)
company <- data.frame(Company = letters[1:10], Sales = scales::dollar(runif(10, 200, 1230)), stringsAsFactors = F)

# UI ---- 
ui <- function(){

  fluidPage(

    sidebarLayout(

      sidebarPanel(numericInput("nums", label = "Num Input", value = 1, min = 1, max = 10)),

      mainPanel(dataTableOutput("mytable"))

      )

  )

}

# server ----
server <- function(input, output, session){

  output$mytable <- renderDataTable({
    datatable(company, 
              caption = tags$caption("StackOverflow Example"), 
              filter = "none", 
              options = list(
                autoWidth = T, 
                pageLength = 10, 
                scrollCollapse = T, 
                dom = '<"right"l>t<"left"f>')
              )

  })

}

runApp(list(ui = ui, server = server))

如前所述,我的目标是将l 移动到右上角,将f 移动到左下角。

谢谢!

【问题讨论】:

    标签: r shiny datatables-1.10 dt


    【解决方案1】:

    流程

    DataTable DOM positioning reference 中,有将元素移动到顶部/底部而不是向左/向右移动的示例。我不确定仅使用 dom 选项是否可以向左/向右移动元素。

    但是,根据question about moving the search box,您可以分三步向左/向右移动元素:

    1. 制作 CSS 类

      css <- HTML(".pull-left{float: left !important;} .pull-right{float: right !important;}")

    2. 使用 javascript/jQuery 将类添加到数据表中

      js <- HTML("$(function(){ setTimeout(function(){ $('.dataTables_filter').addClass('pull-left'); $('.dataTables_length').addClass('pull-right'); }, 200); });")

    3. 将 CSS 和 JS 添加到闪亮应用的 HTML 标头中

      fluidPage( tags$head(tags$style(css), tags$script(js)), ...)


    完整示例

    library(shiny)
    library(DT)
    
    set.seed(2282018)
    company <- data.frame(Company = letters[1:10], Sales = scales::dollar(runif(10, 200, 1230)), stringsAsFactors = F)
    
    css <- HTML(".pull-left{float: left !important;}
                .pull-right{float: right !important;}")
    
    js <- HTML("$(function(){
            setTimeout(function(){
               $('.dataTables_filter').addClass('pull-left');
               $('.dataTables_length').addClass('pull-right');
               }, 200);
               });")
    
    # UI ---- 
    ui <- function(){
    
            fluidPage(
                    tags$head(tags$style(css),
                              tags$script(js)),
    
                    sidebarLayout(
    
                            sidebarPanel(numericInput("nums", label = "Num Input", value = 1, min = 1, max = 10)),
    
                            mainPanel(dataTableOutput("mytable"))
                    )       
            )
    }
    
    # server ----
    server <- function(input, output, session){
    
            output$mytable <- renderDataTable({
                    datatable(company, 
                              caption = tags$caption("StackOverflow Example"), 
                              filter = "none", 
                              options = list(
                                      autoWidth = T, 
                                      pageLength = 10, 
                                      scrollCollapse = T, 
                                      dom = '<"top"l>t<"bottom"f>')
                    )
            })
    }
    runApp(list(ui = ui, server = server))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 2012-12-13
      相关资源
      最近更新 更多