开发者

how to read selected radio button value in Grails?

开发者 https://www.devze.com 2023-03-16 06:25 出处:网络
I\'m having a GSP page like below. The requirement is like a list of reports will be shown - the user has the option to select one report and can export the report to excel.

I'm having a GSP page like below. The requirement is like a list of reports will be shown - the user has the option to select one report and can export the report to excel.

How to read the selected radio button and pass the selected value as "params" ?

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="main" />
</head>
<body>
    <div class="nav">
        <span class="menuButton"><g:link class="create"
                action="excelExport" params="['id':{ radId.value}]">Export To Excel</g:link>
        </span>
    </div>
    <div class="body">
        <div class="message">Select the report and click 'Excel Export'</div>
    </di开发者_开发技巧v>
    <g:form method="post">
        <g:render template="displayUploadedReportsTemplate"
            model="['uploadedReports':uploadedReports]" />
    </g:form>

</body>
</html>

where displayUploadedReportsTemplate is:

<tbody>
            <g:each in="${uploadedReports}" var="bbkRat">
                <tr>
                    <td valign="top"><g:radio name="radId"
                            value="${fieldValue(bean:bbkRat,field:'id')}" /></td>
                    <td valign="top"><label> ${fieldValue(bean:bbkRat,field:'cmpName')}
                    </label></td>
                    <td valign="top"><label> ${fieldValue(bean:bbkRat,field:'reportCreationDate')}
                    </label></td>

                    <%--<td valign="top">
                    <label> ${fieldValue(bean:bbkRat,field:'cmpName')}
                </label> 
                </td>

                --%>
                <tr>
            </g:each>

        </tbody>

How should the params value be below??

<g:link class="create"
                action="excelExport" params="['id':{ radId.value}]">


i would recommand to use a radio button group. instead of using g:readio tag you can replace it with plain html input tag within you each tag, e.g.

<input type="radio" name="myGroup" value="1" checked="checked" />
<input type="radio" name="myGroup" value="2" />
<input type="radio" name="myGroup" value="3" />

you have already defined a form around your displayUploadedReportsTemplate. so you need to add a submit button to this form and a action where the params should be tramsitted, e.g.

<g:form method="post" action="test">

within test action you can print your params.myGroup and you will recieve to selected report.


<g:link is processed on server-side, so you can't use it on client side, you have to use javascript instead.

It would be like:

<a href="#" class="excelExport" onclick="doExport(); return false">
<script type="text/javascript">
function doExport() {
  var id= $('input:radio[name=radId]:checked').val();
  window.location = '${g.createLink(action:'excelReport')}?id=' + id;
}
</script>

ps I assume that you are using jQuery

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号