闪亮的手动扩展子菜单项
Shiny expanding submenu items manually
我正在尝试在 shiny dashboard 的侧边栏中手动展开子菜单。 updateTabItems 函数允许我转到选项卡,但它不会扩展菜单。
我在这里寻找问题的可重现答案:
其中包括一个很好的工作示例和答案,但尚未实现。
这是一个基于 的工作代码。请注意,在这种情况下,JavaScript 使用 tabName
作为选择器将 active
class 添加到树形菜单。
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)
我正在尝试在 shiny dashboard 的侧边栏中手动展开子菜单。 updateTabItems 函数允许我转到选项卡,但它不会扩展菜单。
我在这里寻找问题的可重现答案:
这是一个基于 tabName
作为选择器将 active
class 添加到树形菜单。
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)