Positioning DataTables elements with DOM option
Asked Answered
P

1

1

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!

Phosphate answered 28/2, 2018 at 18:1 Comment(0)
O
2

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))
Outwardbound answered 1/3, 2018 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.