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
AkiTAkiT 

Partial page refresh leaves checkbox fields as selected

Hello folks,

Any idea why this happens:
-I have a list of wrapped items with checkbox in each row
-User can tick one and click "Activate" --> controller sets checkbox to true and only-1-logic deactivates old selection
-Partial refresh happens that rerenders the list.

Problem is that old selection still shows as selected - together with the new selection. The "activation" action works fine - after refreshing browser the values get correctly redrawn from the database. Can it somehow come from the controller that may give the existing selections plus the new selection..?

-AT
Code:
<apex:detail relatedList="false" relatedListHover="false" />

<apex:form >
   
<!-- HERE IS THE SELECTION LIST -->
<apex:outputPanel id="verslist">
 <apex:actionstatus id="verstatus" startText="(Activating...)">
  <apex:facet name="stop">
                    <apex:pageBlock title="Versionlist" >
          
        <apex:pageBlockButtons location="top">
              <apex:outputPanel id="button">
                  <apex:commandButton alt="Activate" value="Activate" action="{!activation}" status="verstatus" rerender="verslist"/>
                  
              </apex:outputPanel>
                  
              </apex:pageBlockButtons>
              <!-- vVer records from wrapper class -->
              
              <apex:pageBlockTable value="{!versions}" var="v" id="table">
              <apex:column >
                       <apex:inputCheckbox value="{!v.selected}" onclick="deselectOther(this)"/>
                   <apex:actionSupport event="onclick" action="{!enable}"  rerender="out,button" status="status2"/>
                   
                  </apex:column>
                  <apex:column value="{!v.ver.Name}" />
                  <apex:column value="{!v.ver.Version_Number__c}" />
                  <apex:column value="{!v.ver.Description__c}" />
                  <apex:column value="{!v.ver.Active__c}" />
              </apex:pageBlockTable>
          </apex:pageBlock>
   </apex:facet> 
   
  </apex:actionstatus> 
</apex:outputPanel>
</apex:form>

<apex:outputPanel id="out" >
          <apex:actionstatus id="status2" startText="Refreshing...">
             <apex:facet name="stop"> 
               <apex:outputPanel rendered="{!dpanel}"> 
                  
                 <apex:pageBlock title="Details">
                 <apex:pageBlockTable value="{!products}" var="p" id="restable">
                 
                 <apex:column value="{!p.Product__c}" />
                 <apex:column value="{!p.Quantity__c}" />
                 <apex:column value="{!p.Price__c}" />
                 
          </apex:pageBlockTable>
           </apex:pageBlock>
              </apex:outputPanel>
            </apex:facet> 
          </apex:actionstatus> 
     </apex:outputPanel> 
 
<script>          
        var selectedChkbox;
        
        function deselectOther(chkBox) {
            if (chkBox.checked) {
                if ((chkBox != selectedChkbox) && (selectedChkbox != null)) {
                    selectedChkbox.checked = false;
                }
                selectedChkbox = chkBox;
            }            
        }
        
        
        
</script>  

</apex:page>

 



Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd
It's tricky to code this with out being able to see it in action but I have tried to take a stab at this. There are many ways to do this so by no means is my way, the way, just a different way. I've put the javascript logic in the controller and I've made some changes that reduces the amount of code. I haven't checked syntax so there may be some issues there but I think this should work.

Code:
PAGE:

