Monday, March 11, 2013

Displaying the records with Checkbox of Contact Object using Wrapper Class


Visualforce Page:

<apex:page controller="checkboxcls">
<apex:form >
    <apex:PageBlock >
        <apex:pageBlockTable value="{!items}" var="i">
        <apex:column >
            <apex:inputCheckbox value="{!i.check}"/>
        </apex:Column>
                <apex:column value="{!i.con.name}"/>
                <apex:column value="{!i.con.phone}"/>
        </apex:pageBlockTable>
        <apex:commandButton value="Selected" action="{!selected}"/>
    </apex:PageBlock>
</apex:form>
</apex:page>

Apex Class:

public with sharing class checkboxcls {
    public PageReference selected() {
    System.debug('------------------------>lstwrap is'+lstwrap);
    for(wrappecls w:lstwrap){
        if(w.check==true){
            System.debug('---------->'+w.con);
                                   }
                                         }  
        return null;
                                                      }
    public class wrappecls{
        public boolean check{get; set;}
        public Contact con{get; set;}
     
        public wrappecls(Contact c){
            con =c;
            check=false;
                                                    }
     
                                      }
  
    LIst<wrappecls> lstwrap= new List<wrappecls>();
 
    public List<wrappecls> getitems(){
    List<Contact> lst=[Select id, name, phone from Contact];
    for(Contact c:lst){
    lstwrap.add(new wrappecls(c));
                              }
    System.debug('------------------------>lstwrap is'+lstwrap);
    return lstwrap;
                                                       }
                                                         }

No comments:

Post a Comment