简体   繁体   English

在ShinyDashboard中的menuSubItems之间切换

[英]Switching between menuSubItems in shinyDashboard

I'm trying to set up a shiny app using shinydashboard, and for the most part, having good luck. 我正在尝试使用Shinydashboard设置一个闪亮的应用程序,并且在大多数情况下,祝您好运。 However, I'm running into a quirk with sidebar behavior that I think is avoidable, but haven't found how yet. 但是,我遇到了一个带有边栏行为的怪癖,我认为这是可以避免的,但尚未找到方法。

Below is a small example that reproduces the problem I'm having. 以下是一个重现我所遇到的问题的小示例。 Basically, there are two sidebarMenus - Menu One and Menu Two, each with two menuSubItems. 基本上,有两个侧边栏菜单-菜单一和菜单二,每个都有两个menuSubItems。 Switching subitems within a menu item works fine. 在菜单项中切换子项可以正常工作。 So, if I wanted to switch from subItemOne to subItemTwo, no problems. 因此,如果我想从subItemOne切换到subItemTwo,没有问题。 I can do that all day. 我可以整天做。

I can also switch to subItems across menus, such that jumping from subItemOne to subItemThree, that's fine. 我还可以跨菜单切换到subItems,这样就可以从subItemOne跳转到subItemThree。 The problem lies in trying to switch back. 问题出在试图切换回来。 If subItemOne is selected, and I try to go to subItemThree and back to subItemOne, I can't do it. 如果选择了subItemOne,而我尝试转到subItemThree,然后返回到subItemOne,则无法执行此操作。 I have to go to subItemTwo, then I can open SubItemOne. 我必须转到subItemTwo,然后才能打开SubItemOne。

Is there a way to correct this setup such that I could jump directly from subItemOne to subItemThree (or two and four) and back again? 有没有一种方法可以纠正此设置,以便我可以直接从subItemOne跳到subItemThree(或两个和四个)然后再次返回?

library('shiny')
library('shinydashboard')
# Sidebar #############################
sidebar <- dashboardSidebar(
  width = 290,

  sidebarMenu(
    menuItem('Menu One', tabName = 'menuOne', icon = icon('line-chart'), 
        collapsible = 
            menuSubItem('Sub-Item One', tabName = 'subItemOne'),
            menuSubItem('Sub-Item Two', tabName = 'subItemTwo')
            )
  ),

  sidebarMenu(
    menuItem('Menu Two', tabName = 'menuTwo', icon = icon('users'), 
             collapsible = 
               menuSubItem('Sub-Item Three', tabName = 'subItemThree'),
             menuSubItem('Sub-Item Four', tabName = 'subItemFour')
    )
  )

)
# Body #############################
body <- dashboardBody(

  tabItems(
    tabItem(tabName = 'subItemOne',
            h2('Selected Sub-Item One')
    ),
    tabItem(tabName = 'subItemTwo',
            h2('Selected Sub-Item Two')
    ),
    tabItem(tabName = 'subItemThree',
            h2('Selected Sub-Item Three')
    ),
    tabItem(tabName = 'subItemFour',
            h2('Selected Sub-Item Four')
    )
  )
)
# UI #############################
ui <- dashboardPage(
  dashboardHeader(title = 'Test', titleWidth = 290),
  sidebar,
  body
)
# Server #############################
server <- function(input, output){

}

shinyApp(ui, server)

The problem is that the tab items stay active and clicking on an active tab item doesn't update the UI. 问题在于选项卡项保持活动状态,并且单击活动的选项卡项不会更新UI。 This can be fixed with some Javascript. 这可以用一些Javascript来解决。

library('shiny')
library('shinydashboard')
# Sidebar #############################
sidebar <- dashboardSidebar(
  tags$head(
    tags$script(
      HTML(
        "
        $(document).ready(function(){
          // Bind classes to menu items, easiet to fill in manually
          var ids = ['subItemOne','subItemTwo','subItemThree','subItemFour'];
          for(i=0; i<ids.length; i++){
            $('a[data-value='+ids[i]+']').addClass('my_subitem_class');
          }

          // Register click handeler
          $('.my_subitem_class').on('click',function(){
            // Unactive menuSubItems
            $('.my_subitem_class').parent().removeClass('active');
          })
        })
        "
      )
    )
  ),
  width = 290,

  sidebarMenu(
    menuItem('Menu One', tabName = 'menuOne', icon = icon('line-chart'),
             collapsible = 
               menuSubItem('Sub-Item One', tabName = 'subItemOne'),
             menuSubItem('Sub-Item Two', tabName = 'subItemTwo')
    )
  ),

  sidebarMenu(
    menuItem('Menu Two', tabName = 'menuTwo', icon = icon('users'), 
             collapsible = 
               menuSubItem('Sub-Item Three', tabName = 'subItemThree'),
             menuSubItem('Sub-Item Four', tabName = 'subItemFour')
    )
  )

)
# Body #############################
body <- dashboardBody(

  tabItems(
    tabItem(tabName = 'subItemOne',
            h2('Selected Sub-Item One')
    ),
    tabItem(tabName = 'subItemTwo',
            h2('Selected Sub-Item Two')
    ),
    tabItem(tabName = 'subItemThree',
            h2('Selected Sub-Item Three')
    ),
    tabItem(tabName = 'subItemFour',
            h2('Selected Sub-Item Four')
    )
  )
)
# UI #############################
ui <- dashboardPage(
  dashboardHeader(title = 'Test', titleWidth = 290),
  sidebar,
  body
)
# Server #############################
server <- function(input, output){

}

shinyApp(ui, server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM