rstudio / shinydashboard

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

is it possible to provide an id to each menuItem #56

Closed pssguy closed 9 years ago

pssguy commented 9 years ago

I would like to be able to dynamically set inputs into the sidebar dependent on which menuItem has been selected. something like this In ui.r

   dashboardSidebar(
        uiOutput("a"),
        sidebarMenu(
            menuItem("b"),
            menuItem("c"),
        ))

in server.r

output$a <- renderUI ({

          if (menuItem="a") {
           selectInput(...) 
      } else {
          numericInput(.....)
    }
})

But the menuItem does not appear to have an id to reference to. Am I missing something (as usual)

Tutuchan commented 9 years ago

Hi pssguy,

You can do this by assigning an id to the sidebarMenu. Here's a working example :

library(shinydashboard)
library(shiny)
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    uiOutput("a"),
    sidebarMenu(
      menuItem("b", tabName = "tabB"),
      menuItem("c", tabName = "tabC"),
      id = "sbMenu"
    )),
  dashboardBody())

server <- function(input, output){
  output$a <- renderUI({
    if (input$sbMenu=="tabB") {
      selectInput("test", "B", c(1:4)) 
    } else {
      numericInput("test2", "C", 50, 0, 100)
    }

  })
}

shiny::shinyApp(ui, server)
pssguy commented 9 years ago

@Tutuchan Thanks a lot I knew there pretty much had to be a way

vishnumaheshwari commented 8 years ago

hi, I am facing the same problem while adding the menu subitem into the menu items. By adding the menuSubitems the tabName od the id of corresponding menuSub item is not catching. Is there a way to track or catch the menu item ID or to activate that on click with its dropdown showing simultaneously.

jienagu commented 7 years ago

Hi Pssguy,

I met this problem too but I solved it by adding div() to the menuItem!

Please see the R code here:

div(id="haha", menuItem(tags$em("Download Reports",style="font-size:150%"),icon=icon("bar-chart-o"),tabName ="download"))

So in this way, your menuItem's id is "haha" and you are able to dynamically set inputs into the sidebar dependent on which menuItem has been selected!

Hopefully it is helpful for you!

Thank you!

Best regards, Jiena Gu

sanjmeh commented 3 years ago

I tried many ways to hide/show individual menuItem() elements but with no success yet.

  1. Giving an id with a div span spoils the css and the menuItem() is shown with less line space and the left padding vanishes.
  2. Opening a new sidebarMenu() doesnt help in hide/show. It is always visible. The shinyjs::hide() does not work presumably on a second sidebarMenu() id.