Adding rows dynamically in pageblock table in Visualforce page.
Hi,
In this post i am going to give sample example of how to add a row dynamically in pageblock table and how to remove the row from pageblock table dynamically.
For this i am developed one Visualforce page, One Controller and One Helper class. Initially i am loading the page with one row then after once the user clicks on AddRow button it is going to add
one new row to that pagblock table. If you want to remove a particular row you can remove using Remove row link.
Visualforce Page:
<apex:page controller="AddMultipleAccountCLS">
<apex:form id="theForm">
<apex:pageblock id="thePB" title="Creating Multiple Accounts">
<apex:pageblockButtons >
<apex:commandButton value="Save" action="{!SaveMultipleAccounts}"/>
</apex:pageblockButtons>
<apex:outputPanel id="accountHead">
<apex:variable value="{!0}" var="rowNum"/>
<apex:pageBlockSection columns="1" title="Adding Multiple Accounts" id="thePbs" collapsible="False">
<apex:pageBlockTable value="{!waAccList}" var="eachRecord">
<apex:column headerValue="Action">
<apex:commandLink value="Remove" style="color:red" action="{!removeRowFromAccList}" 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="Account Name">
<apex:inputField value="{!eachRecord.record.Name}" required="true"/>
</apex:column>
<apex:column headerValue="Account Number">
<apex:inputField value="{!eachRecord.record.AccountNumber}" required="true"/>
</apex:column>
<apex:column headerValue="Account Type">
<apex:inputfield value="{!eachRecord.record.Type}" required="true"/>
</apex:column>
<apex:column headerValue="Annual Revenue">
<apex:inputField value="{!eachRecord.record.AnnualRevenue}" required="true"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
<apex:commandButton value="Add More" action="{!addNewRowToAccList}" rerender="accountHead" Status="status" immediate="true" />
</apex:outputPanel>
</apex:pageblock>
</apex:form>
</apex:page>
Controller Class:
public with sharing class AddMultipleAccountCLS {
public PageReference SaveMultipleAccounts() {
system.debug('controller save method is calling-->');
AddMultipleAccountHelperCLS.save(waAccList);
return null;
}
public List<WrapperpaAccountList> waAccList {get;set;}
public Integer rowToRemove {get;set;}
public AddMultipleAccountCLS(){
waAccList = new List<WrapperpaAccountList>();
addNewRowToAccList();
}
public void removeRowFromAccList(){
waAccList = AddMultipleAccountHelperCLS.removeRowToAccountList(rowToRemove, waAccList);
}
public void addNewRowToAccList(){
waAccList = AddMultipleAccountHelperCLS.addNewRowToAccList(waAccList);
}
public class WrapperpaAccountList{
public Integer index {get;set;}
public Account record {get;set;}
}
}
Controller Helper Class:
public class AddMultipleAccountHelperCLS {
public static List<AddMultipleAccountCLS.WrapperpaAccountList> addNewRowToAccList(List<AddMultipleAccountCLS.WrapperpaAccountList> waAccObjList){
AddMultipleAccountCLS.WrapperpaAccountList newRecord = new AddMultipleAccountCLS.WrapperpaAccountList();
Account newAccountRecord = new Account();
newRecord.record = newAccountRecord;
newRecord.index = waAccObjList.size();
waAccObjList.add(newRecord);
return waAccObjList;
}
public static List<AddMultipleAccountCLS.WrapperpaAccountList> removeRowToAccountList(Integer rowToRemove, List<AddMultipleAccountCLS.WrapperpaAccountList> waAccountList){
waAccountList.remove(rowToRemove);
return waAccountList;
}
public static void save(List<AddMultipleAccountCLS.WrapperpaAccountList> waAccList) {
system.debug('==waAccList==>'+waAccList.size());
List<Account> accountRecordsToBeInserted = new List<Account>();
if(waAccList !=null && !waAccList.isEmpty()){
for(AddMultipleAccountCLS.WrapperpaAccountList eachRecord : waAccList ){
Account accTemp = eachRecord.record;
accountRecordsToBeInserted.add(accTemp);
}
system.debug('==accountRecordsToBeInserted==>'+accountRecordsToBeInserted.size());
insert accountRecordsToBeInserted;
}
}
}
This is about Dynamic row adding and Deleting functionality in Page Block table using Visualforce Tags.
Thanks..