<apex:page>
 <apex:form>
  <!-- HERE IS THE SELECTION LIST -->
  <apex:outputPanel id="verslist">
   <apex:actionstatus id="verstatus" startText="(Activating...)">
    <apex:facet name="stop">
     <apex:pageBlock title="Versionlist">
      
      <apex:pageBlockButtons location="top">
       <apex:outputPanel id="button">
        <apex:commandButton alt="Activate" value="Activate" action="{!activation}" status="verstatus" rerender="verslist" />
       </apex:outputPanel>
      </apex:pageBlockButtons>
      
      <!-- vVer records from wrapper class -->
      <apex:pageBlockTable value="{!versions}" var="v" id="table">
       <apex:column>
        <apex:outputPanel id="checkboxes">
         <apex:inputCheckbox value="{!v.selected}" onclick="deselectOther(this)" />
         <apex:actionSupport event="onclick" action="{!enable}"rerender="out,button,checkboxes" status="status2" />
         <apex:param name="checked" value="{!v.ver.Id}" />
        </apex:outputPanel>
       </apex:column>
       <apex:column value="{!v.ver.Name}" />
       <apex:column value="{!v.ver.Version_Number__c}" />
       <apex:column value="{!v.ver.Description__c}" />
       <apex:column value="{!v.ver.Active__c}" />
      </apex:pageBlockTable>
      
     </apex:pageBlock>
    </apex:facet>
   </apex:actionstatus>
  </apex:outputPanel>
 </apex:form>

 <apex:outputPanel id="out">
  <apex:actionstatus id="status2" startText="Refreshing...">
   <apex:facet name="stop">
    <apex:outputPanel rendered="{!dpanel}">
     <apex:pageBlock title="Details">
      <apex:pageBlockTable value="{!products}" var="p" id="restable">
       <apex:column value="{!p.Product__c}" />
       <apex:column value="{!p.Quantity__c}" />
       <apex:column value="{!p.Price__c}" />
      </apex:pageBlockTable>
     </apex:pageBlock>
    </apex:outputPanel>
   </apex:facet>
  </apex:actionstatus>
 </apex:outputPanel>
</apex:page>

Controller:

public class tenderPage {
   
 public PageReference configure2() {

  .... 
 }
   
 //Global utility variable
 public Boolean askPageSelections = false;
 public ID selectedVersion {get; set;}
  
  
 //--------------------------------- BUTTON / PANEL ENABLERS --------------------

 // ***************** ENABLE IS HERE   ***********************  
 // pageref function called in event to activate panels / buttons
 public PageReference enable() {
  selectedVersion = System.currentPageReference().getParameters().get('checked');

  for(vVersion v : getVersions()){
   if(v.ver.Id == selectedVersion){
    v.selected == true;
   }else{
    v.selected == false;
   }
  }
  return null;
 }


//--------------------------------- END BUTTON / PANEL ENABLERS END-------------------- 

 public tenderPage(ApexPages.StandardController stdController) {
  this.tend = (Tender__c)stdController.getRecord();
 }

 // *** Get the products for the selected version ***
 public List<Product_offering__c> getProducts() {

  //something like this:
  .... List<Product_offering__c> prods = [select Id, Product__c, Quantity__c, Price__c from Product_offering__c where VersionID = :selectedVersion]
   
 return prods;
 }
        
        
// ***************** ACTIVATION IS HERE   ***********************
 public pageReference activation() {

  List <Version__c> vers = new List <Version__c>(); 

  for(vVersion vver : getVersions()){
   if(vver.selected == true){
    vers.add(vver.ver);
   }
  }

  if(!vers.isEmpty())
  update vers;

  return null;
 }
  
  
// ------------------------------ VERSION HANDLING ------------------------------------------------
         

 //Collection of the class/wrapper objects  
 public List<vVersion> versionList {get; set;}

 public List<vVersion> getVersions() {

 if(versionList == null){ 
  Boolean b = false;

  // Instantiate new version list
  versionList = new List<vVersion>();

  for (Version__c v : [select Id, Name, Active__c, Description__c, Version_Number__c from Version__c where Tender__c = :tend.Id]){
   versionList.add(new vVersion(v,b));
  }
 }

 return versionList;
 }
       

// ----------------  wrapper class --------------------------------     
    
        public class vVersion{
                
         ...
        }
        
}

 

All Answers

TehNrdTehNrd
Could you please post your
action="{!enable}" 
action="{!activation}"
methods. I'm also a little confused as to why you are calling the checkbox javascript. I'm not sure if this is needed....or I'm misunderstanding what it does.


Message Edited by TehNrd on 11-19-2008 02:28 PM
AkiTAkiT
The checkbox javascript only takes care that user can tick one box.

