覆盖 Lift ajaxRadio toForm 默认标记

发布于 2024-10-11 23:09:30 字数 327 浏览 5 评论 0原文

我的表单中有 ajaxRadio,但 Lift 为我生成了不需要的标记(它用 span 包装输入标记,我想标记 tag)。 可以覆盖默认的 toForm 函数或完全覆盖 htmlize 方法,该方法称为

object ChoiceHolder {  
    var htmlize: ChoiceItem[_] => NodeSeq = c => (<span>{c.xhtml} {c.key.toString}<br/> </span>)  
  }

那么如何获取输入标签周围的标签而不是跨度?

多谢。

I have ajaxRadio in my form, but Lift generates undesirable markup for me (it wraps input tag with span, and I would like to label tag).
It is possible to override default toForm function or exactly override method htmlize, which is called

object ChoiceHolder {  
    var htmlize: ChoiceItem[_] => NodeSeq = c => (<span>{c.xhtml} {c.key.toString}<br/> </span>)  
  }

So how to get, that around input tags will be labels and not spans?

thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你曾走过我的故事 2024-10-18 23:09:30

考虑到 ChoiceItem 类似于

val choiceItem = ChoiceItem("Some key", <input type="radio" value="val" name="somename" />)

,并且您按如下方式调用 toForm 方法,

ChoiceHolder(Seq(choiceItem)).toForm

您可以使用

ChoiceHolder.htmlize = ci => evalElemWithId((id, e) => e ++ <label for={id}>{ci.key.toString}</label>)(ci.xhtml)

它可能返回类似

NodeSeq(<input name="somename" type="radio" value="val" id="F212153483008MMP"></input>, <label for="F212153483008MMP">Some key</label>)

or 的内容,如果您想将标签包裹起来

ChoiceHolder.htmlize = ci => evalElemWithId((id, e) => <label for={id}>{e} {ci.key.toString}</label>)(ci.xhtml)

,它可以为您提供

NodeSeq(<label for="F689721302326S3S"><input name="somename" type="radio" value="val" id="F689721302326S3S"></input>Some key</label>)

有点复杂:evalElemWithId (import net.liftweb.util.Helpers.evalElemWithId) 获取 NodeSeq 的第一个元素(例如 choiceItem.xhtml ),并向其添加一个 id 属性。之后,将调用接收此 id 的匿名函数,以便您可以在标签的 for 属性中使用它。

编辑
请注意,ChoiceHolder.htmlize 是一种全局函数,将通过所有 ChoiceHolder.toForm 调用进行调用。因此,只有当您想在所有地方都具有相同的布局时,全局更改它才有意义。

如果您只想在一种情况下拥有不同的布局,那么最好只调用一些 choiceToForm 方法。

Considering that the ChoiceItem is something like

val choiceItem = ChoiceItem("Some key", <input type="radio" value="val" name="somename" />)

and you call the toForm method as follows

ChoiceHolder(Seq(choiceItem)).toForm

you could use

ChoiceHolder.htmlize = ci => evalElemWithId((id, e) => e ++ <label for={id}>{ci.key.toString}</label>)(ci.xhtml)

which might return something like

NodeSeq(<input name="somename" type="radio" value="val" id="F212153483008MMP"></input>, <label for="F212153483008MMP">Some key</label>)

or, if you want to wrap the label around

ChoiceHolder.htmlize = ci => evalElemWithId((id, e) => <label for={id}>{e} {ci.key.toString}</label>)(ci.xhtml)

which gives you

NodeSeq(<label for="F689721302326S3S"><input name="somename" type="radio" value="val" id="F689721302326S3S"></input>Some key</label>)

A bit complicated: evalElemWithId (import net.liftweb.util.Helpers.evalElemWithId) takes the first element of a NodeSeq (e.g. choiceItem.xhtml), and adds an id attribute to it. Afterwards the anonymous function will be called which receives this id so that you can use it in the for attribute of the label.

Edit
Note that ChoiceHolder.htmlize is kind of a global function which will be called through all ChoiceHolder.toForm calls. So it only makes sense to globally change it if you want to have the same layout everywhere.

If you just want to have a distinct layout in one case, you’re probably better off just calling some choiceToForm method.

热血少△年 2024-10-18 23:09:30

所以最后我自己找到了解决方案。
它不是我想象的那么干净,但它有效!

因此,对于具有相同问题的下一代 - 我用我自己的函数包装 ajaxRadio:

choicesToMyForm( SHtml.ajaxRadio( ... ) )

并且该函数如下所示:

def choicesToMyForm(choices : ChoiceHolder[String]) : NodeSeq =
{
    choices.flatMap( c => (<label>{c.xhtml} {c.key.toString}</label>) )
}

So finally I found solution by myself.
It's not so clean I would expected, but it works!

So for future generation with same problem - I wrap ajaxRadio with my own function:

choicesToMyForm( SHtml.ajaxRadio( ... ) )

and this function looks like:

def choicesToMyForm(choices : ChoiceHolder[String]) : NodeSeq =
{
    choices.flatMap( c => (<label>{c.xhtml} {c.key.toString}</label>) )
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文