7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Visualforceのページで selectList の変更に応じて rerender したい時

Last updated at Posted at 2018-10-10

Visualforceのページで、selectListの値の変更に応じて表示させるフォームを変更したい場面があったので
いろいろやってみた話
自分用のメモもかねて記事化

やりたいこと

  • <apex:selectList>の値が変更された時に、フォームを対応するフォームに変更したい

やり方

  • <apex:actionSupport event="onchange" reRender="form"/> 使うと良さそう
  • フォームの項目に必須条件があると rerenderがうまく行かない
  • ので、 <apex:actionRegion>で囲うと部分的にサーバ側に値を送ることができるらしい
    • 簡単に言うとselectlistの部分を囲えば、そこだけ値を送ることができて、うまくrerenderが走る

こんな感じ

<apex:form id="form">
  <apex:pageBlockSection columns="1" title="title">

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="Label 1"/>
      <apex:outputPanel styleClass="requiredInput" layout="block">
        <apex:outputPanel styleClass="requiredBlock" layout="block"/>
        <apex:actionRegion >
          <apex:selectList value="{!hoge}" size="1" multiselect="false">
            <apex:selectOptions value="{!classificationsList}"/>
            <apex:actionSupport event="onchange" reRender="createForm"/>
          </apex:selectList>
        </apex:actionRegion>
      </apex:outputPanel>
    </apex:pageBlockSectionItem>

    <apex:pageBlockSectionItem rendered="{!hoge == 'value1'}">
      <apex:outputLabel value="Label 1" />
      <apex:outputPanel styleClass="requiredInput" layout="block">
        <apex:outputPanel styleClass="requiredBlock" layout="block"/>
        <apex:input value="{!someInput}" type="text" required="true"/> 
      </apex:outputPanel>
    </apex:pageBlockSectionItem>
  <apex:pageBlockSection>
</apex:form>

これで選択リストを変更した時に入力フォームの表示も変更される
あとは、項目とかを適宜追加とかする

参考サイト

とても勉強になる

7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?