Reputation: 276
I have a table of data, some thing like below
Id Name LastName
1 John Doe
2 Jane Roi
3 Jerry amol
4 Jeff Jels
'SAVE' 'CANCEL'
all the above rows are editable, i can edit all the 4 rows (id, name, lastname) and hit 'SAVE' button. The challenge here is i have pass all 4 rows values to controller and validate each filed, if any errors have to send the error back to view against each row, if fine then update in database. How to achieve this? please help me
Here is my view:
<table id='show_brands_table'>
<tbody>
<g:each in="${brandsList}" status="i" var="brands">
<tr id="tr_brand_${brands.id}" class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td>
<input type="text" id="brand_${brands.id}" value="${brands.brand}" class="${(i % 2) == 0 ? 'even' : 'odd'}"></input>
</td>
<td>
<input type="text" id="brandID_${brands.id}" value="${brands.bpBrandId}" class="${(i % 2) == 0 ? 'even' : 'odd'}"></input>
</td>
<td>
<g:select id="weight${id}" name="weight${id}"from="${}"value="${weight}" onchange="modify();" />
</td>
</tr>
</g:each>
</tbody>
Upvotes: 3
Views: 1651
Reputation: 66059
Grails can bind form results to multiple objects. Use a prefix followed by a dot for form element names to distinguish the individual objects. Example:
<g:each in="${brandsList}" status="i" var="brand">
<input type="text" name="brand_${brand.id}.name" value="${brand.name}">
<input type="text" name="brand_${brand.id}.weight" value="${brand.weight}">
...
</g:each>
In your controller, you'll have to loop over the params and find out how many objects have been submitted. You can look for the prefix as a param, and then use it as a name space:
def update() {
for (name in params.keySet()) {
def match = name =~ /^brand_(\d+)$/
if (match) {
def id = m[0][1] as long
def brand = Brand.get(id)
brand.properties['name', 'weight'] = params[name]
brand.save()
}
}
}
See the Grails Data Binding section in the manual for more information.
Upvotes: 2