嵌套手风琴似乎不遵守 `multiple=FALSE` 参数

问题描述 投票:0回答:1

当使用

bslib::accordion()
bslib::accordion_panel()
时,如下所示,我有一个带有 s(例如
s=3
)面板的手风琴。我希望每个 Accordion_panels 都包含一个 Accordion,它本身包含 t 个(比如
t=2
)面板。构造这个没问题,如下:

library(shiny)
library(bslib)

accordion(
  accordion_panel(
    title = "panel_1",
    accordion(
      accordion_panel(title = "panel_1_sub_1", HTML("panel 1, subpanel 1 content")),
      accordion_panel(title = "panel_1_sub_2", HTML("panel 1, subpanel 2 content")),
      open=FALSE,
      multiple=FALSE # this is not working

    )
  ),
  accordion_panel(
    title = "panel_2",
    accordion(
      accordion_panel(title = "panel_2_sub_1", HTML("panel 2, subpanel 1 content")),
      accordion_panel(title = "panel_2_sub_2", HTML("panel 2, subpanel 2 content")),
      open=FALSE, 
      multiple=FALSE # this is not working
    )
  ),
  accordion_panel(
    title = "panel_3",
    accordion(
      accordion_panel(title = "panel_3_sub_1", HTML("panel 3, subpanel 1 content")),
      accordion_panel(title = "panel_3_sub_2", HTML("panel 3, subpanel 2 content")),
      open=FALSE, 
      multiple=FALSE # this is not working
    )
  ),
  open=FALSE, 
  multiple=FALSE # this is working fine
)

启动后,所有三个面板和每个嵌套的手风琴都关闭。根据外层/父级手风琴的要求,只允许打开一个手风琴面板

multiple=FALSE
,这效果很好。

但是,虽然每个内部手风琴也有

multiple=FALSE
,但这些内部手风琴中的多个面板确实会打开(即,当选择该内部手风琴中的另一个面板时,内部手风琴中的面板不会自动关闭)。

有人知道这个问题的解决方法,或者我在上面的嵌套手风琴设计中做错了什么吗?

r shiny accordion bslib
1个回答
0
投票

内部

accordion_panel
保持开放的原因是它们在创建时分配了多个父级(属性
data-bs-parent
)。例如。对于你的第一个嵌套
accordion
,父母看起来像这样:

[1] "#bslib-accordion-7599" # "panel_1"
[1] "#bslib-accordion-5703 #bslib-accordion-7599" # "panel_1_sub_1"
[1] "#bslib-accordion-5703 #bslib-accordion-7599" # "panel_1_sub_2"

但是,只有第一个才是“真正的”父级。这个问题必须在

bslib
中解决。只要#819打开,您就可以使用以下解决方法。

我们的想法是,我们必须删除

data-bs-parent
属性的所有部分(除了第一个部分)。上面的
accordion
nestedAcc
,然后我们使用
htmltools::tagQuery
:

来操作它
nestedAcc <- accordion( # defined as in the question
  accordion_panel(
  ...
  )
)

tagQ <- htmltools::tagQuery(nestedAcc)
tagQ$
  find(".accordion-collapse.collapse")$
  each(function(el, i) {
    # save all parents
    parents <- el$attribs[names(el$attribs) == "data-bs-parent"] 
    # remove all parents
    el$attribs <- el$attribs[names(el$attribs) != "data-bs-parent"]
    # append the "true" parent, which is the first one
    tagAppendAttributes(el, `data-bs-parent` = unlist(parents)[1])
    # return edited tag
    el
  })$
  allTags()

这会产生一个

accordion
,其中内面板具有
multiple = FALSE

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.