I have two classes a parent class:
class parentItem{
User user
Date startTime
Date endTime
------
Date dateCreated
Date lastUpdated
static hasMany = [ childItems: ChildItem ]
and a child Class
class ChildItem{
static belongsTo = [parent : ParentItem]
---------
Date time
Date dateCreated
Date lastUpdated
In the gsp file that allows items to be edited I have a table with rows that look like this:
<table id="childDataTable" class="childData">
<g:each in="${ParentInstance?.childItems?.sort{it.id}}" var="childItem" status="i">
<tr class="childRow">
<td>
<g:hiddenField name="childItems[${i}].id" value="${childItem.id}"/</td>
------
<td>
<g:textField name="childItems[${i}].time" class="time" value="${childItem.time}"/>
</td>
<td><a href="#" class="removeButton">✖</a></td>
</tr>
</g:each>
</table>
So my question stems from the fact that in the controller I've tried to update this parentItem where some of the childItem data may also have been changed. I need both the parentItem and the collection of childItems to update and save.
In the controller I've tried this
def parentInstance = ParentItem.get(params.id)
bindData(parentInstance, params)
and I've tried
def parentInstance = ParentItem.get(params.id
parentInstance.properties = params
Both ways I get a Out of Bounds Exception
In a case where I have two child Items the message looks like this:
Index: 1, Size: 1. Stacktrace follows:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at com.sexingtechnologies.laboratory.ParentItemController$_closure7$$EOChoLzu.doCall(ParentItemController.groovy:158)
at grails.plugin.multitenant.core.servlet.CurrentTenantServletFilter.doFilter(CurrentTenantServletFilter.java:53)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
I'm not sure where the error exists or how to solve it.
I've tried to extract the childItems from params map but have not been able to do so successfully.
Any help you be appreciated
Maybe you could initialize the childItems
before the data binding like so:
def parentInstance = ParentItem.get(params.id)
parentInstance.childItems = [].withDefault { new ChildItem() }
bindData(parentInstance, params)