Passing Apex inner class List to JavaScript

Declare this in the script

  1. var JSList = new Array();
  2. function JSObj() {
  3. this.recId= '';
  4. this.Name= '';
  5. }


In Controller


  1. public List<innerClassObj> innerClassObjList {get;set;}
  2. public class innerClassObj{
  3. public String InnerId {get; private set;}
  4. public String InnerName {get; private set;}
  5. }


In VF section of the page let JSList refresh when ever innerClassObjList updates.

  1. <apex:repeat value="{!innerClassObjList}" var="innObj">
  2.  <script>
  3. var JSListObj = new JSObj();
  4. JSListObj.recId = '{!innObj.InnerId}';
  5. JSListObj.Name= '{!innObj.InnerName}';
  6. JSList.push(JSListObj);
  7.  </script>
  8. </apex:repeat>

Or the other and more suitable solution for many perspectives could be passing the inner class list by Serializing it with JSON.serialize()


Comments

Popular Posts