function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Cato1984Cato1984 

Passing wrapper data as paramater unsing actionfunction

Hey All,

I believe my question will underline my lack of experience with Javascript more than anything else, but I am hoping this is the right place to ask this question anyway. I have a pageBlockTable that is iterating over a list of wrapper classes. Each wrapper class holds two select lists. The idea is to have one control the other. I uderstand how to use action functions to create a dependant picklist when all the requesite functionality resides in the controller itself. I seem to be missing something important when using action functions and specific individual iterations of a list. Can anyone help?

Short question - In the example page, controller and wrapper class below; how can I get the first select list for each iteration in the data table to control the second? The dependant select list outside the data table works as an example of what I am looking for.


<apex:page controller="TestController">

<apex:form id="pgFrm">
  <apex:actionFunction name="refreshDependent" action="{!buildSecondSelectList}" rerender="secList" />
  <apex:pageBlock title="Rerender Dependent Picklists" id="firstlBlk" >
   <apex:selectList value="{!val1}" size="1" onchange="refreshDependent()" id="firstList">
    <apex:selectOptions value="{!list1}"/>
   </apex:selectList>
   <apex:selectList value="{!val2}" size="1" id="secList">
    <apex:selectOptions value="{!list2}"/>
   </apex:selectList>
  </apex:pageBlock>
  <apex:pageBlock title="Test Wrapper Picklists" id="seclBlk" >
   <apex:pageBlockTable value="{!wrapperList}" var="wrapper" id="wrapperTable" >
    <apex:column headerValue="First Wrapper Select List" >
     <apex:selectList value="{!wrapper.wrapperVal1}" size="1" id="wrapFirList">
      <apex:selectOptions value="{!wrapper.wrapperList1}"/>
     </apex:selectList>
    </apex:column>
    <apex:column headerValue="Second Wrapper Select List" >
     <apex:selectList value="{!wrapper.wrapperVal2}" size="1" id="wrapSecList">
      <apex:selectOptions value="{!wrapper.wrapperList2}"/>
     </apex:selectList>
    </apex:column>
   </apex:pageBlockTable>
  </apex:pageBlock>
</apex:form>
</apex:page>


public with sharing class TestController {

public String val1 {get;set;}
public String val2 {get;set;}

public List<SelectOption> list1 {get;set;}
public List<SelectOption> list2 {get;set;}

public List<TestWrapper> wrapperList {get;set;}

public TestController() {
  wrapperList = new List<TestWrapper>();
  for (Integer i = 0; i < 3; i++) {
   wrapperList.add( new TestWrapper() );
  }
  buildFirstSelectList();
  buildSecondSelectList();
}

public void buildFirstSelectList() {
  list1 = new List<SelectOption>();
 
  list1.add( new SelectOption('Val1','Val1') );
  list1.add( new SelectOption('Val2','Val2') );
  list1.add( new SelectOption('Val3','Val3') );
}

public void buildSecondSelectList() {
  list2 = new List<SelectOption>();
  if (val1!=null&&val1=='Val1') {
   list2.add( new SelectOption('SecondVal1','SecondVal1') );
   list2.add( new SelectOption('SecondVal2','SecondVal2') );
   list2.add( new SelectOption('SecondVal3','SecondVal3') );
   list2.add( new SelectOption('SecondVal4','SecondVal4') );
  } else if (val1!=null&&val1=='Val2') {
   list2.add( new SelectOption('SecondVal5','SecondVal5') );
   list2.add( new SelectOption('SecondVal6','SecondVal6') );
   list2.add( new SelectOption('SecondVal7','SecondVal7') );
   list2.add( new SelectOption('SecondVal8','SecondVal8') );
  } else if (val1!=null&&val1=='Val3') {
   list2.add( new SelectOption('SecondVal9','SecondVal9') );
   list2.add( new SelectOption('SecondVal10','SecondVal10') );
   list2.add( new SelectOption('SecondVal11','SecondVal11') );
   list2.add( new SelectOption('SecondVal12','SecondVal12') );
  } else {
   list2.add( new SelectOption('SecondVal1','SecondVal1') );
   list2.add( new SelectOption('SecondVal2','SecondVal2') );
   list2.add( new SelectOption('SecondVal3','SecondVal3') );
   list2.add( new SelectOption('SecondVal4','SecondVal4') );
   list2.add( new SelectOption('SecondVal5','SecondVal5') );
   list2.add( new SelectOption('SecondVal6','SecondVal6') );
   list2.add( new SelectOption('SecondVal7','SecondVal7') );
   list2.add( new SelectOption('SecondVal8','SecondVal8') );
   list2.add( new SelectOption('SecondVal9','SecondVal9') );
   list2.add( new SelectOption('SecondVal10','SecondVal10') );
   list2.add( new SelectOption('SecondVal11','SecondVal11') );
   list2.add( new SelectOption('SecondVal12','SecondVal12') );
  }
}
}




