0

I am looking to iterate list from java/grails in jquery like below

Controller

session.legendList = legendList // [Soham Shetty] list in controller set to session

GSP

<input type="hidden" name="legendList" id="legendListId" value="${session.legendList}">

jQuery

var filterList= $("#legendListId").val();
// also tried
// var arrLegendList = jQuery.makeArray( filterList );      
for(f in filterList){
     $('#impact-report-user-filter-dropdown').append($('<option>', {
                     value: filterList[f],
                     text:  filterList[f]
     }));
}
$("#impact-report-user-filter-dropdown").multiselect("refresh"); 

Which is not working its giving one char of string as f instead of one element list

1 Answers1

2

Client-side, $("#legendListId").val() returns a string, not an iterable list.

for(f in filterList) {...} will indeed iterate, but over each character of the string, including the square brackets, which is not what you want. See fiddle.

By serving a CSV representation of your list in the <input> element, javascript/jQuery will be able to parse $("#legendListId").val() into an array with String.prototype.split().

Alternatively, pass your Groovy list to javascript directly.

Community
  • 1
  • 1
Roamer-1888
  • 19,138
  • 5
  • 33
  • 44