手动闪亮的扩展子菜单项 [英] Shiny expanding submenu items manually

查看:88
本文介绍了手动闪亮的扩展子菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to manually expand a submenu in a sidebar in shiny dashboard. The updateTabItems function allows me to go to the tab, but it does not expand the menu with it.

我正在寻找以下问题的可重复答案:

I am looking for a reproducible answer to the question here:how to manually expand a submenu in a shiny dashboard side bar which includes a nice working example and answers which are however not implemented.

推荐答案

以下是基于

Here is a working code based on the referred answer. Please note that in this case the JavaScript uses the tabName as a selector to add the active class to the tree menu.

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)

这篇关于手动闪亮的扩展子菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