预选闪亮的操作按钮

问题描述 投票:0回答:1

我想在会话打开时预先选择一个特定的操作按钮。我将默认值设置为应该选择我的 actionButton

"two"
。但是,我还没有找到一种方法来更改按钮的样式,使会话启动时按钮看起来呈灰色(选定)。我知道通常可以使用
style = "..."
更改样式,但是当选择另一个按钮时,它不会恢复正常。

library(shiny)

ui <- fluidPage(
  actionButton("one", "One"),
  actionButton("two", "Two"),
  actionButton("three", "Three"),
  textOutput("text")
)

server <- function(session, input, output) {
  
  v <- reactiveValues(data = 2)
  
  observeEvent(input$one, {
    v$data <- 1
  })
  
  observeEvent(input$two, { # this button should be preselected
    v$data <- 2
  })
  
  observeEvent(input$three, {
    v$data <- 3
  })
  
  output$text <- renderText({v$data})
  
}

shinyApp(ui, server)

我希望预选看起来像这样:

enter image description here

r shiny
1个回答
0
投票

通过

focus
设置
js
的解决方案:

library(shiny)

ui <- fluidPage(
  tags$head(
    tags$script(
      HTML("
           $(document).ready(function() {
             $('#two').focus()
                      .css('outline', 'none');
           })
           ")
      )
  ),
  actionButton("one", "One"),
  actionButton("two", "Two"),
  actionButton("three", "Three"),
  textOutput("text")
)

server <- function(session, input, output) {
  
  v <- reactiveValues(data = 2)
  
  observeEvent(input$one, {
    v$data <- 1
  })
  
  observeEvent(input$two, { # this button should be preselected
    v$data <- 2
  })
  
  observeEvent(input$three, {
    v$data <- 3
  })
  
  output$text <- renderText({v$data})
  
}

shinyApp(ui, server)
© www.soinside.com 2019 - 2024. All rights reserved.