如何在闪亮的仪表板侧边栏中启用 3 个菜单项之间的切换

How to enable toggling between 3 menu items in shiny dashboard sidebar

我想让闪亮的仪表板中的菜单项可折叠。

我找到了 post 前段时间回答过的类似问题:

但是,解决方案 仅适用于 2 div-s。是否可以用 3 个菜单项做类似的事情

附加的可重现代码(post由 Martin Schmelzer 编辑)

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)

shinydashboard 中,menuItem 已经对任意数量的菜单项执行此操作。试试这个

library(shiny)
library(shinydashboard)

server <- function(input, output) { }

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")))), 
      menuItem(
        "Multiple Incident Analysis2",
        tabName = "dashboard2",
        icon = icon("th"),  
      div(id = "mult2", 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",  numericInput("tckt", "Ticket Number : ", 12345,  width = 290))),
      submitButton("Submit", width = "290")
    )),  
  dashboardBody()
)

shinyApp(ui, server)