public with sharing class TestWrapper {

public String wrapperVal1 {get;set;}
public String wrapperVal2 {get;set;}

public List<SelectOption> wrapperList1 {get;set;}
public List<SelectOption> wrapperList2 {get;set;}

public TestWrapper() {
  buildFirstSelectList();
  buildSecondSelectList();
}

public void buildFirstSelectList() {
  wrapperList1 = new List<SelectOption>();
 
  wrapperList1.add( new SelectOption('Wrapper Val1','Wrapper Val1') );
  wrapperList1.add( new SelectOption('Wrapper Val2','Wrapper Val2') );
  wrapperList1.add( new SelectOption('Wrapper Val3','Wrapper Val3') );
}

public void buildSecondSelectList() {
  wrapperList2 = new List<SelectOption>();
  if (wrapperVal1!=null&&wrapperVal1=='Wrapper Val1') {
   wrapperList2.add( new SelectOption('Wrapper SecondVal1','Wrapper SecondVal1') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal2','Wrapper SecondVal2') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal3','Wrapper SecondVal3') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal4','Wrapper SecondVal4') );
  } else if (wrapperVal1!=null&&wrapperVal1=='Wrapper Val2') {
   wrapperList2.add( new SelectOption('Wrapper SecondVal5','Wrapper SecondVal5') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal6','Wrapper SecondVal6') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal7','Wrapper SecondVal7') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal8','Wrapper SecondVal8') );
  } else if (wrapperVal1!=null&&wrapperVal1=='Wrapper Val3') {
   wrapperList2.add( new SelectOption('Wrapper SecondVal9','Wrapper SecondVal9') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal10','Wrapper SecondVal10') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal11','Wrapper SecondVal11') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal12','Wrapper SecondVal12') );
  } else {
   wrapperList2.add( new SelectOption('Wrapper SecondVal1','Wrapper SecondVal1') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal2','Wrapper SecondVal2') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal3','Wrapper SecondVal3') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal4','Wrapper SecondVal4') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal5','Wrapper SecondVal5') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal6','Wrapper SecondVal6') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal7','Wrapper SecondVal7') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal8','Wrapper SecondVal8') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal9','Wrapper SecondVal9') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal10','Wrapper SecondVal10') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal11','Wrapper SecondVal11') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal12','Wrapper SecondVal12') );
  }
}
}
Best Answer chosen by Cato1984
Damien Phillippi033905702927186443Damien Phillippi033905702927186443
First of all, I would try creating a Map of your Wrappers along with that list with some sort of Identifier.  After you have this, then add an apex:param to your apex:actionfunction and pass in the identifier to it also.  That way when your actionfunction fires, you have the key to your Map and so can grab the correct Wrapper.

All Answers

Damien Phillippi033905702927186443Damien Phillippi033905702927186443
First of all, I would try creating a Map of your Wrappers along with that list with some sort of Identifier.  After you have this, then add an apex:param to your apex:actionfunction and pass in the identifier to it also.  That way when your actionfunction fires, you have the key to your Map and so can grab the correct Wrapper.
This was selected as the best answer
Cato1984Cato1984
Thanks for the fast reply. I understand what you are saying. I still seem to be missing something. I have little to no experience with Javascript so I am sure I am calling the action function incorrectly. I have changed my code to the following and get a null pointer in the "refreshWrapperDepandantList" method on the "wrapper.buildSecondSelectList" line. When I add a debug statement it prints a blank value for itemNumber. 

