Many times we come across a scenario where we need to dynamically add new rows to insert new record into the object. Often we try to give pop up to enter new details. However we can also add new empty row dynamically with the help of wrapper classes.
Here is an example which I've developed to overcome above limitation of creating a pop up.
Here is an example which I've developed to overcome above limitation of creating a pop up.
Visualforce Page:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--************************************************************************ | |
> File Name: creatingListOfRecords.page | |
> Author: Rohit Mourya | |
> Blog: https://mouryarohit.blogspot.in/ | |
> Created Time: Mon April 24 2017 | |
************************************************************************--> | |
<apex:page controller="creatingListOfRecordsController"> | |
<apex:form > | |
<apex:pageBlock title="Creating List Of Expense Records" id="block"> | |
<apex:pageMessages ></apex:pageMessages> | |
<apex:pageBlockButtons location="top"> | |
<apex:commandButton value="Add Row" action="{!addRow}" reRender="table" immediate="true"/> | |
<apex:commandButton value="Save" action="{!saving}" /> | |
</apex:pageBlockButtons> | |
<apex:pageBlockTable value="{!expenseWrapperList}" var="page" id="table"> | |
<apex:column headerValue="Name"> | |
<apex:inputField value="{!page.expense.name}"/> | |
</apex:column> | |
<apex:column headerValue="Amount"> | |
<apex:inputField value="{!page.expense.Amount__c}" /> | |
</apex:column> | |
<apex:column headerValue="Action"> | |
<apex:commandLink value="Delete" action="{!removingRow}" immediate="true" reRender="block"> | |
<apex:param name="index" value="{!page.counterWrap}"/> | |
</apex:commandLink> | |
</apex:column> | |
</apex:pageBlockTable> | |
</apex:pageBlock> | |
</apex:form> | |
</apex:page> |
Apex Controller:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************************* | |
> File Name: creatingListOfRecordsController.cls | |
> Author: Rohit Mourya | |
> Blog: https://mouryarohit.blogspot.in/ | |
> Created Time: Mon April 24 2017 | |
************************************************************************/ | |
public with sharing class creatingListOfRecordsController { | |
public list<ExpenseWrapper> expenseWrapperList{get;set;} | |
public Integer counter{get;set;} | |
public creatingListOfRecordsController(){ | |
counter = 0; | |
expenseWrapperList = new list<ExpenseWrapper>(); | |
for(Integer i=0;i<5;i++){ | |
ExpenseWrapper expenseWrap = new ExpenseWrapper(new Expense__c()); | |
counter++; | |
expenseWrap.counterWrap = counter; | |
expenseWrapperList.add(expenseWrap); | |
} | |
} | |
public PageReference addRow(){ | |
ExpenseWrapper expenseWrap = new ExpenseWrapper(new Expense__c()); | |
counter++; | |
expenseWrap.counterWrap = counter; | |
expenseWrapperList.add(expenseWrap); | |
return null; | |
} | |
public PageReference removingRow(){ | |
Integer param = Integer.valueOf(Apexpages.currentpage().getParameters().get('index')); | |
for(Integer i=0;i<expenseWrapperList.size();i++){ | |
if(expenseWrapperList[i].counterWrap == param ){ | |
expenseWrapperList.remove(i); | |
} | |
} | |
counter--; | |
return null; | |
} | |
public PageReference saving(){ | |
list<Expense__c> updateExpenseList = new list<Expense__c>(); | |
if(!expenseWrapperList.isEmpty()){ | |
for(ExpenseWrapper expenseWrap: expenseWrapperList){ | |
updateExpenseList .add(expenseWrap.expense); | |
} | |
} | |
if(!updateExpenseList .isEmpty()){ | |
upsert updateExpenseList ; | |
} | |
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Info,'Record Saved Successfully.'); | |
ApexPages.addMessage(myMsg); | |
return null; | |
} | |
public class ExpenseWrapper{ | |
public Expense__c expense{get;set;} | |
public Integer counterWrap{get;set;} | |
public ExpenseWrapper(Expense__c expense){ | |
this.expense= expense; | |
} | |
} | |
} |
No comments:
Post a Comment