Implementing search boxes for each column

I am trying to follow along with the logic of this post: DataTables example - Individual column searching (text inputs) in order to create search boxes for each of my columns. I realize this process is in two steps: we need to make the search boxes themselves, and then make them functional using the initComplete method.

I've tried using document on ready or putting the check boxes within a callback but I don't think that's quite right.... any help appreciated!!

library(shiny)
# https://datatables.net/examples/api/multi_filter.html

# we need to set up the input boxes
# and then call the initComplete method
# but I am not sure of the order to do this in!

ui <- fluidPage(
    DT::dataTableOutput("styled"),
    tags$style(HTML(
      "tfoot input {
        width: 100%;
        padding: 3px;
        box-sizing: border-box;
    }"))
)

# Define server logic required to draw a histogram
server <- function(input, output) {
    output$styled <- DT::renderDataTable(mtcars,
          options = list(
                initComplete = DT::JS("function () {
                  // Apply the search
                  this.api().columns().every( function () {
                      var that = this;

                      $( 'input', this.footer() ).on( 'keyup change clear', function () {
                          if ( that.search() !== this.value ) {
                              that
                                  .search( this.value )
                                  .draw();
                          }
                      });
                  });
              }")
          ), 
          callback = DT::JS('table.each( function () {
              var title = $(this).text();
             $(this).html( "<input type="text" placeholder="Search "+title+"" />" );})'),
    )
}

# Run the application 
shinyApp(ui = ui, server = server)
1 Like

Before wrestling with a lot of javascript as per the example you are attempting to follow, I would recommend you test whether the built in top filters might be good enough for your purposes.

DT::datatable(dplyr::starwars,filter="top")
1 Like

oh my GOD perfect thank you!!! this sent me down a rabbit hole of ALL the incredible extensions and plug ins already existing in DT - thanks for this heads up and to read the docs!!! bwahaha :woman_facepalming:

2 Likes

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.