Reputation: 1584
I have three domain classes: MaterialsOfConcern, Material and MaterialStatus setup as such:
class MaterialsOfConcern {
Material material
MaterialStatus materialStatus
}
I'd like to create a form where the user is presented with a static list of Materials that already exist in the system. For each Material listed they must select a corresponding MaterialStatus. Is there a standard approach for this?
I was thinking of the following in the gsp:
<g:each var="material" in="${materials}">
<g:select name="materialStatus[${material.id}].id" from="${MaterialStatus.list()}" value="?" class="many-to-one"/>
</g:each>
Then in the controller just manually parsing the params object for information I need. But I thought there must be a better, more standardized way, using bindData on a Map or Command object or the like.
Upvotes: 0
Views: 798
Reputation: 3532
I am not sure if it is actually a better alternative. But Grails does provide a feature where multiple form fields with the same name are grouped as a list.
Using your example, you could simply pass the list of material statuses to your controller and then resolve them based on their list order.
<g:each var="material" in="${materials}">
<g:select name="materialStatus" from="${MaterialStatus.list()}"/>
</g:each>
Then, you can get a list of material statuses using the params.list mechanism.
def statuses = params.list( 'materialStatus' )
materials.eachWithIndex{ material, index -> new MaterialOfConcern( material: material, materialStatus: statuses[ index ] ) }
Upvotes: 1