Thanks for the help.

<apex:page controller="TestController">

<apex:form id="pgFrm">
  <apex:actionFunction name="refreshDependent" action="{!buildSecondSelectList}" rerender="secList" />
  <apex:actionFunction name="refreshWrapperDependent" action="{!refreshWrapperDependantList}" rerender="wrapSecList" >
   <apex:param name="param1" value="" assignTo="{!itemNumber}" />
  </apex:actionFunction>
  <apex:pageBlock title="Rerender Dependent Picklists" id="firstlBlk" >
   <apex:selectList value="{!val1}" size="1" onchange="refreshDependent()" id="firstList">
    <apex:selectOptions value="{!list1}"/>
   </apex:selectList>
   <apex:selectList value="{!val2}" size="1" id="secList">
    <apex:selectOptions value="{!list2}"/>
   </apex:selectList>
  </apex:pageBlock>
  <apex:pageBlock title="Test Wrapper Picklists" id="seclBlk" >
   <apex:pageBlockTable value="{!wrapperList}" var="wrapper" id="wrapperTable" >
    <apex:column headerValue="First Wrapper Select List" >
     <apex:selectList value="{!wrapper.wrapperVal1}" size="1" onchange="refreshWrapperDependent(this.identifier)" id="wrapFirList">
      <apex:selectOptions value="{!wrapper.wrapperList1}"/>
     </apex:selectList>
    </apex:column>
    <apex:column headerValue="Second Wrapper Select List" >
     <apex:selectList value="{!wrapper.wrapperVal2}" size="1" id="wrapSecList">
      <apex:selectOptions value="{!wrapper.wrapperList2}"/>
     </apex:selectList>
    </apex:column>
    <apex:inputHidden value="{!wrapper.identifier}" />
   </apex:pageBlockTable>
  </apex:pageBlock>
</apex:form>
</apex:page>



public with sharing class TestController {

public String val1 {get;set;}
public String val2 {get;set;}

public String itemNumber {get;set;}

public List<SelectOption> list1 {get;set;}
public List<SelectOption> list2 {get;set;}

public Map<String, TestWrapper> wrapperMap {get;set;}
public List<TestWrapper> wrapperList {
  get {
   return (wrapperMap!=null) ? wrapperMap.values() : null;
  } set;
}

public TestController() {
  wrapperMap = new Map<String, TestWrapper>();
  for (Integer i = 0; i < 3; i++) {
   wrapperMap.put( string.valueOf(wrapperMap.size()), new TestWrapper(wrapperMap.size()) );
  }
  buildFirstSelectList();
  buildSecondSelectList();
}

public void buildFirstSelectList() {
  list1 = new List<SelectOption>();
 
  list1.add( new SelectOption('Val1','Val1') );
  list1.add( new SelectOption('Val2','Val2') );
  list1.add( new SelectOption('Val3','Val3') );
}

public void buildSecondSelectList() {
  list2 = new List<SelectOption>();
  if (val1!=null&&val1=='Val1') {
   list2.add( new SelectOption('SecondVal1','SecondVal1') );
   list2.add( new SelectOption('SecondVal2','SecondVal2') );
   list2.add( new SelectOption('SecondVal3','SecondVal3') );
   list2.add( new SelectOption('SecondVal4','SecondVal4') );
  } else if (val1!=null&&val1=='Val2') {
   list2.add( new SelectOption('SecondVal5','SecondVal5') );
   list2.add( new SelectOption('SecondVal6','SecondVal6') );
   list2.add( new SelectOption('SecondVal7','SecondVal7') );
   list2.add( new SelectOption('SecondVal8','SecondVal8') );
  } else if (val1!=null&&val1=='Val3') {
   list2.add( new SelectOption('SecondVal9','SecondVal9') );
   list2.add( new SelectOption('SecondVal10','SecondVal10') );
   list2.add( new SelectOption('SecondVal11','SecondVal11') );
   list2.add( new SelectOption('SecondVal12','SecondVal12') );
  } else {
   list2.add( new SelectOption('SecondVal1','SecondVal1') );
   list2.add( new SelectOption('SecondVal2','SecondVal2') );
   list2.add( new SelectOption('SecondVal3','SecondVal3') );
   list2.add( new SelectOption('SecondVal4','SecondVal4') );
   list2.add( new SelectOption('SecondVal5','SecondVal5') );
   list2.add( new SelectOption('SecondVal6','SecondVal6') );
   list2.add( new SelectOption('SecondVal7','SecondVal7') );
   list2.add( new SelectOption('SecondVal8','SecondVal8') );
   list2.add( new SelectOption('SecondVal9','SecondVal9') );
   list2.add( new SelectOption('SecondVal10','SecondVal10') );
   list2.add( new SelectOption('SecondVal11','SecondVal11') );
   list2.add( new SelectOption('SecondVal12','SecondVal12') );
  }
}

public void refreshWrapperDependantList() {
  system.debug('############### itemNumber: '+itemNumber);
  TestWrapper wrapper = wrapperMap.get( itemNumber );
  wrapper.buildSecondSelectList();
}
}





