Select and Deselect Check Box on Page Block Table in Visual force Using JavaScript
Requirement: Using javascript select all and Deselect all checkbox in pageBlock Table
Solution
<apex:page sidebar="false" Controller="ManageOppController">
<script>
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:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value="{!lstofwrapperClas}" var="ObjWrap">
<apex:column headerValue="Select">
<apex:facet name="header">
<apex:inputCheckbox id="cb" onclick="selectAllCheckboxes(this,'inputId')"/>
</apex:facet>
<apex:inputCheckbox value="{!ObjWrap.isSelected }" id="inputId"/>
</apex:column>
<apex:column headerValue="Account Name" value="{!ObjWrap.objAcc.Name}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
<apex:commandButton value="Save Record" action="{!SaveRecord}"/>
</apex:pageBlock>
</apex:form>
</apex:page>
Class Name:
public class ManageOppController {
// @description declare the variables
public string OppID{get;set;}
public set<ID> setofAccID;
public List<WrapperClass> lstofwrapperClas{get;set;}
public List<Manage_Opportunity__c > insertlstofMO;
public set<ID> setofExAccID;
// Call the constructor
public ManageOppController()
{
init();
}
public void init()
{
lstofwrapperClas = new List<WrapperClass>();
setofAccID = new set<ID>();
insertlstofMO = new List<Manage_Opportunity__c >();
setofExAccID = new set<ID>();
OppID = ApexPages.currentPage().getParameters().get('ID');
for(Manage_Opportunity__c obj: [Select ID,Opportunity__c,Account__c from Manage_Opportunity__c where Opportunity__c=: OppID ])
{
setofExAccID.add(obj.Account__c);
}
if(OppID != null)
{
for(Opportunity oppobj:[Select ID, AccountID from opportunity where ID =:OppID limit 1])
{
setofAccID.add(oppobj.AccountID );
}
}
if(setofAccID != null && setofAccID.size()>0)
{
for(Account acc: [Select ID,Name from Account where ParentID IN:setofAccID ])
{
if(!setofExAccID.contains(acc.ID))
{
WrapperClass obj = new WrapperClass();
obj.objAcc = acc;
obj.isSelected = false;
lstofwrapperClas.add(obj);
}
}
}
}
// Save the Record
public PageReference SaveRecord()
{
if(lstofwrapperClas != null && lstofwrapperClas.size()>0)
{
for(WrapperClass objWC :lstofwrapperClas)
{
if(objWC.isSelected == true)
{
Manage_Opportunity__c objOP = new Manage_Opportunity__c();
objOP.Opportunity__c = OppID ;
objOP.Account__c = objWC.objAcc.ID;
insertlstofMO .add(objOP);
}
}
}
if(insertlstofMO !=null && insertlstofMO .size()>0)
{
insert insertlstofMO ;
PageReference oppPage = new PageReference('/' +OppID );
oppPage.setRedirect(true);
return oppPage ;
}
return null;
}
// Create Wrapper Class
public class WrapperClass
{
Public Account objAcc{get;set;}
Public Boolean isSelected{get;set;}
public WrapperClass()
{
objAcc = new Account();
isSelected = false;
}
}
}
Thanks
Sumit Shukla
sumitshukla.mca@gmail.com
Comments
Post a Comment