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
Tyler HarrisTyler Harris 

Rerender Page Based on Checkbox or InputField

I'm having trouble rerendering a section on a Visualforce page.

I want to rerender the "Output Panel" with the id 'changeEm'. I've got a lookup to a Contact ct.Buyer__c and a checkbox that if changed to show/hide id='changeEm'

Let me know if there is a better Apex solution. Also, is there a way to how this occured in an AJAX fashion on page?
 
<apex:form >
      <apex:pageBlock >
     	
          Find Existing Contact <apex:inputField value="{!ct.Buyer__c}" ><apex:actionSupport event='onchange' reRender='changeEm'/></apex:inputField><br/><br/>
          Create New Buyer? <apex:inputCheckbox value ='{!turn}'><apex:actionSupport event='onchange' reRender='changeEm'/></apex:inputCheckbox>
           
      </apex:pageBlock>
      <apex:outputPanel id='changeEm'>
      <apex:pageBlock>
          First Name: <apex:inputText title="First Name" /><br/>
          Last Name: <apex:inputText title="Last Name" /><br/>
          		Email Address: <apex:inputText title="Email Address"/>
          
      </apex:pageBlock>
          </apex:outputPanel>
      
     <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">
			
   <apex:column headerValue="ID" rendered="false">
       <apex:outputText value="{!cart[carti].merchandise.Id}" >
       </apex:outputText>
          </apex:column>
          <apex:column headerValue="Product">
              <apex:outputText value="{!cart[carti].merchandise.name}">
          	
         </apex:outputText>
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!cart[carti].merchandise.price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:outputText value="{!cart[carti].count}"/>
          </apex:column>
      </apex:dataTable>
      <apex:commandButton action="{!back}" value="Back"/>
    </apex:form>

 
Vetriselvan ManoharanVetriselvan Manoharan
Hi Tyler,

Try something like this. Get the checkbox value in your controller.

public Boolean isChecked {get;set;}
 And assign isChecked to true or false based on the checkbox.

In VF page,

<apex:outputPanel rendered='{!ischecked == true}'>

Thanks,
Vetri
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Tyler,
Agreeing with to Vetriselvan's comment, I would like to add something.

change your vf like this,
ind Existing Contact 
<apex:inputField value="{!ct.Buyer__c}" ><apex:actionSupport event='onchange' reRender='changeEm' action="{!doShow}"/></apex:inputField><br/><br/>

Create New Buyer? <apex:inputCheckbox value ='{!turn}'><apex:actionSupport event='onchange' reRender='changeEm' action="{!doShow}" /></apex:inputCheckbox>
           
<apex:outputPanel id='changeEm' rendered="{!showPanel}">
      <apex:pageBlock>
          First Name: <apex:inputText title="First Name" /><br/>
          Last Name: <apex:inputText title="Last Name" /><br/>
          		Email Address: <apex:inputText title="Email Address"/>
          
      </apex:pageBlock>
</apex:outputPanel>
Prosenjit Sarkar 7Prosenjit Sarkar 7
Now Add these in your custom controller,
 
public Boolean showPanel{get;set;}

public yourConstructor() {
showPanel = false;
}


public void doShow() {
if(shoPanel)
showPanel = false;
else
showPanel = true;
}

 
Tyler HarrisTyler Harris
Thank you for your help. I've followed your advice, but the checkbox is still not rerendering the output area:


Apex:
 
public virtual class StoreFront2 {
public Boolean flip{get;set;}

public StoreFront2(){
        ct=new Purchases__c();
        flip=false;
        
        
    }

public void doShow(){
        if(flip){
            flip = false;
        }
        else{
            flip = true;
        }
        
    }

}

Visualforce
<apex:page controller="StoreFront2" showHeader="false" sidebar="false" >
    <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
    <h1>Confirm Page</h1>
  <apex:form >
      <apex:pageBlock >
     	
          Find Existing Contact <apex:inputField value="{!ct.Buyer__c}" ><apex:actionSupport event="onchange" reRender="changem" action='{!doShow}'/></apex:inputField><br/><br/>
          
          Create New Buyer?<apex:inputCheckbox value="{!flip}" ><apex:actionSupport event='onchange' reRender='changem' action='{!doShow}' /></apex:inputCheckbox>
           
      </apex:pageBlock>
      <apex:outputPanel id="changem" rendered='{!flip}' >
      <apex:pageBlock >
          First Name: <apex:inputText title="First Name" /><br/>
          Last Name: <apex:inputText title="Last Name" /><br/>
          Email Address: <apex:inputText title="Email Address"/>
          
      </apex:pageBlock>
          </apex:outputPanel>
      
     <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">
			
   <apex:column headerValue="ID" rendered="false">
       <apex:outputText value="{!cart[carti].merchandise.Id}" >
       </apex:outputText>
          </apex:column>
          <apex:column headerValue="Product">
              <apex:outputText value="{!cart[carti].merchandise.name}">
          	
         </apex:outputText>
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!cart[carti].merchandise.price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:outputText value="{!cart[carti].count}"/>
          </apex:column>
      </apex:dataTable><br/>
      <apex:commandButton action="{!back}" value="Back"/>
    </apex:form>
  
