user3363670
user3363670

Reputation: 37

How to create a dynamic radio button in grails controller

Currently I am working with grails and MySQL.

I have to create a dynamic radio button in the controller and pass to the view. How can I do this?

For example: radio button with yes or no and male or female

These values are now in the database. Now my problem is how can I create the radio button in the controller and pass it to the grails view?

List queryData
String s="";
for(int i=0;i<eventformAttri.size();i++){
    queryData=formField.masterTableQuery(eventformAttri[i].fieldQuery.toString()) //to fetch the radio buttons values
    for(int ii=0;ii<queryData.size();ii++){
        System.out.println("list data "+queryData[ii].field1)
        s+=queryData[ii].field1+" <input type='radio' name='myGroup"+i+"' value="+queryData[ii].field2+" />";
    }
    s+=",";
}
def vformData=s.split(',');
render(view:'/auditor/formView',model:[eventformAttri:eventformAttri,queryData:vformData])

Upvotes: 1

Views: 1409

Answers (2)

aldrin
aldrin

Reputation: 4572

Elaborating what Phat H. VU is saying, through the controller, send queryData as is,

render(view:'/auditor/formView',model:[queryData:queryData])

and use the view to render the info,

<g:radioGroup name="dynaRad"
              labels="${queryData.collect{it.field1}}"
              values="${queryData.collect{it.field2}}">
    <p>${it.label} ${it.radio}</p>
</g:radioGroup>

Upvotes: 1

ludo_rj
ludo_rj

Reputation: 3954

You should create your radio button in your view, and use the embedded jquery library to do that. For instance, combine your jquery script in the view with a remotelink to interact with your controller:

$('input:radio[title=YOUR TITLE]').attr('checked',true);


    <g:remoteLink controller="yourcontroller" action="youraction" update="[success: 'results']" onSuccess="yourFunction(data) ">Your link</g:remoteLink>

Upvotes: 1

Related Questions