I am looking to pass a javascript variable in the id field of a g:link gsp tag but don't know how to do it. If I am going about this the wrong way let me know.
Here is what I have:
- a table which each row has the id I want to pass in the g:link
- javascript that onClick of the table row sets a javascript variable with the value of the row id
<r:script disposition="head">
var data = -1;
$(document).ready(function () {
$('#${tableId} tr').click(function (event) {
data = $(this).attr('id');
});
});
</r:script>
- then i want to pass the javascript
data
variable to a g:link
<li><g:link controller="filterIntraday" action="show" id="??????">View</g:link></li>
I can't get this to work. How do I set the javascript variable in the gsp tag. Or what is another way to go about passing a client side variable in the g:link tag ?
Thanks.
I am looking to pass a javascript variable in the id field of a g:link gsp tag but don't know how to do it. If I am going about this the wrong way let me know.
Here is what I have:
- a table which each row has the id I want to pass in the g:link
- javascript that onClick of the table row sets a javascript variable with the value of the row id
<r:script disposition="head">
var data = -1;
$(document).ready(function () {
$('#${tableId} tr').click(function (event) {
data = $(this).attr('id');
});
});
</r:script>
- then i want to pass the javascript
data
variable to a g:link
<li><g:link controller="filterIntraday" action="show" id="??????">View</g:link></li>
I can't get this to work. How do I set the javascript variable in the gsp tag. Or what is another way to go about passing a client side variable in the g:link tag ?
Thanks.
Share Improve this question edited Apr 13, 2013 at 0:34 phil.e.b asked Apr 12, 2013 at 23:38 phil.e.bphil.e.b 2,6212 gold badges17 silver badges18 bronze badges2 Answers
Reset to default 3How about changing "href" attribute inside event function:
function (event) {
data = $(this).attr('id');
$("link-selector").attr("href", "${createLink(controller: 'filterIntraday', action: 'show')}/" + data);
}
You cannot set the id in the gsp tag because the tag is already processed. What you can do is change the href
of the generated html. To do that you need to assign an id to the <a>
element.
<li><g:link controller="filterIntraday" action="show" elementId="linkToFilter">View</g:link></li>
This will create something like:
<li>a<a href='yourapp/filterIntraday/show' id="linkToFilter" /></li>
Then in your javascript you can change the href:
data = $(this).attr('id');
$link = $('#linkToFilter');
$link.attr('href', $link.attr('href')+'/'+data);