问题
I can't correctly position the l
ength changing and the f
iltering 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:
Make CSS classes
css <- HTML(".pull-left{float: left !important;} .pull-right{float: right !important;}")
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); });")
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