Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a parameter to a submit button without using JavaScript?

Tags:

grails

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
        }
    }
like image 809
J. David Beutel Avatar asked Apr 29 '26 00:04

J. David Beutel


1 Answers

I just had the same problem and found another solution using HTML5's "formaction" attributes. They can be given a value that can include a controller, action, additional parameters, etc.

In General, to add a parameter to a submit button (such as a delete of a specific sub-object) on a form would looks like this:

<input type="submit" formaction="/<controller>/<action>/<id>?additionalParam1=...&additionalParam2=..." value="Delete" >

and in your example:

<input type="submit" formaction="/payment/deleteAdj/${adj.id}" value="delete" >

or

<input type="submit" formaction="/payment/deleteAdj?delAdjID=${adj.id}" value="delete" >

in the deleteAdj you would delete the adjustment stated in params.id or params.delAdjID and either save the data within params or use the params directly to re-fill the form (e.g., when rendering create.gsp).

like image 97
Jörg Rech Avatar answered Apr 30 '26 15:04

Jörg Rech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!