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
Abu Zafar WahidAbu Zafar Wahid 

How to add/bind checkbox value in wrapper class

Hi everyone,

Recentetly I've added a form page where user can add much as rows as he wants. All of the input fields keep the previous row data in the form except the checkbox.

FYI, the checkbox does not belong to any standard or custom object, it is just a checkpoint for checking if certain input field checked or not.

I tried to declare the checkbox as List<Boolean> but same issue. Please advise.

Thanks
Best Answer chosen by Abu Zafar Wahid
Prem Anandh 1Prem Anandh 1
Abu, Are facing below issue?

When you clicks on "Add Row", it's not binding the existing selected one. Correct? 

I have ran your code in my dev org and its working as expected apart from above issue. 

If this is only an issue, Please remove "Immediate = true" attribute from "Add Row" button, it will not allow your data to bind your controller

All Answers

Mahesh DMahesh D
Hi Abu,

Please post your current code here and will provide the solution on top of it.

You just need to use the public Boolean isSelected {get; set;} in the wrapper class.

Regards,
Mahesh
Abu Zafar WahidAbu Zafar Wahid

Hi Mahesh,

Thanks for your quick response, please see below:

 

<apex:pageBlockTable value="{!waContList}" var="eachRecord" width="100%" id="infoform">
                    <apex:column headerValue="Applicant Info" width="87" styleClass="header-title" style="vertical-align: top;">  
                        <apex:pageBlock >
                            <apex:pageBlockSection columns="5" title="Personal & Contact Information" collapsible="true" id="personalContactInfo">
                                <apex:outputLabel style="width: 120px" value="Fingerprints Taken" for="fingerprintsTaken" />
                                <apex:inputCheckbox style="width: 50px" id="fingerprintsTaken" value="{!eachRecord.printsTaken}" />
                                <apex:outputLabel style="width: 140px" value="Fee" for="printsFee" />
                                <apex:input style="width: 240px" id="printsFee" value="{!eachRecord.printsFee}"/>
                            </apex:pageBlockSection>
                        </apex:pageBlock>
                    </apex:column>
                    <apex:column headerValue="Action" width="3%" styleClass="header-title" style="vertical-align: middle;">
                        <apex:commandLink value="Remove" style="color:red" action="{!removeRowFromEmpList}" rendered="{!rowNum > 0}" rerender="accountHead" immediate="true" >
                            <apex:param value="{!rowNum}" name="rowToRemove" assignTo="{!rowToRemove}"/>
                        </apex:commandLink>
                        <apex:variable var="rowNum" value="{!rowNum + 1}"/>
                    </apex:column> 
                </apex:pageBlockTable>
public with sharing class AddEmployeeIntakeController {
    public  List<Boolean>                createDisclosure {get;set;}
    public  List<Boolean>                createPhoto      {get;set;}
    public  List<Boolean>                printsTaken      {get;set;}

    /**
    *   Custom object type declaration for additioanl add and row remove funtion
    *   It's wrpaeer class to bind eachrow record
    **/  
    public class WrapperContactList{
        public Integer               index                  {get;set;}
        public Employment_History__c employeeRecord         {get;set;}
        public Application__c        applicationRecord      {get;set;}
        public Contact               contactRecord          {get;set;}
        public Note                  submissionRecord       {get;set;}
        
        public Decimal               disclosureFee          {get;set;}

        public List<Boolean>         createPhoto            {get;set;}
        public List<Boolean>         printsTaken            {get;set;}
        public List<Boolean>         createDisclosure       {get;set;}
        
        public Decimal               photoFee               {get;set;}
        public Decimal               printsFee              {get;set;}
        public Map<String, Boolean>  test                   {get;set;}
    }


    /**
    *   Add additional row in the form
    **/
    public void addNewRowToEmpList(){
        try {
            WrapperContactList newRecord = new WrapperContactList();

            List<Boolean>  newCreateDisclosure  = new List<Boolean>();
            List<Boolean>  newCreatePhoto       = new List<Boolean>();
            List<Boolean>  newPrintsTaken       = new List<Boolean>();


            newRecord.createDisclosure  = newCreateDisclosure;
            newRecord.createPhoto       = newCreatePhoto;
            newRecord.printsTaken       = newPrintsTaken;

            newRecord.index = waContList.size();
            waContList.add(newRecord);
            System.debug(waContList);
        } catch (Exception e) {
            System.debug(e.getMessage());
            ApexPages.addMessages(e);
        }
    }
}

 
Prem Anandh 1Prem Anandh 1
Hi Abu,

Please see below code for example

Visualfore Page:
 
<apex:page controller="AccountController">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockTable value="{!lstWrapper}" var="lstWrap">
                
                <apex:column>
                    <apex:inputCheckbox value="{!lstWrap.isSelected}" />
                    <apex:inputField value="{!lstWrap.objAccount.Name}" />
                </apex:column>
                
            </apex:pageBlockTable>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
 
public with sharing class AccountController {
    
    public List<wrapperAccount> lstWrapper {get;set;}
    public AccountController()
    {
        lstWrapper = new List<wrapperAccount>();
        for(Account objAccount : [Select Id, Name from Account limit 10])
        {
            lstWrapper.add(new wrapperAccount(objAccount));
        }
    }
    
    public class wrapperAccount
    {
        public Boolean isSelected {get;set;}
        public Account objAccount {get;set;}
        
        public wrapperAccount(Account objAcc)
        {
            objAccount = new Account();
            isSelected = false;
            objAccount = objAcc;
        }
    }
}
Thanks,
Prem Anandh
 
