Reputation: 397
I'm sending string array to my controller that contains array of Ids in it.
function submit(){
var ids = [];
bootbox.confirm("Are you sure to send data?", function(confirmed){
if(confirmed){
$('input[id^="tdCheckbox_"]').each(
function () {
var $this = $(this);
if($this.is(":checked")){
ids.push($this.attr("id").replace("tdCheckbox_",""));
}
}
);
$("#Ids").val(ids);
$("#submitForm").submit();
}
});
}
<g:formRemote name="submitForm" url="[controller:'myController', action:'submit']" onSuccess="removeIds(data)">
<g:hiddenField name="Ids" />
</g:formRemote>
CONTROLLER:
def submit(){
def result = [success:false]
if(params?.Ids){
String[] ids = params?.Ids
ids.eachWithIndex{ it, int i ->
//HERE OUTPUT IS LIKE
//4
//,
//5
//,
//6
println(it.value)
}
result["id"] = params?.Ids
}
render result as JSON
}
In eachWithIndex loop i'm getting output with , (comma) that I do not require, I think there must be a good option to loop through it.
Please suggest the same.
Upvotes: 0
Views: 611
Reputation: 28564
problem that you submitting from javascript one string value (ids delimited with coma)
ids=1,2,33
and on the level of groovy/grails the params?.Ids
returns you just a String like this: "1,2,33"
and assigning a String
to a String[]
just splits it by chars...
as workaround in groovy you can use params?.Ids?.split(',')
String[] ids = "1,2,33".split(',')
or submit multiple values form javascript like this:
ids=1 & ids=2 & ids=33
in this case grails will return you an array for params?.Ids
expression if more then one value submitted with the same name
Upvotes: 1