DT icon indicating copy to clipboard operation
DT copied to clipboard

Use DataTables smart search on certain columns

Open SteadyGiant opened this issue 3 years ago • 1 comments

I want to apply DataTables "smart" search to specific columns, not all columns. Is this functionality possible with R's DT package? Is there a way to add JavaScript that'll get the job done? I'm doing this all in a Shiny app. StackOverflow and RStudio Community could not answer.

I believe I want something similar to the columns().search() method in the DataTables library.

Here's a normal global search of all columns:

library(DT)
library(shiny)
library(shinyWidgets)

dat = data.frame(
  first  = letters[1:3],
  second = letters[4:6],
  third  = letters[7:9]
)
ui = function(request) {
  shiny::fluidPage(
    shiny::sidebarLayout(
      shiny::sidebarPanel(
        shinyWidgets::searchInput(
          inputId = "search_all",
          label = "Search all columns"
        )
      ),
      shiny::mainPanel(DT::dataTableOutput("dt"))
    )
  )
}
server = function(input, output, session) {
  output$dt = DT::renderDataTable({
    dat %>%
      DT::datatable(
        options = list(
          dom = "lrtip",
          search = list(search = input$search_all),
          searchHighlight = TRUE
        )
      )
  })
}
shiny::shinyApp(ui, server)

Here's what I wish I could do, set target columns for each search input (this doesn't work, I'm just including it for illustration):

library(DT)
library(shiny)
library(shinyWidgets)

dat = data.frame(
  first  = letters[1:3],
  second = letters[4:6],
  third  = letters[7:9]
)
ui = function(request) {
  shiny::fluidPage(
    shiny::sidebarLayout(
      shiny::sidebarPanel(
        shinyWidgets::searchInput(
          inputId = "search_1",
          label = "Search 1 column"
        ),
        shinyWidgets::searchInput(
          inputId = "search_2",
          label = "Search 2 columns"
        )
      ),
      shiny::mainPanel(DT::dataTableOutput("dt"))
    )
  )
}
server = function(input, output, session) {
  output$dt = DT::renderDataTable({
    dat %>%
      DT::datatable(
        options = list(
          dom = "lrtip",
          search = list(
            list(
              targets = c("first"),
              search = input$search_1
            ),
            list(
              targets = c("second", "third"),
              search = input$search_2
            )
          ),
          searchHighlight = TRUE
        )
      )
  })
}
shiny::shinyApp(ui, server)

By filing an issue to this repo, I promise that

  • [x] I have fully read the issue guide at https://yihui.name/issue/.
  • [x] I have provided the necessary information about my issue.
    • [x] If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • ~~If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('DT'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('rstudio/DT').~~
    • ~~If I have posted the same issue elsewhere, I have also mentioned it in this issue.~~
  • [x] I have learned the Github Markdown syntax, and formatted my issue correctly.

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

SteadyGiant avatar Mar 30 '21 14:03 SteadyGiant

This columns.searchable option may be helpful but I haven't verified it.

https://datatables.net/reference/option/columns.searchable

shrektan avatar Apr 08 '21 15:04 shrektan