Add and Delete Row functionality using wrapper class
Page Code
<apex:page controller="AddandDeleteController">
<apex:form >
<apex:pageBlock title="Account Records">
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockButtons location="top">
<apex:commandButton value="Add Row" action="{!addRow}" reRender="table" immediate="true"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!accountwrapperList}" var="page" id="table">
<apex:column headerValue="Name">
<apex:inputField value="{!page.account.name}"/>
</apex:column>
<apex:column headerValue="Phone">
<apex:inputField value="{!page.account.Phone}" />
</apex:column>
<apex:column headerValue="Action">
<apex:commandLink value="Remove" action="{!removingRow}" immediate="true">
<apex:param name="index" value="{!page.counterWrap}"/>
</apex:commandLink>
</apex:column>
</apex:pageBlockTable>
<apex:commandButton value="Save" action="{!saveRecords}" />
</apex:pageBlock>
</apex:form>
</apex:page>
Class Code
public with sharing class AddandDeleteController {
public list<Account> lstofAccount{get;set;}
public list<Accountwrapper> accountwrapperList{get;set;}
public Integer counter{get;set;}
public AddandDeleteController(){
counter = 0;
lstofAccount = new list<Account>();
accountwrapperList = new list<Accountwrapper>();
for(Integer i=0;i<1;i++){
Accountwrapper actWrap = new Accountwrapper(new Account());
counter++;
actWrap.counterWrap = counter;
accountwrapperList.add(actWrap);
}
}
public PageReference addRow(){
//lstofAccount.add(new Account());
Accountwrapper actWrap = new Accountwrapper(new Account());
counter++;
actWrap.counterWrap = counter;
accountwrapperList.add(actWrap);
return null;
}
public PageReference removingRow(){
Integer param = Integer.valueOf(Apexpages.currentpage().getParameters().get('index'));
for(Integer i=0;i<accountwrapperList.size();i++){
if(accountwrapperList[i].counterWrap == param ){
accountwrapperList.remove(i);
}
}
counter--;
return null;
}
public PageReference saveRecords(){
list<Account> updatelstofAccount;
updatelstofAccount = new list<Account>();
if(!accountwrapperList.isEmpty()){
for(Accountwrapper accountWrapper:accountwrapperList){
updatelstofAccount.add(accountWrapper.account);
}
}
if(!updatelstofAccount.isEmpty()){
upsert updatelstofAccount;
}
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Info,'Record Saved Successfully.');
ApexPages.addMessage(myMsg);
return null;
}
public class Accountwrapper{
public Account account{get;set;}
public Integer counterWrap{get;set;}
public Accountwrapper(Account act){
this.account = act;
}
}
}
Snap
<apex:page controller="AddandDeleteController">
<apex:form >
<apex:pageBlock title="Account Records">
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockButtons location="top">
<apex:commandButton value="Add Row" action="{!addRow}" reRender="table" immediate="true"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!accountwrapperList}" var="page" id="table">
<apex:column headerValue="Name">
<apex:inputField value="{!page.account.name}"/>
</apex:column>
<apex:column headerValue="Phone">
<apex:inputField value="{!page.account.Phone}" />
</apex:column>
<apex:column headerValue="Action">
<apex:commandLink value="Remove" action="{!removingRow}" immediate="true">
<apex:param name="index" value="{!page.counterWrap}"/>
</apex:commandLink>
</apex:column>
</apex:pageBlockTable>
<apex:commandButton value="Save" action="{!saveRecords}" />
</apex:pageBlock>
</apex:form>
</apex:page>
Class Code
public with sharing class AddandDeleteController {
public list<Account> lstofAccount{get;set;}
public list<Accountwrapper> accountwrapperList{get;set;}
public Integer counter{get;set;}
public AddandDeleteController(){
counter = 0;
lstofAccount = new list<Account>();
accountwrapperList = new list<Accountwrapper>();
for(Integer i=0;i<1;i++){
Accountwrapper actWrap = new Accountwrapper(new Account());
counter++;
actWrap.counterWrap = counter;
accountwrapperList.add(actWrap);
}
}
public PageReference addRow(){
//lstofAccount.add(new Account());
Accountwrapper actWrap = new Accountwrapper(new Account());
counter++;
actWrap.counterWrap = counter;
accountwrapperList.add(actWrap);
return null;
}
public PageReference removingRow(){
Integer param = Integer.valueOf(Apexpages.currentpage().getParameters().get('index'));
for(Integer i=0;i<accountwrapperList.size();i++){
if(accountwrapperList[i].counterWrap == param ){
accountwrapperList.remove(i);
}
}
counter--;
return null;
}
public PageReference saveRecords(){
list<Account> updatelstofAccount;
updatelstofAccount = new list<Account>();
if(!accountwrapperList.isEmpty()){
for(Accountwrapper accountWrapper:accountwrapperList){
updatelstofAccount.add(accountWrapper.account);
}
}
if(!updatelstofAccount.isEmpty()){
upsert updatelstofAccount;
}
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Info,'Record Saved Successfully.');
ApexPages.addMessage(myMsg);
return null;
}
public class Accountwrapper{
public Account account{get;set;}
public Integer counterWrap{get;set;}
public Accountwrapper(Account act){
this.account = act;
}
}
}
Snap
Thanks
Sumit Shukla
sumitshukla.mca@gmail.com
Comments
Post a Comment