How To Use Grails ${createlink} In Javascript
Solution 1:
A better way of doing this which doesn't require the JavaScript code be in your GSP would be the following:
<button class="submit_small" onClick="GetSelectedItem();" data-url="${createLink(controller:'country', action: 'wholeTestUnits')}">
<span><g:messagecode="default.button.submit.label" /></span>
</button>
functionGetSelectedItem() {
var button = event.target;
var e = document.getElementById("country");
var strSel = e.options[e.selectedIndex].value;
var url = button.getAttribute("data-url") + "/" + strSel;
}
Solution 2:
I think you have a serverside/clientside problem. The createLink is run on the server, the JS is run on the client...
Try:
var url = '${createLink(controller:'country', action: 'wholeTestUnits')}' + strSel ;
Solution 3:
As I think , you are not getting value of strSel in your link. You can try this.
function GetSelectedItem()
{
var e = document.getElementById("country");
var strSel = e.options[e.selectedIndex].value;
alert(strSel);
var url = "${grailsApplication.config.grails.serverURL}/country/wholeTestUnits/" + strSel
alert(url);
}
Solution 4:
Instead of using the createLink you could build your own URL and use that one instead. You have to pay attention to capital letters in the controller name, though.
var url="${ createLink(controller:'testcontroller', action:'getData') }";
is equivalent to
var url = "/testcontroller/getData;
If you want to pass in arguments from javascript to the controller you can do like this.
var url = "/testcontroller/getData?arg0=" + arg0 + "&arg1=" + arg1;
To extract the arguments in the controller you do use the params keyword. So to print the parameters in the controller you do this:
println params.arg0
println params.arg1
Solution 5:
--in gsp file--
<ahref="#"onclick="callAjax('${createLink(controller:'shift',action: 'addShift')}');" >Add/Edit Shift</a>
--in js file--
functioncallAjax(path){
//path->/shift/addShiftvar xmlhttp = newXMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("updateContent").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", path, true);
xmlhttp.send();
}
Post a Comment for "How To Use Grails ${createlink} In Javascript"