Geo*_*any 5 shiny shinydashboard shinyjs
当事情发生时,我正试图手动折叠一个盒子.似乎我只需要将类添加"collapsed-box"到框中,我尝试使用shinyjs函数addClass,但我不知道如何做到这一点,因为一个盒子没有id.这里是可用于测试可能解决方案的简单基本代码:
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
box(collapsible = TRUE,p("Test")),
actionButton("bt1", "Collapse")
)
)
server <- function(input, output) {
observeEvent(input$bt1, {
# collapse the box
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
Dea*_*ali 13
我从来没有尝试过使用过盒子,所以请记住,我的回答可能非常狭隘.但我快速浏览一下,看起来简单地设置盒子上的"折叠盒子"类并不会让盒子崩溃.所以我的下一个想法是以编程方式实际单击折叠按钮.
如你所说,没有与该框相关联的标识符,所以我的解决方案是添加一个id参数box.我最初期望它是盒子的id,但是它看起来像id被赋予盒子里面的元素.没问题 - 它只是意味着为了选择折叠按钮,我们需要获取id,查找DOM树以找到box元素,然后向下看DOM树以找到按钮.
我希望我说的一切都有道理.即使它没有,这个代码应该仍然有效,并希望使事情更清楚:)
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),
actionButton("bt1", "Collapse box1"),
actionButton("bt2", "Collapse box2"),
br(), br(),
box(id = "box1", collapsible = TRUE, p("Box 1")),
box(id = "box2", collapsible = TRUE, p("Box 2"))
)
)
server <- function(input, output) {
observeEvent(input$bt1, {
js$collapse("box1")
})
observeEvent(input$bt2, {
js$collapse("box2")
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)