Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update numeric slider values on updateSearch #1111

Merged
merged 8 commits into from
Jan 18, 2024
Merged

Conversation

mikmart
Copy link
Contributor

@mikmart mikmart commented Jan 12, 2024

Fixes #1110.

library(shiny)
library(DT)

ui <- fluidPage(
  actionButton("set_search", "Set column search"),
  tags$hr(),
  DTOutput("iris")
)

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

  proxy <- dataTableProxy("iris")
  observeEvent(input$set_search, {
    updateSearch(proxy, keywords = list(columns = c("", "5.0 ... 6.0")))
  })
}

shinyApp(ui, server)

image

@CLAassistant
Copy link

CLAassistant commented Jan 12, 2024

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much!

inst/htmlwidgets/datatables.js Outdated Show resolved Hide resolved
@mikmart
Copy link
Contributor Author

mikmart commented Jan 13, 2024

I changed the approach to use a custom event updatesearch so that each search field can handle updates from R as it wishes. This was to avoid the unnecessary searchColumn() call while also bypassing the input throttling for character columns.

Would you have another look @yihui? In particular I'm wondering if the custom event name should be namespaced somehow.

Copy link
Member

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought for a while, and perhaps we can pass an extra parameter to the input event handler to decide whether to throttle the search. You can find jQuery's trigger() documentation here: https://api.jquery.com/trigger/ The idea is that our handler fun takes two arguments e (event) and immediate (whether to search immediately or throttle the search). immediate is false by default, and we set it to true in the updateSearch() method, e.g.,

$(td).find('input').first().val(v).trigger('input', [true]);

I feel that might be a little simpler than defining an updatesearch event for all types of inputs. Let me know if the idea is not clear to you. I can also tweak the PR by myself. Thanks!

@mikmart
Copy link
Contributor Author

mikmart commented Jan 18, 2024

Yeah that makes sense. I can pivot to that approach.

@mikmart
Copy link
Contributor Author

mikmart commented Jan 18, 2024

Ready for a review again @yihui.

Copy link
Member

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks perfect now! I'll make a cosmetic change and merge soon. Thank you so much!

@yihui yihui merged commit 8bf8fa4 into rstudio:main Jan 18, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

updateSearch does not update numeric slider values
3 participants