My form has a list of items, and each item has a delete button. I need to submit the index of the item to be deleted, along with the other values of the form (for further editing).
With JavaScript, it looks like this:
<g:form method="post" mapping="defaultAction" id="${paymentInstance?.id}">
<g:hiddenField name="id" value="${paymentInstance?.id}"/>
<g:hiddenField name="version" value="${paymentInstance?.version}"/>
<g:hiddenField name="deleteAdjIdx"/>
<g:each in="${paymentInstance?.adjustments}" var="adj" status="idx">
<g:set var="adjName" value="adjustments[${idx}]"/>
<g:textField name="${adjName}.dollars" value="${adj.dollars}"/>
<g:actionSubmit action="deleteAdj" value="delete" onclick="jQuery('#deleteAdjIdx').val(${idx})"/>
</g:each>
</g:form>
How can I do this without JavaScript? (Can I add a param to the URL, in addition to the posted params? Or, some kind of multiplex in the mapping?)
The defaultAction mapping is:
name defaultAction: "/$controller/$id"{ // stable URL for payments regardless of current status (editable or not)
constraints {
id(matches: /\d+/) // since our action names don't start with a digit but many domain ids do
}
}
I’m going to try adding params to the action name in an actionSubmit:
I didn’t find a nice way to do this, so I came up with the following hack. If anyone knows of any improvements, I’d appreciate it.
Added to MyTagLib:
I unpack the params and restore the original action name in a filter:
I made utility methods to override the request params and configure the filter:
Finally, I added the following to _Events.groovy to configure the filter in web.xml by extending ControllersGrailsPlugin.doWithWebDescriptor: