-->

Hide the filters in Shiny DT datatable

2019-07-31 06:31发布

问题:

In my shiny app I'm creating a datatable using the DT package. I have column filters enabled, however, I want to hide the row of filter boxes. I have separate shiny widgets outside of the datatable which will act as the filters and pass them to the datatable through the searchCols option. Disabling column filters would hide the row of filter boxes but then the searchCols option doesn't work.

When I run the app and inspect the elements, I see that the row I want to delete is called < tr role="row">...< /tr>. If I right click it and click "Delete element", the row disappears and the datatable looks like I want it to, with the outside filters working as intended. I can also accomplish that by adding "display:none" to the element.style css. My question is how do I tell the app to delete this row when the datatable is rendered?

I'm not sure what kind of reproducible code I could provide, but here are some screenshots to make it clearer what I want to do.

I want to remove all the filter boxes and the row they are in so that the data is right below the column headers, as if filters were not enabled.

If I inspect the app and delete the highlighted element or add "display:none", the row gets hidden. How can I make this happen automatically when the datatable is rendered?

回答1:

I don't know if this will work with your widgets, but you can try this to set the display property in the styling sheet, using an id selector and a child selector:

library(shiny)
library(DT)

ui <- fluidPage(
  tags$style("#mydatatable thead > tr:nth-child(2) {display:none;}"),
  mainPanel(
    dataTableOutput("mydatatable")
  )
)

server <- function(input, output) {

  output$mydatatable <- DT::renderDataTable(
    datatable(iris, filter = 'top', options = list(
              pageLength = 5, autoWidth = TRUE)
    )
  )

}

shinyApp(ui = ui, server = server)


标签: shiny dt