Home > Net >  I need JS code to collapse a box in Shiny/bs4Dash
I need JS code to collapse a box in Shiny/bs4Dash

Time:01-30

I try to collapse a box with an action. This code works in shinydashboard how should I change jscode for doing same thing in bs4Dash ? Any idea?

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

jscode <- "
shinyjs.collapse = function(boxid) {
$('#'   boxid).closest('.box').find('[data-widget=collapse]').click();
}
"

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    useShinyjs(),
    extendShinyjs(text = jscode, functions = c("collapse")),
    actionButton("bt2", "Collapse box2"),
    br(), br(),
    box(id = "box2", collapsible = TRUE, p("Box 2"))
  )
)

server <- function(input, output) {

  observeEvent(input$bt2, {
    js$collapse("box2")
  })
}

shinyApp(ui, server)

CodePudding user response:

bs4Dash lets you do this without any javascript.

  observeEvent(input$bt2, {
    updateBox('box2', action = 'toggle')
  })

App:

library(shiny)
library(bs4Dash)
library(shinyjs)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    actionButton("bt2", "Collapse box2"),
    br(),
    br(),
    box(
      id = "box2",
      collapsible = TRUE,
      p("Box 2")
    )
  )
)

server <- function(input, output) {
  observeEvent(input$bt2, {
    updateBox("box2", action = "toggle")
  })
}

shinyApp(ui, server)
  • Related