问题
I have a list of items and user of this application may select as many items he wish. Now, I want to download the details of each selected items. I have method:
getSelectedItems()
which returns the ids of each selected items comma separated such as if user selects 2,4,6,7 item then it will return 2,4,6,7,
Now, I want to pass this string as params to controller. I am using
<g:link>
tag such as:
<g:link controller="items" action="downloadSelecteditems" params="${getSelectedItemss}" class="btn btn-xs ">
Now, How I can send all the selected items that I will get from above method as params in this tag ?
Thanks for you time, consideration and guidance :-)
回答1:
Try using: Suppose the getSelectedItem holds a list and you pass the list from the controller :
ItemsController{
renderSelectedList(){
.....
selectedList = callyourservice.getSelectedItems()
render[view:'yourviewshowingtheselectedlist' model:[selectedList :selectedList ]]
}
}
In yourviewshowingtheselectedlist gsp:
<g:set value="${getSelectedItemss}" var="selectedItems"/>
<g:link controller="items" action="downloadSelecteditems"
params="[selectedItems:selectedItems]" class="btn btn-xs ">
In your controller :
ItemsController{
.......
def downloadSelecteditems(){
println params.selectedItems //will be visible
}
}
Hope it helps.. Regards
回答2:
An alternative solution has been proposed for this problem. Actually our JavaScript renders at browser end and our tags such as renders at server end that's why we can't invoke JavaScript method in tags.
As I wished to pass the ids of multiple selected selected items (that I get by calling the JavaScript method getSelectedItems which returns me the comma separated IDs of each selected item), I used the tag instead because tag use this tag at back end too. This is how I used this tag:
<a id="linkURL" href ="${createLink(controller: 'Controller', action: 'actionMethid')}" onclick="downloadSelectedItems()">
And In JavaScript I handled onClick event where I append the Comma Separated IDs of seleceted Items (returned by getSelectedItems JavaScript method) with the URL such as:
function downloadSelectedItems() {
var selectedSessions = getSelectedItems();
var link = document.getElementById("linkURL").href;
alert(link);
document.getElementById("linkURL").href=link+"?selectedSessions="+selectedSessions;
link = document.getElementById("linkURL").href;
alert(link);
}
Thus I was able to get the selected items on the Controller Side :-)
来源:https://stackoverflow.com/questions/24931996/sending-multiple-selected-items-from-controller-to-view