Wednesday, December 31, 2014

Adding and Deleting rows dynamically in pageblock table in Visualforce page.

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..












6 comments:

Sohit said...

very nice...I have requirement here. How would you handle dynamic calculation of total amount on a field on client side. Lets says a, b, c are fields where a= 5 and b = 5 and c should dynamically update to multiple of these c = 25.. should we use Javascript.

I should not loose value of C when I dynamically add row and then click save button to save values . thanks

Unknown said...

Not sure what I'm doing wrong but waAccObjList is always null so getting attempt to de-refernce error

MUDASAR HANIF said...

u beauty <3

Unknown said...

Many thanks for your helpful blog post, I was able to use this in my one requiremnt with minor tweeks :), Can you provide the test class for the same as well, or point me to some link that might be helpful to learn how to write test classes of extension classes.
Many Thanks ,
Sakshi

Anonymous said...

Incorrect parameter type for function 'not()'. Expected Boolean, received Object
I am getting error

nick jones said...

really helpful
CISCO Wireless Access Points

Post a Comment

 
| ,