I am using selectInputs in a column of a DT datatable in a Shiny app. Thanks to some help here, I am including some JavaScript to selectize the selectInputs to keep the style and search capability of selectize. It is a wide table, so the selectInputs require scrolling horizontally to see them.
When I make a selection in any of the selectInputs the first time, everything works fine. However, when I click any of the selectInputs a second time, the page scrolls back to the left and the selectInputs are out of view. How can I keep the style and search capability I have but prevent this from happening?
EDIT: I also tried using shinyWidgets::pickerInput, and it does not have the scrollbar problem. However, the liveSearch feature does not work for me in a datatable. If you can solve that issue, I'll consider this question answered.
Example:
library(shiny)
library(DT)
# Function to selectize one or more input ids
selectize_ids <- function(ids) {
myStrings <- as.character(sapply(ids, function(id) {
paste0(" $('#", id, "').selectize();")
}))
c(
"function(settings){",
myStrings,
"}"
)
}
shinyApp(
ui = fluidPage(
div(style = "display: none;", selectInput(inputId = "dummy", label = NULL, choices = 1:2)),
fluidRow(DT::dataTableOutput("mytable"))
),
server = function(input, output, session) {
df <- as.data.frame(matrix(data = paste0("text", 1:60), ncol = 20))
colnames(df) <- paste0("column", 1:ncol(df))
df$myselect <- sapply(1:nrow(df), function(i) {
as.character(selectInput(
inputId = paste0("myselect_", i),
label = NULL,
choices = c("option1", "option2", "option3")
))
})
select_ids <- paste0("myselect_", 1:nrow(df))
output$mytable <- DT::renderDataTable({
DT::datatable(
data = df,
escape = F,
options = list(
initComplete = JS(selectize_ids(select_ids))
)
)
})
}
)