rstudio / shinydashboard

Shiny Dashboarding framework
https://rstudio.github.io/shinydashboard/
Other
887 stars 299 forks source link

Triggering "shown" javascript event on a menu item causes it to be selected #268

Open daattali opened 6 years ago

daattali commented 6 years ago

(originally posted on daattali/shinyjs#148 and then https://github.com/rstudio/shiny/issues/1991 and now suspected to be a shinydashboard issue)

The following code creates a shinydashboard with two menu items. Triggering the "shown" event on a menu item in javascript causes that tab to become selected. The reason this is problematic is because if the menu is hidden and then shown, it would be auto selected instead of keeping the current selection.

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      id = "tabs",
      menuItem("Item 1", tabName = "item1"),
      menuItem("Item 2", tabName = "item2"),
      actionButton("go", "Trigger 'shown'")
    )
  ),
  dashboardBody(
    tags$script(
      "Shiny.addCustomMessageHandler('triggerShown', function(message) {
        $(message).trigger('shown');
      })"
    ),
    tabItems(
      tabItem("item1", h1("Item 1")),
      tabItem("item2", h1("Item 2"))
    )
  )
)

server <- function(input, output, session) {
  observeEvent(input$go, {
    session$sendCustomMessage('triggerShown', 'a[data-value=item2]')
  })
}

shinyApp(ui = ui, server = server)

@jcheng5 believes this line may be related

BartJanvanRossum commented 5 years ago

I just ran into the same problem and it found that removing the id from the sidebarMenu doesn't cause the tab to become selected anymore. I don't know why this is, but it might help solving the problem.