+ Start a Discussion
robertcw777robertcw777 

Dependent SelectLists in PageBlockTable

I’m trying to create a Country selectList within a table row based on the Region selected in another selectList in that row. The Country selectList should be updated based on the region selected.  My Country object is a child of Region object.

 

I’m getting some strange results. For example, when I select a Region in a record, the Country list is sometimes set to “All” with no Country values in the dropdown. Sometimes, it does work. And, sometimes it gets the wrong Countries for the Region. Can anybody tell me what’s wrong with what I have below? Thanks.

 

<apex:page controller="CompetencyOwnerController"> 

  <apex:form >

     <apex:pageblock id="CompetencyOwner">

       <apex:pageblocktable value="{!CompetencyOwners}"  var="c">

          <apex:column headerValue="Region">

            <apex:selectList value="{!regionValue}"  size="1"> 

                <apex:selectOptions value="{!RegionChoices}"/>

                <apex:actionSupport event="onchange" rerender="countryList"/>

            </apex:selectList>

          </apex:column>

          <apex:column headerValue="Country">

            <apex:selectList id= "countryList" value="{!countryValue}"  size="1" style="width:300px"> 

                <apex:selectOptions value="{!CountryChoices}"/>

                <apex:actionSupport event="onchange"/>

            </apex:selectList>

          </apex:column>

       </apex:pageblocktable>

    </apex:pageblock>

    </apex:form>

</apex:page>

 

public class CompetencyOwnerController {

       

    Public List <CompetencyOwner__c> compOwnerData{get;set;}

    Public String regionValue{get;set;}

    Public String countryValue{get;set;}

    Public String stateProvinceValue{get;set;}

   

String[] Regions=new String[] {};

 

    public List <CompetencyOwner__c> getCompetencyOwners() {

      compOwnerData = [Select Name from CompetencyOwner__c];

      return compOwnerData;

    }

 

Controller:

 

public List<SelectOption> getRegionChoices() {

   List<SelectOption> regionOptions = new List<SelectOption>();

   List <Region__c> regions=[Select ID, Name from Region__c];

     regionOptions.add(new SelectOption('All',''));

    for(Region__c r:regions) {

       regionOptions.add(new SelectOption(r.ID,r.Name));

    }

   System.debug(LoggingLevel.Info,'+++++++++ value after region init '+regionValue);

   return regionoptions;

} /* end getRegionChoices */

                               

 public List<SelectOption> getCountryChoices() {

   List<SelectOption> countryOptions = new List<SelectOption>();

     If(null!=regionValue) {

        System.debug(LoggingLevel.Info,'???????????? '+regionValue);

        List <Country__c> countries=[Select ID,Name from Country__c where Region__c=:regionValue];        

        countryOptions.add(new SelectOption('All','All'));

        for(Country__c co:countries) {

           countryOptions.add(new SelectOption(co.ID,co.Name));

        }

     }

      return countryOptions;

} /* end getRegionChoices */

 

}

ShahTheTrainerShahTheTrainer

Hi,

 

in getCountryChoices(), in the SOQL query,   replace Region__c =: regionValue [lookup field] with Region__r.Name (or) Region__r.Id [i.e. Relationship]

just for your information,  __c stands for custom,,   __r stands for relationship..

 

try the modified code below, and revert back..

public List<SelectOption> getCountryChoices() {
   List<SelectOption> countryOptions = new List<SelectOption>();
     If(null!=regionValue) {
        System.debug(LoggingLevel.Info,'???????????? '+regionValue);
        List <Country__c> countries=[Select ID,Name from Country__c where Region__r.Id=:regionValue];        
        countryOptions.add(new SelectOption('All','All'));
        for(Country__c co:countries) {
           countryOptions.add(new SelectOption(co.ID,co.Name));
        }
     }
      return countryOptions;
} /* end getRegionChoices */

 use Region__r.Name by changing the same in 

 

public List<SelectOption> getRegionChoices() {

   List<SelectOption> regionOptions = new List<SelectOption>();

   List <Region__c> regions=[Select ID, Name from Region__c];

     regionOptions.add(new SelectOption('All',''));

    for(Region__c r:regions) {

       regionOptions.add(new SelectOption(r.ID,r.Name));

    }

   System.debug(LoggingLevel.Info,'+++++++++ value after region init '+regionValue);

   return regionoptions;

} /* end getRegionChoices */

 

 

----  Good luk