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
AbAb 

Select All check box for table and Saving them

Hello,

I have a PageBlockTable, with few columns, one of them being Checkbox. I want to have a feature like "select/deselect Al"l and Save All.
User-added image

User-added image
 
<apex:page Controller="GestionDeProduit" sidebar="false" showHeader="false" >
    <script type="text/javascript" language="javascript">  
    function checkJS(id1,id2)
    {        
        var v1 ='';
        var v2 ='';
        if(document.getElementById(id1) != null){
            v1 = document.getElementById(id1).value;
        }     
        if(document.getElementById(id2) != null){
            v2 = document.getElementById(id2).value;
        }
        checkAF(v1,v2); 
    }     
    </script> 
    <apex:form id="products">  
        <apex:actionFunction name="checkAF" action="{!updateRow}" reRender="panelRefresh">
            <apex:param name="check1" value="" />
            <apex:param name="check2" value="" />
        </apex:actionFunction>         
        <apex:outputPanel id="panelRefresh" >
            <apex:outputPanel >
                <apex:pageBlock >
                    <apex:pageBlockTable value="{!productList}" var="item">
                        <apex:column headerValue="Products" >
                            <apex:outputText value="{!item.Product__r.Name}"/>
                        </apex:column>
             
                        <apex:column headerValue="Disponibilite" >
                            <apex:inputField value="{!item.Availability__c }" id="availabilityId" onchange="checkJS('{!$Component.availabilityId}','{!$Component.primaryId}');"/>
                            <apex:inputHidden value="{!item.Name }" id="primaryId"/>
                        </apex:column>                          
                    </apex:pageBlockTable> 
                </apex:pageBlock>
            </apex:outputPanel>
        </apex:outputPanel>
    </apex:form>
</apex:page>

public with sharing class GestionDeProduit {
    transient public List<Product_Availability__c> productList{get;set;}
    
    public GestionDeProduit(){
        init();
    }
    
    public void init(){
        try{
              productList = [SELECT  Name, Availability__c
                               FROM Product_Availability__c];   
        }Catch(QueryException de ){
            System.debug(de);
        } 
    }
    
    public void updateRow(){
        String temp2 = System.currentPageReference().getParameters().get('check2');
        List<Product_Availability__c> pList = new List<Product_Availability__c>();
        try{
            pList = [SELECT  Name, Availability__c
                     FROM Product_Availability__c WHERE Name =:temp2];
        }Catch(QueryException de ){
            System.debug(de);
        }               
        if(pList.size()>0){
            if(pList[0].Availability__c == true){
                pList[0].Availability__c = false;
            }else if(pList[0].Availability__c == false){
                pList[0].Availability__c = true;
            }
            try{
                update pList[0];
            }Catch(DMLException de ){
                System.debug(de);
            }              
        }
        init();         
    }    
    
 
}

Thank you
Best Answer chosen by Ab
Amit Chaudhary 8Amit Chaudhary 8
Please check below post i hope that will help you
http://www.sfdcpoint.com/salesforce/select-all-checkbox-using-javascript-in-visualforce-page/

You need to add below java script code.
<script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");                  
            for(var i=0; i<inputCheckBox.length; i++){          
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){                                     
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
 
<apex:column >
                    <apex:facet name="header">
                        <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                    </apex:facet>
                    <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                </apex:column>

Please let us know if this will help u

Thanks,
Amit Chaudhary

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post i hope that will help you
http://www.sfdcpoint.com/salesforce/select-all-checkbox-using-javascript-in-visualforce-page/

You need to add below java script code.
<script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");                  
            for(var i=0; i<inputCheckBox.length; i++){          
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){                                     
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
 
<apex:column >
                    <apex:facet name="header">
                        <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                    </apex:facet>
                    <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                </apex:column>

Please let us know if this will help u

Thanks,
Amit Chaudhary
This was selected as the best answer
AbAb
Sorry Amit, i bymistakenly clciked dislike and i cannot undo it. aplologies
Nizar SalehNizar Saleh
I've tried to add the source code and selected all function still not working.
Can you help me, please?
Naveen MosuruNaveen Mosuru
Hello Amit Chaudhary 8, (#)
I have used your code and its working fine, but I want to achieve this without using javascript because I want to convert it into lightning. So it is not possible if I use javascript. So I wish if you have any alternatives suggestions to achieve this.

Thanks,
Naveen