tags:

views:

407

answers:

2

I create a GSP page with controls depending on the rows in a database.
This depends on the value returned by the <g:each in="${Vehicles}" var="vehicle"> So, if there are 3 vehicles, 3 rows with text boxes will be generated. (The maximum can be 200)

<g:form action="update" >
      <label for="SearchTerm">${term}</label>

          <g:each in="${Vehicles}" var="vehicle">
            <tr>
                     <td> <label for="Name">${vehicle.name}</label> </td>
                     <td><g:textField name="${vehicle.id}.ModelNo" /> </td>
                     <td><g:textField name="${vehicle.id}.Year" /> </td>
            </tr>
          </g:each>
  <td> <g:submitButton name="update" value="Update"/></td>
  </g:form>

How can I basically pass this value to my controller so that I can then save/update the data to the database. or Is there any easy way to achieve this scenario?

+1  A: 

You need to use the request object from your controller. If you can generate the names of the controls you need to access do something like the following

idList.each {
theYear=request.getParameter(it+Year)
}

If you want a list of all your generated form fields use something like

java.util.Enumeration theFields=request.getParameterNames()
theFields.each {
//look at your field name and take appropriate action
}

For more info on the request object see this

Jared
+1  A: 

You need some code like this in the GSP

    <g:form action="update" >
          <label for="SearchTerm">${term}</label>

              <g:each in="${Vehicles}" var="vehicle" status="i">
                <tr>
                         <td> <label for="Name">${vehicle.name}</label> </td>
                         <td><g:hiddenField name="vehicle[${i}].id" value="${vehicle.id}"/>
<g:textField name="vehicle[${i}].ModelNo" value="${vehicle.ModelNo}"/> </td>
                         <td><g:textField name="vehicle[${i}].Year" value="${vehicle.Year}"/> </td>
                </tr>
              </g:each>
      <td> <g:submitButton name="update" value="Update"/></td>
    </g:form>

The Controller needs to either have a Domain with a List Property or a Command Object with a List Property ie

SearchCommand {   
  List<Vehicle> vehicle = new Arraylist<Vehicle>(3); 
}

Then in the controller (if using the command object)

def save = {SearchCommand searchCmd-> 
  searchCmd.vehicle.each {vehicle ->
     /* Process Vehicle */
  }
}

Hope that Helps

Scott Warren