0

So I have a Page block table which creates multiple output text fields

<apex:pageBlockTable title="Country" value="{!displayCountries}" var="dc" align="center">
     <apex:column headerValue="Description">
              <apex:outputText value="{!dc.name}"/>
     </apex:column>   
     <apex:column headerValue="Country Code">
        <apex:selectList value="{!codeSelected}" multiselect="false" size="1">
                    <apex:selectOptions value="{!countryCode}"/>
                    <apex:actionSupport event="onchange" action="{!updateCountries}" rerender="CId"/>
                </apex:selectList>
            </apex:column>    
     <apex:column headerValue="Countries">
              <apex:outputText value="{!country}" id="CId">
              </apex:outputText>
     </apex:column>

Controller code

public String country{set;get;};
public String countryCode {set; get;}

public void updateCountries(){
  if(countryCode == 'US'){  
     country = 'USA';
  }else if(countryCode == 'JP'){
     country = 'Japan'; 
  }

/* Some more code */

But the problem is everytime the same value gets populated in each of the output text fields created irrespective of the value selected from the picklist. The problem is I suppose, that the id and value variable is same for each of the output text created from the loop.

Expected outcome:

For each row of the table there would a picklist value set and an output text field. Based on the value selected in the picklist set, the corresponding value should be displayed in the output text.

1
  • You would need to put these properties and methods in a wrapper class
    – Eric
    Commented Apr 10, 2017 at 19:22

1 Answer 1

1

You are using properties public String country{set;get;}; public String countryCode {set; get;} so when updateCountries method changes Country its rendered everywhere on vf with new value.

Try following code its working.

Visualforce Page

<apex:page controller="picklistBasedOutput" action="{!init}">
<apex:form>
<apex:pageBlock>
<apex:pageBlockTable title="Country" value="{!displayCountries}" var="dc" align="center">
     <apex:column headerValue="Description">
              <apex:outputText value="{!dc.name}"/>
     </apex:column>   
     <apex:column headerValue="Country Code">
        <apex:selectList value="{!dc.CodeSelected}" multiselect="false" size="1">
                    <apex:selectOptions value="{!CountryCode}"/>
                    <apex:actionSupport event="onchange" action="{!updateCountries}" rerender="CId"/>
                </apex:selectList>
            </apex:column>    
     <apex:column headerValue="Countries">
              <apex:outputText value="{!dc.country}" id="CId">
              </apex:outputText>
     </apex:column>
     </apex:pageBlockTable >
     </apex:pageBlock>
     </apex:form>
</apex:page>

Controller

public class picklistBasedOutput{
       public List<Wrapper>  displayCountries {get; set;}

    public void init() {
       List<Wrapper> wpList = new List<Wrapper>();
       for(integer i = 0 ; i < 3; i++)
       {
           Wrapper w = new Wrapper();
           wpList.add(w);
       }
       displayCountries = wpList;

    }
    public void updateCountries(){
   for(Wrapper w : displayCountries)
   {
       if(w.CodeSelected == 'US'){  
     w.country = 'USA';
     }else if(w.CodeSelected == 'JP'){
     w.Country = 'Japan'; 
    }

   }

    }

    public class Wrapper{
    public string name{get; set;}
    public String CodeSelected {get; set;}
    public String Country{get; set;}
    }

    public List<SelectOption> getcountryCode()
    {
        List<SelectOption> countryCodeList = new List<SelectOption>();
        countryCodeList.add(new SelectOption('', 'None'));
        countryCodeList.add(new SelectOption('US', 'US'));
        countryCodeList.add(new SelectOption('JP', 'JP'));
        return countryCodeList;


    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .