1

USING: grails 1.3.7 fyi.

<g:each var="name" in="${nameList}">
 <li>${name.first}</li>
 <input id="Add" type="radio" name="nameMod" value="${name.first}"/>Add
 <input id="Modify" type="radio" name="nameMod" value="${name.first}"/>Modify
 <g:hiddenField name="action" value="${nameMod.id} "/>
</g:each>

So this is what I'm trying to do: I know how to get the value of the radio buttons but, I also need to know which button was pressed. As you can see from above, I tried to set the ID of the radio button to the action (Add/Modify), but I get an error stating that .id is not a valid property. I know how to test for a radio button being selected or not in jQuery, but I'm not sure how to get the value from jQuery to a controller or domain class.

Is there another way of doing this? Basically I need to know the action and the name, so that I can update the database.

Thanks for any advice! (Is it okay for me to say this? edit away!! ;))

1
  • The error is coming from your hiddenField. It is trying to get the id property of nameMod. I think you may want value=${name.id} instead.
    – Weezle
    Jul 20, 2012 at 20:12

5 Answers 5

2

add some class (used only in javaScript) to your radio button, and use it!

<g:each var="name" in="${nameList}">
 <li>${name.first}</li>
 <input id="Add" type="radio" name="nameMod" class="f_radioButtonGroup" value="${name.first}"/>Add
 <input id="Modify" type="radio" name="nameMod" class="f_radioButtonGroup" value="${name.first}"/>Modify
 <g:hiddenField name="action" value="${nameMod.id} "/>
</g:each>

and in javaScript function

function getCheckedItemNumber() {
    var result;
    var radioButtons = $(".f_radioButtonGroup");
    for (var i = 0; i < radioButtons.length; i++) {
        if (radioButtons[i].checked) {
            result = radioButtons[i].value;
        }
    }
    return result
}
1

You could do that with jQuery, but this would be a good case for using a command object:

View:

<g:each var="name" in="${nameList}" status="i">
  <li>${name.first}</li>
  <input type="radio" name="nameAction[${i}]" value="add"/>Add
  <input type="radio" name="nameAction[${i}]" value="modify"/>Modify
  <g:hiddenField name="name[${i}]" value="${name.first} "/>
</g:each>

Command Object:

class NameCommand {
    List<String> nameAction = []
    List<String> name = []
}

Controller:

def theAction = { NameCommand foo ->
    foo.nameAction.eachWithIndex { val, index ->
        if (val == "add") {
            whateverService.add(foo.name[index]) 
        }
        else {
            whateverService.modify(foo.name[index])
        }
    }
}
1

Maybe something like this:

 <g:each var="name" in="${nameList}">
   <li>${name.first}</li>
  <input id="Add" type="radio" name="selectedAction" value="add"/>Add
  <input id="Modify" type="radio" name="selectedAction" value="modify"/>Modify
  <g:hiddenField name="nameMod" value="${name.first} "/>
 </g:each>
1

You are probably going to have to do a little more with the radio button names since you are iterating over an unknown number of names. You could have 100 radio buttons in a group called nameMod - then you could only select one of those 100 radio buttons - which I don't think is what you want.

When I've had things like this I've done them like this:

<g:each var="name" in="${nameList}">
  <li>${name.first}</li>
  <input type="radio" name="nameMod_${name.id}" value="add"/>Add
  <input type="radio" name="nameMod_${name.id}" value="modify"/>Modify
  <g:hiddenField name="name_${name.id}" value="${name.first} "/>
</g:each>

That will give you one radio group per name and allow you to access them in the controller individually.

0

The simple way: just declare a string with name selectedAction (must match with name of radio button in gsp) in your domain class. That's it, GORM will take care of binding process; the value of the selected radio button will be put into selectedAction (domain class field).

You need to do nothing more than that. Whew!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.