Grails checkbox handling

一笑奈何 提交于 2019-12-08 06:42:57

问题


Let's say, i have this scenerio:

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


回答1:


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)
        }
    }

}




回答2:


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())
        }
    }
}


来源:https://stackoverflow.com/questions/8196321/grails-checkbox-handling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!