Positioning DataTables elements with DOM option

寵の児 提交于 2019-12-08 03:36:34

问题


I can't correctly position the length changing and the filtering input to the top-right and bottom-left respectively on my DT::datatable output in shiny using the dom option. Code:

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))

As stated before, my goal is to move l to the top right and f to the bottom left.

Thanks!


回答1:


Process

In the DataTable DOM positioning reference, there are examples of moving elements to top/bottom, but not left/right. I'm not sure if moving elements left/right is possible using just the dom option.

However, per this question about moving the search box, you can move the elements left/right in three steps:

  1. Make CSS classes

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

  2. Use javascript/jQuery to add the classes to your datatable

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

  3. Add the CSS and JS to the HTML header of your shiny app

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


Full example

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))


来源:https://stackoverflow.com/questions/49035864/positioning-datatables-elements-with-dom-option

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