向元素和所有子元素添加或更新属性

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

我正在使用fillPage()进行整页布局。对于一个能够垂直拉伸的盒子,定义这个盒子的所有元素都需要有一个style = "height: 100%"属性(或者我被告知)。有没有办法为元素和所有子元素追加/更新style属性?

我已经提出了一个似乎适用于我的案例的实现,但我可能错过了一些细节。

library(htmltools)

tagAppendAttributesAll <- function(x, ...) {
  if (!is.list(x)) return(x)
  if (inherits(x, "shiny.tag.list")) {
    x[] <- purrr::map(x[], tagAppendAttributesAll, ...)
    x
  } else {
    x <- tagSetChildren(
      x,
      list = purrr::map(x$children[], tagAppendAttributesAll, ...)
    )
    tagAppendAttributes(x, ...)
  }
}

tagSetFullHeightAll <- function(x) {
  tagAppendAttributesAll(x, style = "height: 100%;")
}

print(tagSetFullHeightAll(
  div(
    div(
      div("test"),
      style = "height: 400px; "
    )
  )
))
#> <div style="height: 100%;">
#>   <div style="height: 400px;  height: 100%;">
#>     <div style="height: 100%;"></div>
#>   </div>
#> </div>

reprex package创建于2019-04-29(v0.2.1.9000)

r shiny htmltools
1个回答
0
投票

也许我误解了你的问题。你试过使用'vh'单位吗?在我的一个闪亮的应用程序中,我正在使用这一行:

leafletOutput(“rutMap”,width ='100%',height ='75vh')

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