使用 scalatags 创建 table 正文后添加 table 行

Add table row after creating table body using scalatags

使用 scalatags,通常我会像这样创建 table:

table(
  thead(
    tr(
      th("A"),
      th("B")
    )
  ),
  tbody(
   tr(
    td("HELLO"),
    td("WORLD")
   ),
   tr(
    td("FOO"),
    td("BAR")
   )
  )
)

是否也可以在 table 声明后添加 tr 元素?这样的东西会很棒:

val myTableBody = tbody(
   tr(
    td("HELLO"),
    td("WORLD")
   )
)

table(
  thead(
    tr(
      th("A"),
      th("B")
    )
  ),
  myTableBody
)

if(myCondition){
  myTableBody.addTr( // this is what I am searching for
   tr(
    td("FOO"),
    td("BAR")
   )
  )
}

*编辑: 为了澄清,目前我正在做这样的事情:

table(
  thead(
    tr(
      th("A"),
      th("B")
    )
  ),
  tbody(
   tr(
    td("HELLO"),
    td("WORLD")
   ),
   if(myCondition){
    tr(
     td("FOO"),
     td("BAR")
    )
   } else {
    tr() // this is a bit ugly
   }
  )
)

当然可以,但与 Scalatags 本身无关。这是非常正常的 DOM 操作,您需要一个库或框架来处理它。 可以完成in raw DOM, but it's more common to develop web applications using frameworks such as React, or at least libraries such as jQuery. There are zillions of such options (including a somewhat rough Scalatags-based on I built myself);您应该选择最适合您需求的一款...