How to pass parameters using action button in grails

had torrents with a button that has an action. I have a few bits that I want to learn about its parameter. The Grails manual says it should be like this:

<g:actionSubmit action="action" value="${message(code: 'default.button.edit.label', default: 'Edit')}" params="['actionTaken':editPhone]"/> 

I tried to use remotelink, submitButton, submitToRemote, but no one works. I always get null when I try to parse it in the controller:

 def action= { def actionTaken = params.actionTaken def employeeId= params.employeeId MySession session = MySession.getMySession(request, params.employeeId) profileInstance = session.profileInstance switch(actionTaken) { case "editPhone" : isEditPhone=true break case "editEmail" : isEditEmail=true break } render(view:"profile", model:[profileInstance:session.profileInstance, isEditPhone:isEditPhone, isEditEmail:isEditEmail]) } 

What am I missing? is my params code wrong? Is my parsing params code wrong? it just leads me into circles without progress. Help. thanks.

+6
source share
5 answers

Grails documentation does not list params as one of the attributes accepted by actionSubmit .

You can enter the value you want into your parameter list in the controller using what this tag actually does:

 def editPhone = { forward(action:'action', params:[actionTaken: 'editPhone'])} def editEmail = { forward(action:'action', params:[actionTaken: 'editEmail'])} 

You can also just simply encode completely separate logic in the editPhone and editEmail actions if this makes your code cleaner.

Updated view code:

 <g:actionSubmit action="editPhone" value="${message(code: 'default.button.edit.label', default: 'Edit')}" /> 
+7
source

The params attribute is parsed as a map, where keys are processed by strings, but values ​​are treated as expressions. therefore

 params="['actionTaken':editPhone]" 

tries to define a key called actionTaken to have a value from a variable called editPhone in the GSP model. Since there is no such variable, you get zero. So the fix is ​​moving the quotes:

 params="[actionTaken:'editPhone']" 

which sets the value to the string "editPhone".

+4
source

You can also pass the parameter inside the POST data using

 <input type="hidden" name="actionTaken" value="editPhone" /> 

inside the form. Then it is also available through the params variable. This works for me.

+2
source

I had a similar problem (I needed to send the deletion along with the identifier) ​​and found a solution using the HTML5 formaction attribute for send-input. They can be assigned a value, which may include a controller, action, additional parameters, etc.

In general, to add a parameter to a submit button, such as editing a specific sub-object in a form, will look like this:

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

and in your example:

 <input type="submit" formaction="action?actionTaken=editPhone" value="${message(code: 'default.button.edit.label', default: 'Edit')}" > 
0
source

In my situation, I had a single form element with several actions in each row of the table (i.e. buttons for editing the data table). It was important to submit all the form data so that I couldn’t just use the links. The fastest way I found for parameter input was with javascript. Not perfect, but it works:

 function injectHiddenField(name, value) { var control = $("input[type=hidden][name='" + name + "']"); if (control.size() == 0) { console.log(name + " not found; adding..."); control = $("<input type=\"hidden\" id=\"" + name + "\" name=\"" + name + "\">"); $("form").append(control); } control.val(value); } 

Your button might look like this:

 <g:each in="${objects}" var="object"> ... <g:actionSubmit value="edit" action="anotherAction" onclick="injectHiddenField('myfield', ${object.id})"/> </g:each> 
0
source

Source: https://habr.com/ru/post/920952/


All Articles