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
ard_17ard_17 

Selectable checkbox in table in custom visualforce page?

I have a VF page that searches for contacts using the first name and mailing state of the contact. It then returns a list of contacts on that same page that match the search. I would like to be able to select a checkbox next to the returned records and be able to update the mailing state for all of the selected records. Here is what I have:
 
<apex:page controller="searchContacts_Controller">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Search" action="{!searchContacts}" reRender="contact-table" />
            </apex:pageBlockButtons>

            <apex:pageBlockSection id="contact-table" columns="1">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="FirstName" />
                    <apex:inputText value="{!Firstname}" />
                </apex:pageBlockSectionItem>

                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Mailing State" />
                    <apex:inputText value="{!mailingState}" />
                </apex:pageBlockSectionItem>

                <apex:pageBlockTable value="{!contacts}" var="c">
                    
                    <apex:column >
                        <apex:facet name="header">Name</apex:facet>
                        {!c.FirstName}
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">Mailing State</apex:facet>
                        {!c.MailingState}
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class searchContacts_Controller {
    public List<Contact> contacts { get; set; }
    public String Firstname { get; set; }
    public String mailingState { get; set; }

    public applyPayments_Controller()
    {
        contacts = new List<Contact>();
    }

    public PageReference searchContacts()
    {
        contacts = [select Id
                          ,FirstName
                          ,MailingState 
                     from Contact 
                    where FirstName = :Firstname
                    and MailingState = :mailingState];
        return null;
    }
}

Any help is appreciated. Thanks!
Madhukar_HeptarcMadhukar_Heptarc
Hi Adriana,
I understand your requirement, you are almost reached. try this below code it may helps you.
 
Apex Class :
=========
public class Community_Checkbox 
{
    public List<Contact> contacts { get; set; }
    public String Firstname { get; set; }
    public String MailingState { get; set; }
    public Boolean checked {get;set;}

    public Community_Checkbox()
    {
        contacts = new List<Contact>();
    }
    public PageReference searchContacts()
    {
        contacts = [select Id,
                           Name,
                    	   phone,
                           MailingState
                      from Contact
                     where FirstName = :Firstname
                       and MailingState = :MailingState];
        return null;
    }
    public void Displayrecord()
    {
        if(checked == true)
        {
           upsert contacts; 
        } 
        else if(checked == false)
        {
            return;
        }
    }
}

Visual Force Code :
==============
<apex:page controller="Community_Checkbox">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Search" action="{!searchContacts}" reRender="contact-table" />
                <apex:commandButton value="Save" action="{!Displayrecord}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection id="contact-table" columns="1">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="FirstName" />
                    <apex:inputText value="{!Firstname}" />
                </apex:pageBlockSectionItem>
               <apex:pageBlockSectionItem >
                    <apex:outputLabel value="MailingState" />
                    <apex:inputText value="{!MailingState}" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockTable value="{!contacts}" var="c">
                    <apex:column >
                    <apex:inputCheckbox value="{!checked}"/>
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">Name</apex:facet>
                        {!c.Name}
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">MailingState</apex:facet>
                        <apex:inputfield value="{!c.MailingState}"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Please let me know if it's help you. 

Thanks & Regards.
Madhukar_Heptarc