­
ForceTree.com: Dynamically Add/Remove rows from a table in visualforce

Dynamically Add/Remove rows from a table in visualforce

5:31 PM

Screenshot:


The example below illustrates the way to REMOVE any given row in a table in visualforce and ADD new rows.

The REMOVE button only removes the contact from the UI.
The REMOVED rows are deleted from the database when the "SAVE Changes" button is clicked.

To execute this example include a valid Account ID in the URL as below

https://na5.salesforce.com/apex/deleteRowsExample?id=0017000000UIvsg

VF PAGE: deleteRowsExample

<apex:page controller="deleteRowsExample" action="{!onloadmethod}">
<apex:form >
<apex:pageBlock >
<apex:commandbutton value="Add Contact" action="{!addContact}" immediate="true"/>
<apex:commandButton value="Save Changes" action="{!saveChanges}"/>
<!-- Display some account information -->
<apex:pageBlockSection columns="1">
<apex:outputField value="{!accountRec.Name}"/>
<apex:outputField value="{!accountRec.OwnerId}"/>
</apex:pageBlockSection>
<!-- Display the related contacts for the Account -->
<apex:pageblocktable value="{!Contacts}" var="con"> <apex:variable value="{!0}" var="cnt"/>
<apex:column headervalue="Action">
<apex:commandlink value="Remove" action="{!removeContact}" immediate="true">
<!-- Pass the row number to the controller so that we know which row to remove -->
<apex:param name="index" value="{!cnt}"/>
</apex:commandlink>
<apex:variable var="cnt" value="{!cnt+1}"/>
</apex:column>
<apex:column headerValue="Last Name">
<apex:inputfield value="{!con.LastName}"/>
</apex:column>
</apex:pageblocktable>
</apex:pageBlock>
Apex Controller: deleteRowsExample

public class deleteRowsExample {
//Variable to hold all accounts added/edited
public List<Contact> allContactList = new List<Contact>();
//Variable to hold add contacts to be deleted
public List<Contact> deleteContactList = new List<Contact>();
//Variable to hold the Contact record
public Account accountRec {get;set;}
//Called when the page loads initially from the "action" method on the apex:page. Populates the Account record and the releated contact list
public void onloadmethod(){
String aid =System.currentPageReference().getParameters().get('id');
accountRec = [Select Id,OwnerId,Name,Type,Phone,Website from Account where Id=:aid];
allContactList = [Select Id,FirstName,LastName,Email from Contact where AccountId=:accountRec.Id];
}
//Send the list of contacts to the visualforce page
public List<Contact> getContacts(){
return allContactList;
}
//Add a temporary contact to the table. Not saved to the database
public void addContact(){
Contact c = new Contact();
allContactList.add(c);
}
//Remove a contact from the table.
public void removeContact(){
Integer indexVal = Integer.valueof(system.currentpagereference().getparameters().get('index'));
//If the contact is an existing contact then add it to the list to delete from the databse
if(allContactList[indexVal - 1].Id != null)
deleteContactList.add(allContactList[indexVal - 1]);
//Remove the contact from the table
allContactList.remove(indexVal - 1);
}
public void saveChanges(){
//update existing contacts and insert new ones
upsert allContactList;
//delete the contacts that were removed
if(deleteContactList.size() > 0)
delete deleteContactList;
}
}

4 comments

  1. I think its better to check if deleteContactList is not null too before checking the size, if deleteContactList is null it can throw null pointer exception on size

    ReplyDelete
    Replies
    1. Not really, DML on null lists is allowed.

      Delete
    2. It can't be null, the deleteContactList list is initialised at the top.

      Delete
  2. Thanks for this! As others suggested, a query string of ?id=[ACCOUNT-ID] needs to be added to the URL.

    ReplyDelete