Grails 复选框处理

发布于 2024-12-17 04:06:33 字数 333 浏览 4 评论 0原文

比方说,我有这样的场景:

在此处输入图像描述

但是比方说我有数百个这样的复选框,我需要这些复选框提交表格后同时处理所有事情。然后,我需要根据选中的框以及每个块的 id 将一些内容保存到 BD

所以,我需要这个:

a)一种知道选中了数百个复选框的方法 b) 每个复选框都应该与我要传递的 ID '链接',以便执行特定操作。

我有一个 标签向我写入整个表,从数据库读取值。我将不胜感激任何帮助, 提前致谢,RR

Let's say, i have this scenerio:

enter image description here

But let's say i have hundreds of those checkBoxes, which i need to handle everything at same time after submiting a form. I then will need to save to the BD something based on which boxes are checked, and the id of each block

So, i need this:

a) a way to know which checkboxes are checked, within hundreds of them
b) each checkbox should be 'linked' with an id which im gona pass, so that a specific action will be performed.

I have a <g:each> tag writing me the whole table, reading values from the DB. I would appreciate any help with this,
Thanks in advanced, RR

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

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

发布评论

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

评论(2

对不⑦ 2024-12-24 04:06:33

您可以将参数绑定到域对象或命令对象的 List 属性。

视图:

<g:each in="${elements}">
    <g:checkBox name="elementSelected[${it.id}]" value="${it.id}" />
</g:each>

命令对象:

class ElementCommand {
    List elementSelected
}

控制器:

def execute = { ElementCommand cmd ->       
    cmd.elementSelected.each {
        if (it) {
            processId(it.toInteger())
        }
    }
}

You can bind the params to a List property of a domain object or command object.

View:

<g:each in="${elements}">
    <g:checkBox name="elementSelected[${it.id}]" value="${it.id}" />
</g:each>

Command Object:

class ElementCommand {
    List elementSelected
}

Controller:

def execute = { ElementCommand cmd ->       
    cmd.elementSelected.each {
        if (it) {
            processId(it.toInteger())
        }
    }
}
歌入人心 2024-12-24 04:06:33

在您的 gsp 中,您需要显示所有复选框:

<g:each in="${model}" status="i" var="invoiceItem">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
    <td>
         <g:checkBox name="invoiceItem_${i}"/>
    </td>
   </tr>
</g:each>

在控制器操作中,您需要将选定的复选框映射到您的域对象

List invoiceList = session.invoiceList
params.each {
    if (it.key.contains("invoiceItem_")){
        if (it.value.contains("on")){
            InvoiceItem invoiceItem = invoiceList.get((it.key - "invoiceItem_") as Integer)
        }
    }

}

In your gsp you need to display all the checkboxes:

<g:each in="${model}" status="i" var="invoiceItem">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
    <td>
         <g:checkBox name="invoiceItem_${i}"/>
    </td>
   </tr>
</g:each>

In the controller action you need to map the selected checkboxes to your domain objects

List invoiceList = session.invoiceList
params.each {
    if (it.key.contains("invoiceItem_")){
        if (it.value.contains("on")){
            InvoiceItem invoiceItem = invoiceList.get((it.key - "invoiceItem_") as Integer)
        }
    }

}

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