rstudio / shiny

Easy interactive web applications with R
https://shiny.posit.co/
Other
5.37k stars 1.86k forks source link

Potential future feature: select all in selectize #1684

Open cinkie opened 7 years ago

cinkie commented 7 years ago

It would be nice to have the 'select all' or 'unselect all' options available within selectize UI. There are several ways to work around that but still this might be a nice feature.

One example is here: https://silviomoreto.github.io/bootstrap-select/examples/#selectdeselect-all-options

tylerlittlefield commented 3 years ago

+1

tylerlittlefield commented 3 years ago

I am currently attempting to do this with some hacks, definitely not a clean solution:

library(shiny)

selectize_helpers <- list(
  init = "Selectize.prototype.selectall = function() { var self = this; self.setValue(Object.keys(self.options)); }",
  onclick = paste(
    'var selectdiv = document.getElementById("test");',
    'var values = selectdiv.getElementsByClassName("option");',
    'var selectinput = document.getElementById("babynames");',
    'var array = [];',
    'for(var i=0;i < values.length; i++) { array.push(values[i].textContent); }',
    'selectinput.selectize.setValue(array);'
  )
)

ui <- fluidPage(
  tags$head(tags$script(selectize_helpers$init)),
  actionButton("all", "Select all", onclick = selectize_helpers$onclick),
  div(
    id = "test", 
    selectizeInput(
      inputId = "babynames",
      label = "Baby names",
      choices = NULL,
      width = "100%",
      multiple = TRUE,
      options = list(
        placeholder = 'Type to narrow available choices...',
        onInitialize = I('function() { this.setValue(""); }'),
        plugins = list(
          "remove_button"
        )
      )
    )
  )
)

server <- function(input, output, session) {
  updateSelectizeInput(
    session = session,
    inputId = "babynames",
    choices = unique(babynames::babynames$name), 
    server = TRUE
  )
}

shinyApp(ui, server)