【问题标题】:To enable and disable sidebar toggle button using a action button使用操作按钮启用和禁用侧边栏切换按钮
【发布时间】:2018-08-23 00:34:17
【问题描述】:

我正在寻找一个代码 sn-p 使用它,我可以启用/禁用闪亮仪表板标题中的侧边栏切换按钮。

library(shiny)
library(shinydashboard)
library(shinyjs)

ui <- shinyUI(dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    useShinyjs()
  )
))

server <- shinyServer(function(input, output, session) {
  addClass(selector = "body", class = "sidebar-collapse") # Hide Side Bar
})

shinyApp(ui = ui, server = server)

如果有人可以帮忙,请告诉我???

【问题讨论】:

    标签: shiny togglebutton shinydashboard shinyjs


    【解决方案1】:

    我已经找到了解决方案...如果有人遇到同样的问题,可以参考以下解决方案:

    library(shiny)
    library(shinydashboard)
    library(shinyjs)
    
    ui <- shinyUI(dashboardPage(
      dashboardHeader(),
      dashboardSidebar( tags$head(
        tags$script(
          HTML(#code for hiding sidebar tabs 
            "Shiny.addCustomMessageHandler('manipulateMenuItem1', function(message)
            {
            var aNodeList = document.getElementsByTagName('a');
    
            for (var i = 0; i < aNodeList.length; i++) 
            {
            if(aNodeList[i].getAttribute('data-toggle') == message.toggle && aNodeList[i].getAttribute('role') == message.role) 
            {
            if(message.action == 'hide')
            {
            aNodeList[i].setAttribute('style', 'display: none;');
            } 
            else 
            {
            aNodeList[i].setAttribute('style', 'display: block;');
            };
            };
            }
            });"
                                                          )
        )
        )
        ),
      dashboardBody(
        useShinyjs(),
        actionButton("h1","Hide toggle"),
        actionButton("h2","Show toggle")
      )
    ))
    
    server <- shinyServer(function(input, output, session) {
      observeEvent(input$h1,{
         session$sendCustomMessage(type = "manipulateMenuItem1", message = list(action = "hide",toggle = "offcanvas", role = "button"))
        })
      observeEvent(input$h2,{
        session$sendCustomMessage(type = "manipulateMenuItem1", message = list(action = "show",toggle = "offcanvas", role = "button"))
      })
    })
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      【解决方案2】:

      如果您使用 shinyjs 包,您可以使用快速的 JavaScript 行显示或隐藏侧边栏切换。

      library(shiny)
      library(shinydashboard)
      library(shinyjs)
      
      ui <- shinyUI(dashboardPage(
        dashboardHeader(),
        dashboardSidebar(),
        dashboardBody(
          useShinyjs(),
          actionButton("hide","Hide toggle"),
          actionButton("show","Show toggle")
        )
      ))
      
      server <- shinyServer(function(input, output, session) {
        observeEvent(input$hide,{
          shinyjs::runjs("document.getElementsByClassName('sidebar-toggle')[0].style.visibility = 'hidden';")
        })
        observeEvent(input$show,{
          shinyjs::runjs("document.getElementsByClassName('sidebar-toggle')[0].style.visibility = 'visible';")
        })
      })
      
      shinyApp(ui = ui, server = server)
      

      JavaScript 本身仅引用类 sidebar-toggle 的第一个元素(即菜单按钮),并根据用户按下的按钮切换可见性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-14
        • 2020-04-19
        • 1970-01-01
        • 2018-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多