Search code examples
grailsgroovygsp

How to get checked items in gsp/groovy


I have code similar to as mentioned below.

<g:each in="${Accounts}" var="account">
<li>
    <g:hiddenField name="id" value="${account.id}" />
    <g:hiddenField name="memberId" value="${account.memberId}" />
    <g:hiddenField name="accountType" value="${account.type}" />
    <label><input type="checkbox" />${member.name}</label>
</li>
</g:each>

In my groovy controller, I am trying to access the list of accounts checked by the user.

Could somebody help me how I can get the checked items in my controller 'params'

Thanks.


Solution

  • GSP:

      <g:form controller="some" action="updateAccountIds">
        <g:each in="${Accounts}" var="account">
           ...
          <label><g:checkBox name="checkedAccount" value="${account.id}"/>${member.name}</label>
        </g:each>
      </g:form>
    

    Controller action:

    def updateAccountIds(){
       def ids = params.list 'checkedAccount'
       log.info "checked = $ids"
    
       doOtherStuff()
    }