Abu Zafar WahidAbu Zafar Wahid

Hi Prem,

Thank you, my scenerio is slightly different, I'm using your example and rearrange them according my situation


 

<apex:page controller="AddEmployeeIntakeController">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockTable value="{!waContList}" var="eachRecord">
                
                <apex:column>
                    <apex:inputCheckbox value="{!eachRecord.createDisclosure}" />
                    <apex:inputCheckbox value="{!eachRecord.createPhoto}" />
                    <apex:inputCheckbox value="{!eachRecord.printsTaken}" />
                </apex:column>
                
            </apex:pageBlockTable>
            <apex:pageBlockButtons location="bottom" >
                <apex:commandButton value="Add Row" action="{!addNewRowToEmpList}"  rerender="accountHead" Status="status" immediate="true" />
            </apex:pageBlockButtons>              
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public with sharing class AddEmployeeIntakeController{
    
    public  List<WrapperContactList>     waContList       {get;set;}

    public AddEmployeeIntakeController()
    {
        waContList = new List<WrapperContactList>();
        addNewRowToEmpList();
    }
    
    public class WrapperContactList
    {
        public Boolean         createPhoto            {get;set;}
        public Boolean         printsTaken            {get;set;}
        public Boolean         createDisclosure       {get;set;}
    }

    public void addNewRowToEmpList(){
        WrapperContactList newRecord = new WrapperContactList();
        newRecord.createDisclosure  = false;
        newRecord.createPhoto       = false;
        newRecord.printsTaken       = false;
        waContList.add(newRecord);
    }
}
The problem is happening when trying to add a new row, then found that the previous checkboxes are uncheked. Which is because the the value of these fields are not binded in the wrapper.
Prem Anandh 1Prem Anandh 1
If you like the suggestion please mark it as answer, It will useful for others to find the solutions
Abu Zafar WahidAbu Zafar Wahid

Prem,

No bro it's not helping me yet, let me post my refiened code snippet so that you can guide me to the right way:

<apex:page docType="html-5.0" controller="TestWrapperController" sidebar="false">
    <apex:include pageName="pca__Component"/>
   
    <apex:pageMessages />
   
    <apex:form id="accountEditForm">
        <apex:pageBlock id="thePB">
            <apex:outputPanel id="accountHead">
                <apex:variable value="{!0}" var="rowNum"/>
                <apex:pageBlockTable value="{!waContList}" var="eachRecord" width="100%" id="infoform">
                    <apex:column headerValue="Action" width="3%" styleClass="header-title" style="vertical-align: middle;">
                        <apex:commandLink value="Remove" style="color:red" action="{!removeRowFromEmpList}" rendered="{!rowNum > 0}" rerender="accountHead" immediate="true" >
                            <apex:param value="{!rowNum}" name="rowToRemove" assignTo="{!rowToRemove}"/>
                        </apex:commandLink>
                        <apex:variable var="rowNum" value="{!rowNum + 1}"/>
                    </apex:column> 
                    <apex:column headerValue="Applicant Info" width="87" styleClass="header-title" style="vertical-align: top;">  

                        <apex:pageBlock >
                            <apex:pageBlockSection columns="8" title="Details to Create">
                                <apex:outputLabel style="width: 120px" value="Create Photo" for="createPhoto" />
                                <apex:inputCheckbox style="width: 50px" id="createPhoto" value="{!eachRecord.createPhoto}" />
                            </apex:pageBlockSection>
                        </apex:pageBlock>                      

                    </apex:column>
                </apex:pageBlockTable>    
            </apex:outputPanel>
            <apex:pageBlockButtons location="bottom" >
                <apex:commandButton value="Add Row" action="{!addNewRowToEmpList}"  rerender="accountHead" Status="status" immediate="true" />
            </apex:pageBlockButtons>                   
        </apex:pageBlock>
    </apex:form>
</apex:page>


public with sharing class TestWrapperController {
    public  List<WrapperContactList>     waContList       {get;set;}
    public  Integer                      rowToRemove      {get;set;}
    /**
    * Constructor method
    **/
    public TestWrapperController () {
        waContList = new List<WrapperContactList>();

        addNewRowToEmpList();
    }
    
    public class WrapperContactList{
        public Integer         index                  {get;set;}
        public Boolean         createPhoto            {get;set;}
    }
    
    public void addNewRowToEmpList(){
        try {
            
            System.debug(waContList);        
            WrapperContactList newRecord = new WrapperContactList();
            newRecord.createPhoto       = false;
            
            newRecord.index = waContList.size();
            waContList.add(newRecord);
            System.debug(waContList);
        } catch (Exception e) {
            System.debug(e.getMessage());
            ApexPages.addMessages(e);
        }
    }    
    
    public void removeRowFromEmpList() {
        try { 
            waContList.remove(rowToRemove);
        } catch(Exception e) {
            System.debug(e.getMessage());
            ApexPages.addMessages(e);
        }
    }    
    
}
Prem Anandh 1Prem Anandh 1
Abu, Are facing below issue?

When you clicks on "Add Row", it's not binding the existing selected one. Correct? 

I have ran your code in my dev org and its working as expected apart from above issue. 

If this is only an issue, Please remove "Immediate = true" attribute from "Add Row" button, it will not allow your data to bind your controller
This was selected as the best answer
Abu Zafar WahidAbu Zafar Wahid

Prem you're right! This was the root cause.

Thanks so much.

Prem Anandh 1Prem Anandh 1
Great!! :)