DT
DT copied to clipboard
updateSearch does not update selectize inputs in factor columns
Hi, there seems to be some bug when using updateSearch to update column search on factor columns. While the table is filtered correctly, when clicking into the selectize search field, selected values are not shown in the field. On the other hand, when adding the filter at the moment of re-rendering the table via searchCols = list() option, it seems the search selectize works correctly and as expected.
Below is an example that reproduces this behavior. You can see this behavior when clicking on the click me button and using the column names search field selectize.
Any idea what to do to fix this?
library(DT)
server = shinyServer(function(input, output, session) {
values <- reactiveValues()
values$i <- 0
mtcars2 = mtcars[,c("hp","mpg")]
mtcars2$names <- as.factor(rownames(mtcars))
output$x4 = DT::renderDataTable(mtcars2, server = TRUE, filter = "top", rownames = FALSE)
dtp = dataTableProxy("x4")
output$x5 = renderPrint({
cat('Rows on the current page:\n\n')
cat(input$x4_rows_current, sep = ', ')
cat('\n\nAll rows:\n\n')
cat(input$x4_rows_all, sep = ', ')
cat('\n\nSelected rows:\n\n')
cat(input$x4_rows_selected, sep = ', ')
cat('\n\nSearch term:\n\n')
cat(input$x4_search, sep = ', ')
cat('\n\nColumn searches\n\n')
cat(input$x4_search_columns, sep = ', ')
cat('\n\nButton clicked\n\n')
cat(values$i, sep = ', ')
cat('\n\nLast action\n\n')
cat(values$last, sep = ', ')
})
observeEvent(input$mybutton,{
values$i <- values$i + 1
if (all(input$x4_search_columns == "")) {
values$last <- "Input vals"
updateSearch(dtp, keywords = list(global = "", columns = c("","", '["Mazda RX4","Hornet 4 Drive"]')))
} else {
values$last <- "Clear vals"
updateSearch(dtp, keywords = list(global = "", columns = ""))
}
})
})
ui = fluidPage(
title = 'DataTables Information',
fluidRow(
column(6, DT::dataTableOutput('x4')),
column(6, verbatimTextOutput('x5'))
),
actionButton("mybutton", "Click Me!")
)
shinyApp(ui = ui, server = server)
Based on my tests, it seems that while updateSearch updates the array that filters data in the given column, it does not update the selectize itself. That keeps the previously entered values.
I'm also experiencing this issue. It's not critical, but it makes working with updateSearch a bit ugly, as the user has to manually clear the filter and then start over.
Here's some screenshots from @raisjo 's test case. The tooltip is also missing with the version created via updateSearch, whereas selecting them manually gives the desired result.
Does anyone know of a workaround for this issue?
I am raving the same problem.
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(fluidRow(column(12, DTOutput('tbl')))),
server = function(input, output) {
output$tbl = renderDT(
iris %>% mutate_all(as.factor),
options = list(
scrollX = TRUE,
dom = 't',
lengthChange = FALSE,
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#222d32', 'color': '#fff'});",
"}")
),
rownames = FALSE,
filter = list(position = 'top', clear = FALSE)
)
}
)
I would expect to see only options that are available after start filtering no always all of them.
This is even more confusing when the button resets the filters, as then when the user clicks into one it looks like the filter is still there.
Any workaround for this? I've tried to just redraw the table with $('#example').DataTable().draw();
but that didn't work either.
FWIW, I found the solution in this SO post to be useful: https://stackoverflow.com/a/57438490/4425601 But it'd still be nice to have this handled directly by the DT package.
Is this still an issue? I tried the examples provided here before opening #1110 which is very similar, but was unable to reproduce the problem with factor columns.