[SalesForce] Wrapper Class is not Working when selected a Checkbox

When I select Checkbox it's value has to be passed to another PageBlock, this is My VF Page

<apex:page controller="Dynamicsearch3" sidebar="false" tabStyle="University__c"  >

<apex:form >

<apex:pageMessages />
<apex:outputPanel style="float:left;width:100%">
<apex:outputPanel style="float:right;width:80%">
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel >Specialisation</apex:outputLabel><apex:selectList id="countries" value="{!selectedSpecialisation}" size="1" required="true" >
 <apex:selectOptions value="{!Specialisation}"/><apex:actionSupport event="onchange" reRender="out" status="status"/> 
</apex:selectList>
</apex:pageBlockSectionItem>
</apex:pageBlocksection>
</apex:pageBlock>
<apex:pageBlock >
<apex:pageBlocksection >
<apex:outputPanel id="out" >
        <apex:actionstatus id="status" startText="Searching...">
            <apex:facet name="stop">
                <apex:outputPanel >
                <apex:pageBlockTable value="{!coursesearch1}" var="c" rendered="{!coursesearch1.size > 0}">
                <apex:column headerValue="Select" ><apex:inputcheckbox value="{!c.selected}"> <apex:actionSupport event="onchange" action="{!getselected}" reRender="Selected_PBS"/></apex:inputcheckbox></apex:column>
                <apex:column headerValue="Name">{!c.acc.name}</apex:column>
                <apex:column headerValue="Course Duration">{!c.acc.Course_Duration__c  }</apex:column>
                <apex:column headerValue="Specialisation">{!c.acc.Specialisation__c}</apex:column>
                </apex:pageBlockTable>

                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
</apex:pageBlocksection>
<apex:pageBlockSection id="Selected_PBS">

<apex:pageBlockTable value="{!selectedcoursesearch}" var="c1"  >
                <apex:column headerValue="Name">{!c1.name}</apex:column>
                <apex:column headerValue="Course Duration">{!c1.Course_Duration__c  }</apex:column>
                <apex:column headerValue="Specialisation">{!c1.Specialisation__c}</apex:column>
                </apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>    
</apex:outputPanel>
</apex:form>
</apex:page>

This is My Controller, here In getselected() method when i Use if(wrapAccountObj.selected == true) it is not showing the Results.

public class Dynamicsearch3 {


list<Courses__c> selectedcoursesearch2= new list<Courses__c>();
 public string selectedSpecialisation { get; set; }
 public SelectOption[] Specialisation { get; set; }
 private String soql {get;set;}
 public list<Courses__c> Coursesearch{ get; set; }
 Public List<WrapperClassEx> WrapperList{get;set;}


 public Dynamicsearch3(){
 Specialisation = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult3 = Course_Detail__c.Specialisation__c.getDescribe();
        List<Schema.PicklistEntry> Spec = fieldResult3.getPicklistValues();
        Specialisation.add(new SelectOption('None','--None--'));
        for(Schema.PicklistEntry p3 : Spec)
        {
          Specialisation.add(new SelectOption(p3.getValue(), p3.getValue())); 
        }
 }
 public List<WrapperClassEx> getCoursesearch1() {
        soql ='SELECT Id,Name,Specialisation__c,Course_Duration__c   FROM Courses__c where Name!=null';

        if (selectedSpecialisation !='None')
          {
          soql += ' and Specialisation__c LIKE \''+selectedSpecialisation +'%\'';

          } 


        try {
           Coursesearch= Database.query(soql);

           WrapperList = New List<WrapperClassEx>();
              for(Courses__c acc: Coursesearch){

                    WrapperList.add(New WrapperClassEx(acc)); 
                  }

             }
         catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
      }
    return WrapperList;
    }

 public PageReference getselected() {
    selectedcoursesearch2.clear();
        for(WrapperClassEx wrapAccountObj : WrapperList)
         {if(wrapAccountObj.selected == true)  //**This condition is Not Working**
        selectedcoursesearch2.add(wrapAccountObj.acc);
        }

        return null;
    }
    public list<Courses__c> getSelectedcoursesearch() {
    if(selectedcoursesearch2.size()>0)
        return selectedcoursesearch2;
        else
        return null;    
    }
    Public Class WrapperClassEx{
   Public Courses__c acc{get;set;}
   Public Boolean selected{get;set;}

     Public WrapperClassEx(Courses__c accRec){
        this.acc = accRec;
        this.selected= false;
     }
   }

}

Best Answer

Ok looks like you have some required field in the page! Just wrap the actionSupport component inside the table in a actionRegion

Something like

<apex:actionRegion>
    <apex:inputcheckbox value="{!c.selected}">
        <apex:actionSupport event="onchange" action="{!getselected}" reRender="Selected_PBS" />
    </apex:inputcheckbox>
</apex:actionRegion>
Related Topic