闪亮的仪表板侧边栏中的可折叠菜单项

Collapsible menu item in shiny dashboard sidebar

我在边栏中有两个菜单项。目前,如果我单击任何菜单项,则会显示所有菜单项的选项卡项。我想让它可折叠,如果我点击多个名称菜单,单一分析应该折叠,如果我点击单一分析,多个分析应该折叠。
当前设计是

相同的可重现代码是

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)

我无法在此使用条件面板。

这是使用 JavaScript 的方法。注意事项:

我们将菜单中的项目包装在 ID 为 #mult#singlediv 个容器中。

我们添加 JavaScript 代码来选择所有菜单按钮(li 元素)并添加点击事件。单击其中一个菜单项时,两个 div 容器的状态都是 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)