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
Harsha ShriHarsha Shri 

Help in Select all functionality for check box

Hi All,
I have written code for check box select all functionality. It is working, but is having one issue please help me. I am sharing my code.
public class Mainclass{
 public WrapperClass viewWrapper {get;set;}
public void SelectAll(){

    
        if(viewwrapper.check){
            for(WrapperClass.SearchItem s : viewWrapper.resultList)
            {
                s.checked = true;
            }
        }
        else
        {
            for(WrapperClass.SearchItem s : viewWrapper.resultList)
            {
                s.checked = false;
            }
            
        }        
       
    }
}
 
public class WrapperClass 
{
 public Boolean check{get;set;}
  public List<SearchItem> resultList{ get; set; }
  public class SearchItem {
    public Boolean checked{ get; set; }
	\\ some other fields are also there
	
	}
	}
 
<apex:page>
<apex:form>
<apex:coloumn>
<apex:facet>
<apex:inputCheckbox style="margin:2px;" value="{!viewWrapper.check}">
                            <apex:actionSupport action="{!selectAll}" event="onclick" reRender="resultsBlock"/></apex:inputCheckbox>
                        </apex:facet>
</apex:coloumn>
</apex:form>
</apex:page>

Here if I check main check box, it is selecting all checkboxes under it. If I am unselecting all, it is unselecting all.
But issue is, If I unselect any check box from result check box main checkbox is not getting unselected. the below image is reference. 1st check is main check box. when I deselect 3rd checkbox the 1st one should uncheck. right now it is not happening. Please help me

User-added image
Thanks in Advance
Best Answer chosen by Harsha Shri
Satish PrajapatSatish Prajapat

Hi harsha Shri,

Its easy to achieve required logic,
please check some help below :
 

<apex:pageBlocktable value="{!viewWrapper.resultList}" var="item">
            <apex:column>
                    <apex:inputCheckbox style="margin:2px;" value="{!item.checked}">
                            <apex:actionSupport action="{!apexLogic}" event="onclick" reRender="formId"/>
                    </apex:inputCheckbox>
            <apex:column>
</apex:pageBlocktable>
 
public void apexLogin()
{
    Boolean allTrue = true;
    for(WrapperClass.SearchItem s : viewWrapper.resultList)
    {
        if(s.checked = true)
        {
            //if all checkbox is true make main checkbox true otherwise make it false.
            continue;
        }
        else
        {
            allTrue = false;
            break;
        }
    }
    if(allTrue == true)
    {
        viewwrapper.check = true;
    }
    else
    {
        viewwrapper.check = false;
    }
}
Dear, If it helps you than let me know otherwise tell me if problem still exist.
Thanks,
Satish.

All Answers

Satish PrajapatSatish Prajapat

Hello Harsha,

When look it into your VF page code than Its looks like it not full code,

Please share all code than we can understand more about problem.

And in VF page single checkbox code is shown, and in image you shown more than one checkbox.

please share more about your problem, So, Good people can help you.

Thanks,

Satish. :-)

Harsha ShriHarsha Shri

HI Satish,

the 1st checkbox will be used for select all buttom. below checkboxes are results of records from wrapper list. 

Satish PrajapatSatish Prajapat

Hi harsha Shri,

Its easy to achieve required logic,
please check some help below :
 

<apex:pageBlocktable value="{!viewWrapper.resultList}" var="item">
            <apex:column>
                    <apex:inputCheckbox style="margin:2px;" value="{!item.checked}">
                            <apex:actionSupport action="{!apexLogic}" event="onclick" reRender="formId"/>
                    </apex:inputCheckbox>
            <apex:column>
</apex:pageBlocktable>
 
public void apexLogin()
{
    Boolean allTrue = true;
    for(WrapperClass.SearchItem s : viewWrapper.resultList)
    {
        if(s.checked = true)
        {
            //if all checkbox is true make main checkbox true otherwise make it false.
            continue;
        }
        else
        {
            allTrue = false;
            break;
        }
    }
    if(allTrue == true)
    {
        viewwrapper.check = true;
    }
    else
    {
        viewwrapper.check = false;
    }
}
Dear, If it helps you than let me know otherwise tell me if problem still exist.
Thanks,
Satish.
This was selected as the best answer
Satish PrajapatSatish Prajapat

Hi harsha Shri,

please correct Line #6
 if(s.checked == true)