search

Home  >  Q&A  >  body text

Implement the select-all function of PickerInput and display all options when "all" is clicked

In the shiny application below, I want the word to be displayed inside pickerInput() when all options of pickerInput() are selected "all"As an option, when you click on it, all three options will be displayed. If we can implement it with selectInput(), then there will be no problem, but the printed output should not be affected. What should I do?

library(shiny)
library(shinyWidgets)
ui <- fluidPage(
  
  uiOutput("pick"),
  verbatimTextOutput("PR")
)

server <- function(input, output, session) {
  output$pick<-renderUI({
    pickerInput(
      inputId = "p9",
      label = "健康保险",
      choices = unique(as.character(iris$Species)),
      width = "150px",
      selected = unique(as.character(iris$Species)),
      multiple = TRUE,
      options = list(
        `actions-box` = TRUE,
        `deselect-all-text` = "无",
        `select-all-text` = "全部",
        `none-selected-text` = "零"
      )
    )
  })
  output$PR<-renderPrint({
    input$p9
  })
}

shinyApp(ui, server)

P粉665679053P粉665679053563 days ago738

reply all(1)I'll reply

  • P粉739942405

    P粉7399424052023-09-10 11:51:56

    Here is an example based on this great answer. We use a clickHandler here, which changes the style of dropdown-item based on the click status of the container All so that it is in display: block Switch between and display: none. Note that on application initialization, items will only be hidden behind All if all selections are selected.

    library(shiny)
    library(shinyWidgets)
    
    js <- HTML(
        "
    $(function() {
      let observer = new MutationObserver(callback);
    
      function clickHandler(evt) {
        if ($('.dropdown-item').css('display') == 'block') {
            $('.dropdown-item').on('click', clickHandler).css('display', 'none');
        } else {
            $('.dropdown-item').on('click', clickHandler).css('display', 'block');
        }
      }
    
      function callback(mutations) {
          for (let mutation of mutations) {
              if (mutation.type === 'childList') {
                  $('.dropdown-header').on('click', clickHandler).css('cursor', 'pointer');
                  if ($('#p9 option').length == $('#p9 :selected').length) {
                      $('.dropdown-item').on('click', clickHandler).css('display', 'none');
                  }
              }
          }
      }
    
      let options = {
        childList: true,
      };
    
      observer.observe($('.inner')[0], options);
    })
    "
    )
    
    choices <- list("All" = unique(as.character(iris$Species)))
    
    ui <- fluidPage(
        tags$head(tags$script(js)),
        pickerInput(
            inputId = "p9",
            label = "Health Insurance",
            choices = choices,
            width = "150px",
            selected = unlist(unname(choices)),
            multiple = TRUE,
            options = list(
                `actions-box` = TRUE,
                `deselect-all-text` = "None",
                `select-all-text` = "All",
                `none-selected-text` = "zero"
            )
        ),
        verbatimTextOutput("PR")
    )
    
    server <- function(input, output, session) {
        output$PR <- renderPrint({
            input$p9
        })
    }
    
    shinyApp(ui, server)

    reply
    0
  • Cancelreply