shinydashboard icon indicating copy to clipboard operation
shinydashboard copied to clipboard

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

Open daattali opened this issue 7 years ago • 1 comments

(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

daattali avatar Mar 29 '18 01:03 daattali

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.

BartJanvanRossum avatar Jul 25 '18 09:07 BartJanvanRossum