Scala lift框架,提交多个值的ajax表单?

发布于 2024-07-16 16:13:08 字数 984 浏览 9 评论 0原文

我刚刚开始使用 lift,现在尝试将普通形式更改为 ajax 形式,但从未调用方法 processEntryAdd

def addUser(xhtml : Group) : NodeSeq = {

    var firstName = ""
    var lastName = ""

    def processEntryAdd() {
        Log.info("processEntryAdd: " + firstName + ", " + lastName)
    }

    SHtml.ajaxForm(
        bind("entry", xhtml,
             "firstName" -> SHtml.text(firstName, 
                 (x) => {
                     Log.info("Setting firstName to " + x); 
                     firstName = x
                 }),
             "lastName" -> SHtml.text(lastName, 
                 (x) => {
                     Log.info("Setting lastName to " + x); 
                     lastName = x
                 }),
             "submit" -> SHtml.submit("Add user", processEntryAdd),
        ))
}

知道如何实现我想要做的事情,或者为什么上面的代码不起作用。 按下按钮并设置两个局部变量 firstNamelastName 时,会提交两个表单字段的值,但不会调用与 SHtml.submit 关联的函数。

谢谢!

I am just getting started with lift and I am now trying to change a normal form to an ajax form but the method processEntryAdd is never called.

def addUser(xhtml : Group) : NodeSeq = {

    var firstName = ""
    var lastName = ""

    def processEntryAdd() {
        Log.info("processEntryAdd: " + firstName + ", " + lastName)
    }

    SHtml.ajaxForm(
        bind("entry", xhtml,
             "firstName" -> SHtml.text(firstName, 
                 (x) => {
                     Log.info("Setting firstName to " + x); 
                     firstName = x
                 }),
             "lastName" -> SHtml.text(lastName, 
                 (x) => {
                     Log.info("Setting lastName to " + x); 
                     lastName = x
                 }),
             "submit" -> SHtml.submit("Add user", processEntryAdd),
        ))
}

Any idea how to achieve what I am trying to do, or why the code above doesn't work.
The values of the two form fields are submitted when the button is pressed and the two local variables firstName and lastName are set but the function associated with SHtml.submit isn't called.

Thanks!

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

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

发布评论

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

评论(3

素手挽清风 2024-07-23 16:13:08

这个问题有点老了,但我最近需要自己知道这一点,这是迄今为止我见过的最好的解决方案:

ajaxForm(
    bind("entry", xhtml,
         "firstName" -> text(firstName, firstName = _),
         "lastName" -> text(lastName, lastName = _),
         "submit" -> submit("Add user", processEntryAdd _),
    ) ++ hidden(processEntryAdd _)
)

通过将处理添加到隐藏的表单元素,您可以保留提交按钮,而不需要更改任何内容查看代码。

您可以通过让 processEntryAdd() 返回 JsCmd 来添加客户端行为:

def processEntryAdd() {
    Log.info("processEntryAdd: " + firstName + ", " + lastName)
    JsRaw("alert('process entry added')")
}

This question is kind of old, but I recently needed to know this myself, and this is the best solution I've seen so far:

ajaxForm(
    bind("entry", xhtml,
         "firstName" -> text(firstName, firstName = _),
         "lastName" -> text(lastName, lastName = _),
         "submit" -> submit("Add user", processEntryAdd _),
    ) ++ hidden(processEntryAdd _)
)

By adding the processing to a hidden form element you get to keep the the submit button, without changing any view code.

You can add client side behaviour by having processEntryAdd() return a JsCmd:

def processEntryAdd() {
    Log.info("processEntryAdd: " + firstName + ", " + lastName)
    JsRaw("alert('process entry added')")
}
挽清梦 2024-07-23 16:13:08

在回答这个问题时,David Pollak 建议使用

“提交”-> SHtml.hidden("添加用户", processEntryAdd) ++

在电梯邮件列表上。

In response to this question David Pollak suggested using

"submit" -> SHtml.hidden("Add user", processEntryAdd) ++

on the lift mailing list.

终陌 2024-07-23 16:13:08

这就是答案,滚动到底部,(忽略<之后的第一个空格)

http:// /www.assembla.com/wiki/show/liftweb/ajaxForm

“提交”-> (SHtml.hidden(auth) ++ <输入类型=“提交”值=“登录”/>)

Here's the answer, scroll to the bottom, (ignore the first space after <)

http://www.assembla.com/wiki/show/liftweb/ajaxForm

"submit" -> (SHtml.hidden(auth) ++ < input type="submit" value="Login"/>)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文