cari

Rumah  >  Soal Jawab  >  teks badan

Laksanakan fungsi pilih-semua PickerInput dan paparkan semua pilihan apabila "semua" diklik

Dalam shiny应用程序中,当所有pickerInput()的选项都被选择时,我希望在pickerInput()内部显示单词"all"作为选项,当您点击它时,将显示所有三个选项。如果我们可以用selectInput()implementasi di bawah, maka tiada masalah, tetapi output yang dicetak tidak boleh terjejas. Apa patut saya buat?

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粉665679053504 hari yang lalu709

membalas semua(1)saya akan balas

  • P粉739942405

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

    Berikut ialah contoh berdasarkan ini jawapan yang sangat bagus. Kami menggunakan clickHandler,它根据容器All的点击情况,改变dropdown-item的样式,使其在display: blockdisplay: none之间切换。请注意,在应用程序初始化时,如果所有选择都被选中,项目只会隐藏在All di sini.

    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)

    balas
    0
  • Batalbalas