1

Trying to increase my VF skills. What I would like to do it is have a table of related records that only displays if the user clicks a button/checkbox.

When I select the checkbox check the debug log I can that the value is changing to true and false. But the section is not displaying on the page.

I went through Bob Buzzards blog about conditional rendering. And I put the data table into a container but still nothing changes on selecting the checkbox. At this point I think I am just overcomplicating my controller and my page trying to make this work.

Here is the page section

 <apex:form >
    <apex:inputCheckbox value="{!showNat4}" >Related Campaigns
          <apex:actionSupport event="onchange"  action="{!displayNat4}" reRender="Nat4Container"/>
      </apex:inputCheckbox>
</apex:form>
 <apex:outputPanel id="nat4Container">
     <apex:outputPanel   id="Nat4Table" rendered="{!showNat4}">
          <apex:dataTable value="{!national4Campaigns}" var="campaign">
              <apex:column headerValue="Name">
                 <apex:outputField value="{!campaign.Name}"/>
               </apex:column>
           </apex:dataTable>
  </apex:outputPanel>
 </apex:outputPanel>

The Controller

public with sharing class BenchmarkController {

    public transient BenchMarks__c benchMarks {get; set;}   
    public Campaign_BenchMarks__mdt national4 {get; set;}       

    public Boolean showNat4 {get; set;} 

public ApexPages.StandardSetController national4SetController{
        get{
            if (national4SetController == null ) {
                national4SetController = new ApexPages.StandardSetController(Database.getQueryLocator(
                [SELECT Name, Total_Sent__c, Open_Rate__c, Delivery_Rate__c, Opt_Out_Rate__c, Unique_Click_Through_Rate__c FROM Campaign]
                ));
            }
            return national4SetController;
        }
        set;
    }



    public BenchmarkController(){
        benchMarks = BenchmarksSelector.getBenchMarks();        
        national4 = BenchmarksSelector.getCampaignBenchMarks('National_Category4');


        showNat4 = false;

    }   

    public PageReference displayNat4(){
        if (showNat4 ) {
            showNat4 = false;
            System.debug(showNat4);
        }else {
            showNat4 = true;
            System.debug(showNat4);
        }
        return null;
    }

}
6
  • 1
    what happens if you match the case of the panel ID? nat4Container instead of Nat4Container in the reRender? Nov 1 '19 at 13:57
  • That was a great catch and I am embarrassed to have missed that. It works now. But the interesting part is the component rendered after I check and then uncheck the box. It does not render when I check it the first time. Nov 1 '19 at 14:04
  • 1
    Does your debug log show it going into the displayNat4() on the first time you click the check? Nov 1 '19 at 14:08
  • It is but checking the box is returning a value of false and unchecking returns a true. Nov 1 '19 at 14:17
  • 1
    It might be the fact I haven't had a coffee yet, but have you tried all this without your "displayNat4()" action? If you comment out the logic so it's got no code execution does it work? It might be working against you here Nov 1 '19 at 14:23
1

So it was two things:

  1. The reference to the Id of the apex:outputPanel is case sensitive so you need to look at your capitalization in what you're using in reRender.

  2. Set was already storing the change in button value on the variable

It might be worth looking at this great answer about get;set; as I think it was a misunderstanding of that.

Apex Controllers: What do the get; set; do?

The set part is basically doing what you're re-doing in your action (copied below from the answer above).

enter image description here

When you toggle the button the value of showNat4 is already changed appropriately without you needing to handle it in apex (and then the reRender would have the outputPanel evaluated on this new value). What you ended up doing was working against yourself. The button would change to true and then the action in apex would switch it to false on you.

2
  • When the first attempt to rerender did not work I added the method to try and set the value. Because I thought that {set;} was not working. Thanks for taking the time Kris. Nov 1 '19 at 15:13
  • 1
    Ah, that explains it. Either way, glad we got to the bottom of it. Nov 1 '19 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.