Controller class: public class YourController { public Boolean showForm { get; set; } public String yourValue { get; set; } public YourController() { showForm = false; } // Server-side method to toggle form visibility public PageReference processFormVisibility() { showForm = isFormVisible; return null; } } The above sample code creates a page with a hidden form. Clicking the \"Toggle Form\" button makes the form appear or disappear. The \"Your Value\" field is for input, which you can customize for your specific use.  Related: https://salesforce.stackexchange.com/questions/327394/how-to-show-hide-a-visualforce-component-using-the-rendered-attribute   https://www.forcetalks.com/salesforce-topic/can-we-hide-particular-portion-of-visualpage-on-click-of-button-using-render-and-rerender/   If this information helps, please mark the answer as best. Thank you  ", "upvoteCount": 0, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T4OV1SAN", "datePublished": "2023-10-11T08:59:13.000Z", "author": { "@type": "Person", "name": "Ashwini Garwandha", "url": "https://trailblazers.salesforce.com/profileView?u=0054S000002svOUQAY", "affiliation": { "@type": "Organization", "name": "Salesforce" } } } ] } } Skip to main content
初心者です。

VisualforceとJavascriptでの値の参照方法について質問させてください。

Javascriptの変数の値(true,false)でVisualforceのapex:formの表示非表示を行いたいです。

renderedで制御したいのですが、Javascriptの変数の値はどのようにして記載すればよろしいのでしょうか?

{!XXX}だとコントローラを探しに行ってしまいます。

もし上記が不可能な場合、Apexにパラメータを渡して、Apexで処理をしてbooleanを返してもらう手段を取ろうと思っているのですが、Apexへのパラメータの渡し方と受け取り方がわかりません。

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

ありがとう。
1 Antwort
  1. 11. Okt. 2023, 08:59
    Hi Lelouch,

    For showing or hiding Visualforce apex:form based on a JavaScript variable and  passing parameters to an Apex method, You can refer below sample code and make adjustments as per your requirement.

    Vfpage code:

    <apex:page controller="YourController">

    <apex:form>

    <script>

    var isFormVisible = false;

      function toggleFormVisibility() {

    isFormVisible = !isFormVisible;

    processParameter(isFormVisible);

    }

    </script>

    <input type="button" value="Toggle Form" onclick="toggleFormVisibility();" />

    <apex:outputPanel id="formBlock">

    <apex:form rendered="{!showForm}">

    <!-- Form fields -->

    <apex:inputText value="{!yourValue}" label="Your Value" />

    <apex:commandButton value="Submit" />

    </apex:form>

    </apex:outputPanel>

    <apex:actionFunction name="processParameter" action="{!processFormVisibility}"

    rerender="formBlock">

    <apex:param name="paramValue" value="" assignTo="{!isFormVisible}" />

    </apex:actionFunction>

    </apex:form>

    </apex:page>

    Controller class:

    public class YourController {

    public Boolean showForm { get; set; }

    public String yourValue { get; set; }

    public YourController() {

    showForm = false;

    }

    // Server-side method to toggle form visibility

    public PageReference processFormVisibility() {

    showForm = isFormVisible;

    return null;

    }

    }

    The above sample code creates a page with a hidden form. Clicking the "Toggle Form" button makes the form appear or disappear. The "Your Value" field is for input, which you can customize for your specific use. 

    Related:https://salesforce.stackexchange.com/questions/327394/how-to-show-hide-a-visualforce-component-using-the-rendered-attribute 

    https://www.forcetalks.com/salesforce-topic/can-we-hide-particular-portion-of-visualpage-on-click-of-button-using-render-and-rerender/ 

    If this information helps, please mark the answer as best. Thank you