suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Implementieren Sie die Funktion „Alle auswählen“ von PickerInput und zeigen Sie alle Optionen an, wenn auf „Alle“ geklickt wird

In der shiny应用程序中,当所有pickerInput()的选项都被选择时,我希望在pickerInput()内部显示单词"all"作为选项,当您点击它时,将显示所有三个选项。如果我们可以用selectInput()Implementierung unten gibt es dann kein Problem, aber die Druckausgabe sollte nicht beeinträchtigt werden. Was soll ich machen?

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粉665679053489 Tage vor699

Antworte allen(1)Ich werde antworten

  • P粉739942405

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

    这是一个基于这个很好的答案的示例。我们在这里使用了一个clickHandler,它根据容器All的点击情况,改变dropdown-item的样式,使其在display: blockdisplay: none之间切换。请注意,在应用程序初始化时,如果所有选择都被选中,项目只会隐藏在All后面。

    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)

    Antwort
    0
  • StornierenAntwort