【问题标题】:Grails checkbox handlingGrails 复选框处理
【发布时间】:2011-11-19 18:40:52
【问题描述】:

假设,我有这个场景:

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

所以,我需要这个:

a) 一种在数百个复选框中知道哪些复选框被选中的方法 b) 每个复选框都应该与我要传递的 id 进行“链接”,以便执行特定的操作。

我有一个<g:each> 标记给我写整个表,从数据库中读取值。我将不胜感激任何帮助, 提前致谢,RR

【问题讨论】:

    标签: grails checkbox handle


    【解决方案1】:

    您可以将参数绑定到域对象或命令对象的 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())
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      在您的 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)
              }
          }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-11
        • 1970-01-01
        • 1970-01-01
        • 2010-11-01
        • 2021-10-05
        • 2020-02-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多