rstudio / DT

R Interface to the jQuery Plug-in DataTables
https://rstudio.github.io/DT/
Other
587 stars 184 forks source link

Extract search functionality #1135

Open jmorten opened 3 months ago

jmorten commented 3 months ago

I would like to know if the table search functionality can be extracted. By adding a filter to a table renderDT(tbl, filter = "top") one can search each column. However, I would like to change where the filter boxes, namely to the left in their own pane, and use keep the underlying functionality, especially having the search boxes dynamically update based on the other inputs.

server <- function(input, output, session) {
  output$table <- renderDT(tbl, filter = "top")

  filterable_sets <- eventReactive(input$table_search_columns, {
    # Get separately filtered indices
    fi <- Map(doColumnSearch, tbl, input$table_search_columns)

    # Find what rows others leave available
    ai <- lapply(seq_along(fi), function(j) Reduce(intersect, fi[-j]))

    # Get the corresponding data
    lapply(Map(`[`, tbl, ai), function(x) {
      if (is.factor(x)) droplevels(x) else x
    })
  })

  proxy <- dataTableProxy("table")
  observeEvent(filterable_sets(), {
    updateFilters(proxy, filterable_sets())
  })
}

By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.