public with sharing class TestWrapper {

public String identifier {get;set;}

public String wrapperVal1 {get;set;}
public String wrapperVal2 {get;set;}

public List<SelectOption> wrapperList1 {get;set;}
public List<SelectOption> wrapperList2 {get;set;}

public TestWrapper(Integer i) {
  this.identifier = string.valueOf(i);
  buildFirstSelectList();
  buildSecondSelectList();
}

public void buildFirstSelectList() {
  wrapperList1 = new List<SelectOption>();
 
  wrapperList1.add( new SelectOption('Wrapper Val1','Wrapper Val1') );
  wrapperList1.add( new SelectOption('Wrapper Val2','Wrapper Val2') );
  wrapperList1.add( new SelectOption('Wrapper Val3','Wrapper Val3') );
}

public void buildSecondSelectList() {
  wrapperList2 = new List<SelectOption>();
  if (wrapperVal1!=null&&wrapperVal1=='Wrapper Val1') {
   wrapperList2.add( new SelectOption('Wrapper SecondVal1','Wrapper SecondVal1') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal2','Wrapper SecondVal2') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal3','Wrapper SecondVal3') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal4','Wrapper SecondVal4') );
  } else if (wrapperVal1!=null&&wrapperVal1=='Wrapper Val2') {
   wrapperList2.add( new SelectOption('Wrapper SecondVal5','Wrapper SecondVal5') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal6','Wrapper SecondVal6') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal7','Wrapper SecondVal7') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal8','Wrapper SecondVal8') );
  } else if (wrapperVal1!=null&&wrapperVal1=='Wrapper Val3') {
   wrapperList2.add( new SelectOption('Wrapper SecondVal9','Wrapper SecondVal9') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal10','Wrapper SecondVal10') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal11','Wrapper SecondVal11') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal12','Wrapper SecondVal12') );
  } else {
   wrapperList2.add( new SelectOption('Wrapper SecondVal1','Wrapper SecondVal1') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal2','Wrapper SecondVal2') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal3','Wrapper SecondVal3') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal4','Wrapper SecondVal4') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal5','Wrapper SecondVal5') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal6','Wrapper SecondVal6') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal7','Wrapper SecondVal7') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal8','Wrapper SecondVal8') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal9','Wrapper SecondVal9') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal10','Wrapper SecondVal10') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal11','Wrapper SecondVal11') );
   wrapperList2.add( new SelectOption('Wrapper SecondVal12','Wrapper SecondVal12') );
  }
}
}
Damien Phillippi033905702927186443Damien Phillippi033905702927186443
Change 'this.identifier' to '{!iwrapper.dentifier}'
Cato1984Cato1984
Thanks for all the help. It's working!