</apex:page>

 
Vetriselvan ManoharanVetriselvan Manoharan
Hi Tyler,

Change this rendered='{!flip}' to rendered='{!flip == true}' and let me know whether it works. Also check the debug logs whether the value for flip is assigned or not.

Thanks,
Vetri
Tyler HarrisTyler Harris
Hi Vetriselvan,

I've added this and  it still doesn't seem to be firing. I've put debug's all over my code and it doesn't seem to be firing ont he checkbox change.

Visualforce

    
    

Confirm Page

Find Existing Contact

Create New Buyer? First Name:
Last Name:
Email Address:
Apex
public Purchases__c ct{get;set;}
    
    public StoreFront2(){
        ct=new Purchases__c();
        flip=false;
        system.debug(flip);
        
        
    }
   
    public void doShow(){
        if(flip){
            flip = false;
            system.debug(flip);
        }
        else{
            flip = true;
            system.debug(flip);
        }
        
        system.debug(flip);
        
    }


Thank you!
Tyler HarrisTyler Harris
Sorry Visualforce code
public virtual class StoreFront2 {    
    
    public String message { get; set; }
    
    List products;
    
    Map cart;
    
    public Boolean incart{get;set;}
    
    public Id rowz;
    
    public id rowDel{get;set;}
    
    public Boolean flip{get;set;}

    public Purchases__c ct{get;set;}
    
    public StoreFront2(){
        ct=new Purchases__c();
        flip=false;
        system.debug(flip);
        
        
    }
   
    public void doShow(){
        if(flip){
            flip = false;
            system.debug(flip);
        }
        else{
            flip = true;
            system.debug(flip);
        }
        
        system.debug(flip);
        
    }
   
    public PageReference shop(){
   
        handleTheBasket();
        message = 'You bought: ';
        for (DisplayMerchandise p:products){
            if(p.tempCount > 0){
               message += p.merchandise.name + ' (' + p.tempCount + ') ' ;
               }
            }
        return null;
    }
    
    public void remove(){
     rowz = (Id) ApexPages.currentPage().getParameters().get('rowDel');
        system.debug(rowz);
        if(cart.containsKey(rowz)){
            cart.remove(rowz);
            if(cart.isEmpty()){
                incart = false;
                system.debug(incart);
            }      
        }  
       
    }
        public pageReference back(){
        PageReference doit = new PageReference('/apex/StoreCart');
        doit.setRedirect(false);
            return doit;
        }
    
    	public void confirm(){       
        List pur = new List();
    	}
    
    public Pagereference checkout(){
        if(cart.isEmpty()){
            ApexPages.Message myError = new ApexPages.Message(ApexPages.Severity.ERROR, 'Shopping Cart is Empty');
            ApexPages.addMessage(myError);
            System.debug('on');
            return null;
        }
        
        else{
        PageReference send = new PageReference('/apex/ConfirmBuy');
        return send;
        }
    } 
    
    
    public void handleTheBasket(){
        for(DisplayMerchandise c : products){
            if(c.tempCount > 0){
            if(cart.containsKey(c.merchandise.Id)){        
                cart.get(c.merchandise.Id).count += c.tempCount;
                
            }
            else{
                cart.put(c.merchandise.Id, c);
                cart.get(c.merchandise.Id).count = c.tempCount;
                incart = true;
                System.debug(incart);
            } 
        
        }
        }
        
    }
    
    public Map getCart() {
        if(cart == null){
            cart = new Map();
            incart = false;
                }
      
        return cart;
    }
    
    public class DisplayMerchandise {
        public Merchandise__c merchandise{get; set;}
        public Decimal count{get; set;}
        public Decimal tempCount{get;set;}
        public DisplayMerchandise(Merchandise__c item){
            this.merchandise = item;
            
        }
    }

    public List getProducts() {
        if (products == null){
            products = new List();
    
            for (Merchandise__c item :
            [SELECT id, name, description__c, price__c
            FROM Merchandise__c
            WHERE Total_Inventory__c > 0]) {
           
            products.add(new DisplayMerchandise(item));
            }
        }
        return products;
    }

}