【问题标题】:Shiny expanding submenu items manually闪亮的手动扩展子菜单项
【发布时间】:2017-04-12 09:13:02
【问题描述】:

我正在尝试在闪亮仪表板的侧边栏中手动展开子菜单。 updateTabItems 函数允许我转到选项卡,但它不会用它展开菜单。

我在这里寻找问题的可重现答案:how to manually expand a submenu in a shiny dashboard side bar 其中包括一个很好的工作示例和未实施的答案。

【问题讨论】:

    标签: javascript r shiny shinydashboard shinyjs


    【解决方案1】:

    这是一个基于referred answer 的工作代码。请注意,在这种情况下,JavaScript 使用 tabName 作为选择器将 active 类添加到树形菜单中。

    library(shiny)
    library(shinydashboard)
    library(shinyjs)
    
    ui <- dashboardPage(
      dashboardHeader(title = "Simple tabs"),
      dashboardSidebar(
        sidebarMenu(
          id = "tabs",
          actionButton('switchtab', 'Switch tab'),
          menuItem("Widgets", tabName = "widgets", icon = icon("th")),
          menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"),
                   menuSubItem("Sub Menu 1",icon = icon("folder-open"), tabName = "subMenu1")
          )
        )
      ),
      dashboardBody(
        useShinyjs(), 
        extendShinyjs(text = "shinyjs.activateTab = function(name){
                      setTimeout(function(){
                      $('a[href$=' + '\"#shiny-tab-' + name + '\"' + ']').closest('li').addClass('active')
                      }, 200);
                      }"
        ),
        tabItems(
          tabItem(tabName = "subMenu1",
                  h2("Dashboard tab / Sub menu 1 content")
          ),
          tabItem(tabName = "widgets",
                  h2("Widgets tab content")
          )
        )
      )
      )
    
    server <- function(input, output, session) {
      observeEvent(input$switchtab, {
        newtab <- switch(input$tabs,
                         "subMenu1" = "widgets",
                         "widgets" = "subMenu1"
        )
        updateTabItems(session, "tabs", newtab)
        if (newtab == "subMenu1")
          js$activateTab("dashboard")
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2015-12-04
      • 1970-01-01
      • 2019-10-01
      • 2019-02-23
      • 2018-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      相关资源
      最近更新 更多