Question

I am designing a simple agenda functionality that can select a suitable option from a list of dates and times (instances from an Agenda domain class. Am trying to return the selected ID to the controller with a parameter, the id-value is derived from a javascript function. Unfortunately, it does not work.

My view looks like this:

[...]
<g:form name="agendaForm" url="[resource:aanvraagInstance, action:'showAgenda']" >
<table class="table">
<g:each in="${agendaInstanceList}" status="i" var="agendaItem">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}"><td><input type="radio" name="agendaGroup" value="${agendaItem?.id}"/>
<g:formatDate date="${agendaItem?.startDate}"  type="date" style="LONG"/>
from <g:formatDate date="${agendaItem?.startDate}"  type="time" style="SHORT"/>
to <g:formatDate date="${agendaItem?.endDate}"  type="time" style="SHORT"/></td></tr>
</g:each>
</table>
<fieldset class="buttons">
<g:link name="save" class="btn btn-default" params="[agendaID: getGeselectedID()]" action="prikDatum" id="${aanvraagInstance?.id}">Indienen link</g:link>
</fieldset>
</g:form>
    </div>
</div>
</div>
<g:javascript>
    function getGeselectedID(){
    return getCheckedValue(document.forms['agendaForm'].elements['agendaGroup']);
    }

    function getCheckedValue(radioObj) {
    if(!radioObj)
        return "";
    var radioLength = radioObj.length;
    if(radioLength == undefined)
        if(radioObj.checked)
            return radioObj.value;
        else
            return "";
    for(var i = 0; i < radioLength; i++) {
        if(radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
    }
</g:javascript>

My controller method is:

def prikDatum(Aanvraag aanvraagInstance){

    def agenda = Agenda.find("from Agenda as ag where ag.id=${params.agendaID}")
    // rest of the code
    render view:'editContacts' , model: [aanvraagInstance: aanvraagInstance]

} 

How can I send the selected date to the controller?

Was it helpful?

Solution

getGeselectedID() is a JavaScript function, so you don't have access of it in GSP tags (JavaScript is executed in the browser and GSP tags are parsed in the server side).

If you just want to submit a form with the id selected, you're already doing it, see the <input type="radio" />.

The value will be available thought the name attribute, in this case agendaGroup.

def prikDatum(Aanvraag aanvraagInstance){
  println params.agendaGroup
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top