我正在尝试在Scala文档中构建自定义FieldConstructor。

我按照说明进行操作,构建一个twitterBootstrapInput.scala.html
它有效...

这是我的输出:

问题来了:

我希望@helper.inputRadioGroup跨“水平”跨度,而不是垂直跨度。

(因为twitterBootstrapInput.scala.html@elements.input块中扭曲了<div>)

但是我不知道如何在不感染其他“文本域”的情况下修改模板?

我应该定义另一个hiddenFieldConstructor吗?还是做其他事情?

我找不到有关如何解决此问题的示例...

所有自定义模板文档都很少见。

有人可以给我一个例子吗?谢谢 !

这是我的代码(播放2.1):

@import views.html.helper.FieldConstructor
@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.f) }

@helper.inputRadioGroup(consultForm("currency")
  , options = Seq(
    "USD" -> "USD"
  , "HKD" -> "HKD"
  , "RMB" -> "RMB")
  , '_label -> "Currency"
  , '_error -> consultForm("currency").error.map(_.withMessage("select currency"))
)

======== 4月11日更新==============

感谢@Schleichardt给我的第一步。我在(FieldConstructor(twitterBootstrapRadioGroup.f) , lang)后面附加inputRadioGroup,看来可行。但是,即使我在模板中写了最简单的@elements.input(没有其他装饰),它仍然垂直延伸。如下面的截图:

输出的html源代码为:

如何修改<span class="buttonset" ...>块?
我不应该在模板中调用@elements.input吗?

最佳答案

简化版:

@(consultForm: Form[Consult])(implicit lang: play.api.i18n.Lang)

@import views.html.helper.FieldConstructor
@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.f) }

@* uses twitterBootstrapInput */
@helper.inputText(consultForm("name"))

@* uses anotherFieldConstructor */
@helper.inputRadioGroup(consultForm("currency")
  , options = Seq(
    "USD" -> "USD"
  , "HKD" -> "HKD"
  , "RMB" -> "RMB")
  , '_label -> "Currency"
  , '_error -> consultForm("currency").error.map(_.withMessage("select currency"))
)(FieldConstructor(anotherFieldConstructor.f), lang)


@* uses twitterBootstrapInput */
@helper.inputText(consultForm("anotherFormFieldName"))
anotherFieldConstructor是您必须创建的其他FieldConstructor。它应该根据您的需要放置单选按钮。

较长版本:

inputRadioGroup的API DOC在这里:http://www.playframework.com/documentation/api/2.1.0/scala/index.html#views.html.helper.inputRadioGroup $

由于inputRadioGroup是Scala单例对象,因此以下代码语句相同:
helper.inputRadioGroup(consultForm("currency") /* etc. */)
helper.inputRadioGroup.apply(consultForm("currency") /* etc. */)

inputRadioGroup的apply方法具有两个参数列表。第二个列表使用隐式参数。

使用@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.f) }指定时,twitterBootstrapInput FieldConstructor是inputRadioGroup的第二个参数列表中处理程序的“默认参数”。

但是您可以使用显式参数覆盖它:
helper.inputRadioGroup(consultForm("currency") /* etc. */)(FieldConstructor(anotherFieldConstructor.f), lang)

如果使用Scala控制器,则模板需要lang的其他参数列表:
@(consultForm: Form[Consult)])(implicit lang: play.api.i18n.Lang)

模板中不能有两个隐式的FieldConstructors。

关于playframework - Play Framework 2.1中的多字段构造函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15930343/

10-10 12:56