Rohit Kumar Singh
Rohit Kumar Singh

Reputation: 669

Collapsible menu item in shiny dashboard sidebar

I have two menu items in the sidebar. Currently, if I click on any menu item the tab items for all the menu items are shown. I want to make it collapsible, if I click on multiple names menu, Single Analysis should collapse and if I click on the single analysis, multiple ones should collapse.
Current design is

enter image description here

The reproducible code for the same is

library(shinydashboard)
ui <- dashboardPage(
   dashboardHeader(title = "Dashboard", titleWidth = 290),  
  dashboardSidebar(
   width = 290,
  sidebarMenu(
   menuItem(
    "Multiple Incident Analysis",
    tabName = "dashboard",
    icon = icon("th")),      
  splitLayout(cellWidths = c("44%", "31%", "25%"),
              dateInput("datefrom", "Date From:", format = "mm/dd/yyyy", Sys.Date()-5),
              textInput("datefrom_hour", "Hour",
                        value = "12:00"),
              textInput("datefrom_noon","AM/PM", value = "AM")),      
  splitLayout(cellWidths = c("44%", "31%", "25%"),
              dateInput("dateto", "Date To:", format = "mm/dd/yyyy"),
              textInput("dateto_hour", "Hour",
                        value = "11:59"),
              textInput("dateto_noon","AM/PM", value = "PM")),
  menuItem("Single Analysis", 
           tabName = "widgets", 
           icon = icon("th")),
  numericInput("tckt", "Ticket Number : ", 12345,  width = 290),
  submitButton("Submit", width = "290")
)),  
dashboardBody(    
      ))
shinyApp(ui, server)

I am not able to use the conditional panel in this.

Upvotes: 3

Views: 4649

Answers (1)

Martin Schmelzer
Martin Schmelzer

Reputation: 23879

Here is a way using JavaScript. Things to note:

We wrap the items in the menu in div containers with the ids #mult and #single.

We add JavaScript code that selects all menu buttons (the li elements) and adds a click event. When one of those menu items is clicked, the states of both div containers is toggled.

library(shiny)
library(shinydashboard)


server <- function(input, output) { }
jsc <- '
$(document).ready(function () {
  $(".sidebar-menu").children("li").on("click", function() {
    $("#mult, #single").toggle();
  });
});
'

ui <- dashboardPage(
  dashboardHeader(title = "Dashboard", titleWidth = 290),  
  dashboardSidebar(
    width = 290,
    sidebarMenu(
      menuItem(
        "Multiple Incident Analysis",
        tabName = "dashboard",
        icon = icon("th")),      
      div(id = "mult", splitLayout(cellWidths = c("44%", "31%", "25%"),
                  dateInput("datefrom", "Date From:", format = "mm/dd/yyyy", Sys.Date()-5),
                  textInput("datefrom_hour", "Hour",
                            value = "12:00"),
                  textInput("datefrom_noon","AM/PM", value = "AM")),      
      splitLayout(cellWidths = c("44%", "31%", "25%"),
                  dateInput("dateto", "Date To:", format = "mm/dd/yyyy"),
                  textInput("dateto_hour", "Hour",
                            value = "11:59"),
                  textInput("dateto_noon","AM/PM", value = "PM"))),
      menuItem("Single Analysis", 
               tabName = "widgets", 
               icon = icon("th")),
      div(id = "single", style="display: none;", numericInput("tckt", "Ticket Number : ", 12345,  width = 290)),
      submitButton("Submit", width = "290")
    )),  
  dashboardBody(
    tags$head(tags$script(jsc))
  ))





shinyApp(ui, server)

Upvotes: 7

Related Questions