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
kobakoba 

連動プルダウンの作成

都道府県と市区の連動プルダウンを作成したいと考えております。

 

都道府県と市区はそれぞれオブジェクトとして持ち、市区は都道府県を参照しています。

 

とあるサイトにactionSupport + reRendersを使用すると実装できると、記載されていたのですが、

具体的な実装方法がわかりませんでした。

 

このほかの実装方法でもかまいませんので、どなたかご教授願います。

 

よろしくお願い致します。

tetsutetsu

こんな感じではいかがでしょうか。

 

Visualforce

<apex:selectList value="{!todouhukenValue}">
  <apex:selectOptions value="{!todouhukenList}" />
  <apex:actionSupport event="onchange" reRender="shiku"/>
</apex:selectList>

<apex:selectList value="{!shikuValue}" id="shiku">
  <apex:selectOptions value="{!shikuList}" />
</apex:selectList>

 

Apex Code

 

public String todouhukenValue {get; set;}

public List<SelectOption> getTodouhukenList(){
  List<SelectOption> todouhukenList = new List<SelectOption>();
  List<todouhuken__c> todouhukenRecords = [select id, name
                         from todouhuken__c];
  
  for(todouhuken__c todouhukenRecord : todouhukenRecords){
    todouhukenList.add(new SelectOption(todouhukenRecord.id, todouhukenRecord.name));
  }

  return todouhukenList;
}

public String shikuValue { get; set; }

public List<SelectOption> getShikuList(){
  List<SelectOption> shikuList = new List<SelectOption>();
  if(todouhukenValue != null){
    List<shiku__c> shikuRecords = [select id, name
                                   from shiku__c
                                   where shiku__c.todouhuken__c = :todouhukenValue];

    for(shiku__c shikuRecord : shikuRecords){
      shikuList.add(new SelectOption(shikuRecord.id, shikuRecord.name));
    }
  }
  return shikuList;
}

 

 ※「todouhuken__c」「shiku__c」「shiku__c.todouhuken__c」は

 お使いの環境のオブジェクト名、項目名に

 書き換えてください。