the {!enable} may be unnecessary, as the intend is just to rerender button row - buttons either show or hide based on disabled value - this is not in the above VF code but it should be: <apex:commandButton disabled="{!stat}".../>

controller (some part shortened):
I feel the problem is in the "version handling" part - page rerenders the old selections...
Code:
public class tenderPage {
   
   public PageReference configure2() {
    
     .... 
    }
 
  
 //Global utility variable
 public Boolean askPageSelections = false;
 
  
  
//--------------------------------- BUTTON / PANEL ENABLERS --------------------
   
 
// ***************** ENABLE IS HERE   ***********************  
 // pageref function called in event to activate panels / buttons
 public PageReference enable() {
  
  return null;
        }
        
        // func to return param to activate element
        public Boolean getStat() {
         
         
                
  for(vVersion vVer : getSelVersions()){
                     
                     if(vVer.selected == true){
                         return false;
                     }else{
                      return true;
                     }
           }
             
         // else, list is empty and return true    
                return true;
                
          
        }
        
        // func to return param to activate element - contrary to getStat
        public Boolean getDpanel() {
        
         return !getStat();
        
        }
        
//--------------------------------- END BUTTON / PANEL ENABLERS END-------------------- 

  
  public tenderPage(ApexPages.StandardController stdController) {
   this.tend = (Tender__c)stdController.getRecord();
  }
  
  
  // *** Get the products for the selected version ***
  public List<Product_offering__c> getProducts() {
   
   ....   
         
   return prods;
         }
        
        
// ***************** ACTIVATION IS HERE   ***********************
          public pageReference activation() {
   
   List <Version__c> vers = new List <Version__c>(); 
   
   for(vVersion vver : getSelVersions()){
  
    vers.add(vver.ver);
  
   }
   
   for(Version__c v : vers){
   
    v.Active__c = true;
   
   }
   
   if(!vers.isEmpty())
    update vers;
   
   
   
         return null;
        }
  
  
// ------------------------------ VERSION HANDLING ------------------------------------------------
         

       //Collection of the class/wrapper objects  
        public List<vVersion> versionList {get; set;}
        
 public List<vVersion> getVersions() {
       
       if(!askPageSelections){ 
        Boolean b = false;
          
         // Instantiate new version list
                    versionList = new List<vVersion>();
   
   for (Version__c v : [select Id, Name, Active__c, Description__c, Version_Number__c from Version__c where Tender__c = :tend.Id]){
          
             
    versionList.add(new vVersion(v,b));
             
   }
       }
        
  return versionList;
 }
       
      
 // Getter to return the current list with its selections - with no re-instantanation
 public List<vVersion> getSelVersions() {
       
       // set to true to indicate that we only need to get current page selections
        askPageSelections = true;
       
       List<vVersion> tickedVersions = new List<vVersion>();
       
       if(versionList != null){
        System.debug('Selected versions:');
        for(vVersion vv : versionList){
        
         if(vv.selected){
          tickedVersions.add(vv);
          system.debug(vv);
         }        
        }
       }
       
        return tickedVersions;
         
      }
      
  
  
    


// ----------------  wrapper class --------------------------------     
    
        public class vVersion{
                
         ...
        }
        
}

 



Message Edited by AT on 11-20-2008 10:56 AM

Message Edited by AT on 11-20-2008 11:10 AM
TehNrdTehNrd
It's tricky to code this with out being able to see it in action but I have tried to take a stab at this. There are many ways to do this so by no means is my way, the way, just a different way. I've put the javascript logic in the controller and I've made some changes that reduces the amount of code. I haven't checked syntax so there may be some issues there but I think this should work.

Code:
PAGE:

