+ Start a Discussion
SubhaSubha 

How to get value of Checkbox in a datatable

I am writing a datatable that contains data of an Object and I have added one more column that with checkbox in each row... when I submit a button I want to get the rows of selected checkboxes... Can you please tell me how to get this

Best Answer chosen by Admin (Salesforce Developers) 
SFDC_LearnerSFDC_Learner

hello Subha,

 

Just implement this code.

 

<apex:page controller="wrapchkcon">
     <apex:form >
         <apex:pageBlock >
             <apex:pageBlockButtons >
                 <apex:commandButton value="Selectednames" action="{!selname}"/>
             </apex:pageBlockButtons>
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!lstwrapper}" var="w">
                    <apex:column headerValue="CheckValue">
                        <apex:inputcheckbox value="{!w.checked}"/>
                    </apex:column>
                    <apex:column headerValue="Name">
                        {!w.names}
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>        
         </apex:pageBlock>
     </apex:form>
</apex:page>

 

 

 

class:

 

public with sharing class wrapchkcon {

    List<String> selectednames =new List<String>();
    public void selname()
    {
       
        for(wrapper w: lstwrapper)
        {
            if(w.checked==true)
            {
                selectednames.add(w.names);
            }
        }
        system.debug('************ Selected Names are **********'+selectednames);
       
    }


   
    List<Departmentt__c> lstdept=new List<Departmentt__c>();
    List<wrapper> lstwrapper=new List<wrapper>();
    wrapper w;
   
    public List<wrapper> getlstwrapper()
    {
        return lstwrapper;
    }
    public wrapchkcon()
    {
        lstdept=[select Id,name from Departmentt__c];
        for(Integer i=0;i<lstdept.size();i++)
        {
        w=new wrapper(lstdept[i].name);
        lstwrapper.add(w);
        }
       
    }
   
    public class wrapper
    {
        public String names{get;set;}
        public boolean checked{get;set;}
        public wrapper(String names)
        {
            this.names=names;
            checked=false;
        }
    }
}

 

 

 

All Answers

Ankit AroraAnkit Arora

You need to use the wrapper class. A very good documentation is here :

 

http://wiki.developerforce.com/index.php/Wrapper_Class

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

SFDC_LearnerSFDC_Learner

hello Subha,

 

Just implement this code.

 

<apex:page controller="wrapchkcon">
     <apex:form >
         <apex:pageBlock >
             <apex:pageBlockButtons >
                 <apex:commandButton value="Selectednames" action="{!selname}"/>
             </apex:pageBlockButtons>
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!lstwrapper}" var="w">
                    <apex:column headerValue="CheckValue">
                        <apex:inputcheckbox value="{!w.checked}"/>
                    </apex:column>
                    <apex:column headerValue="Name">
                        {!w.names}
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>        
         </apex:pageBlock>
     </apex:form>
</apex:page>

 

 

 

class:

 

public with sharing class wrapchkcon {

    List<String> selectednames =new List<String>();
    public void selname()
    {
       
        for(wrapper w: lstwrapper)
        {
            if(w.checked==true)
            {
                selectednames.add(w.names);
            }
        }
        system.debug('************ Selected Names are **********'+selectednames);
       
    }


   
    List<Departmentt__c> lstdept=new List<Departmentt__c>();
    List<wrapper> lstwrapper=new List<wrapper>();
    wrapper w;
   
    public List<wrapper> getlstwrapper()
    {
        return lstwrapper;
    }
    public wrapchkcon()
    {
        lstdept=[select Id,name from Departmentt__c];
        for(Integer i=0;i<lstdept.size();i++)
        {
        w=new wrapper(lstdept[i].name);
        lstwrapper.add(w);
        }
       
    }
   
    public class wrapper
    {
        public String names{get;set;}
        public boolean checked{get;set;}
        public wrapper(String names)
        {
            this.names=names;
            checked=false;
        }
    }
}

 

 

 

This was selected as the best answer
SamdarshSamdarsh

This is working fine, but if we unselect some items and select some more items, the previous ones are still shown in the debug log. Any resolution about that !