<apex:page>
 <apex:form>
  <!-- HERE IS THE SELECTION LIST -->
  <apex:outputPanel id="verslist">
   <apex:actionstatus id="verstatus" startText="(Activating...)">
    <apex:facet name="stop">
     <apex:pageBlock title="Versionlist">
      
      <apex:pageBlockButtons location="top">
       <apex:outputPanel id="button">
        <apex:commandButton alt="Activate" value="Activate" action="{!activation}" status="verstatus" rerender="verslist" />
       </apex:outputPanel>
      </apex:pageBlockButtons>
      
      <!-- vVer records from wrapper class -->
      <apex:pageBlockTable value="{!versions}" var="v" id="table">
       <apex:column>
        <apex:outputPanel id="checkboxes">
         <apex:inputCheckbox value="{!v.selected}" onclick="deselectOther(this)" />
         <apex:actionSupport event="onclick" action="{!enable}"rerender="out,button,checkboxes" status="status2" />
         <apex:param name="checked" value="{!v.ver.Id}" />
        </apex:outputPanel>
       </apex:column>
       <apex:column value="{!v.ver.Name}" />
       <apex:column value="{!v.ver.Version_Number__c}" />
       <apex:column value="{!v.ver.Description__c}" />
       <apex:column value="{!v.ver.Active__c}" />
      </apex:pageBlockTable>
      
     </apex:pageBlock>
    </apex:facet>
   </apex:actionstatus>
  </apex:outputPanel>
 </apex:form>

 <apex:outputPanel id="out">
  <apex:actionstatus id="status2" startText="Refreshing...">
   <apex:facet name="stop">
    <apex:outputPanel rendered="{!dpanel}">
     <apex:pageBlock title="Details">
      <apex:pageBlockTable value="{!products}" var="p" id="restable">
       <apex:column value="{!p.Product__c}" />
       <apex:column value="{!p.Quantity__c}" />
       <apex:column value="{!p.Price__c}" />
      </apex:pageBlockTable>
     </apex:pageBlock>
    </apex:outputPanel>
   </apex:facet>
  </apex:actionstatus>
 </apex:outputPanel>
</apex:page>

Controller:

public class tenderPage {
   
 public PageReference configure2() {

  .... 
 }
   
 //Global utility variable
 public Boolean askPageSelections = false;
 public ID selectedVersion {get; set;}
  
  
 //--------------------------------- BUTTON / PANEL ENABLERS --------------------

 // ***************** ENABLE IS HERE   ***********************  
 // pageref function called in event to activate panels / buttons
 public PageReference enable() {
  selectedVersion = System.currentPageReference().getParameters().get('checked');

  for(vVersion v : getVersions()){
   if(v.ver.Id == selectedVersion){
    v.selected == true;
   }else{
    v.selected == false;
   }
  }
  return null;
 }


//--------------------------------- END BUTTON / PANEL ENABLERS END-------------------- 

 public tenderPage(ApexPages.StandardController stdController) {
  this.tend = (Tender__c)stdController.getRecord();
 }

 // *** Get the products for the selected version ***
 public List<Product_offering__c> getProducts() {

  //something like this:
  .... List<Product_offering__c> prods = [select Id, Product__c, Quantity__c, Price__c from Product_offering__c where VersionID = :selectedVersion]
   
 return prods;
 }
        
        
// ***************** ACTIVATION IS HERE   ***********************
 public pageReference activation() {

  List <Version__c> vers = new List <Version__c>(); 

  for(vVersion vver : getVersions()){
   if(vver.selected == true){
    vers.add(vver.ver);
   }
  }

  if(!vers.isEmpty())
  update vers;

  return null;
 }
  
  
// ------------------------------ VERSION HANDLING ------------------------------------------------
         

 //Collection of the class/wrapper objects  
 public List<vVersion> versionList {get; set;}

 public List<vVersion> getVersions() {

 if(versionList == null){ 
  Boolean b = false;

  // Instantiate new version list
  versionList = new List<vVersion>();

  for (Version__c v : [select Id, Name, Active__c, Description__c, Version_Number__c from Version__c where Tender__c = :tend.Id]){
   versionList.add(new vVersion(v,b));
  }
 }

 return versionList;
 }
       

// ----------------  wrapper class --------------------------------     
    
        public class vVersion{
                
         ...
        }
        
}

 

This was selected as the best answer