+ Start a Discussion
DeveloperSudDeveloperSud 

Dynamic Add row button in pageBlockTable without Resetting the entered input values in each row while clicking add row button

Hi Guys,

I have created a pageblock table where user can dynmically add and remove rows .In each rows I am using an input field .The problem I am facing while clicking the add row button its resetting the previous value entered in the previous row.Kindly help if anyone else faced the same problem .
<apex:page standardController="Account" extensions="AddnRemoveController" >
<apex:form id="f" >
<apex:pageMessages ></apex:pageMessages>
<div>
 <apex:pageBlock mode="maindetail" >
 <!-- <apex:variable value="{!0}" var="rowNumber" /> -->
  <apex:commandButton value="AddRow" action="{!addrow}" reRender="table">
  </apex:commandButton>
  <apex:commandButton value="Remove Row" action="{!removerow}" reRender="table">
  <!-- <apex:param value="" name="rowNum" assignTo="{!rowNum}" /> -->
  </apex:commandButton>
  
 
  <apex:pageBlockTable value="{!wrapList}" var="wrapVar" id="table">
  <!-- <apex:variable value="{!wrapVar.index}" var="rowNumber" /> -->
   <apex:column >{!wrapVar.index}</apex:column>
   <apex:column headervalue="{!wrapVar.act.Industry}">
    <apex:inputField value="{!wrapVar.act.Industry}" id="modalBankCountry"/>
   </apex:column>
  </apex:pageBlockTable>
   <apex:commandButton value="Save" action="{!saving}" />
 </apex:pageBlock>
 </div>
 </apex:form>
</apex:page>
 
<apex:page standardController="Account" extensions="AddnRemoveController" >
<apex:form id="f" >
<apex:pageMessages ></apex:pageMessages>
<div>
 <apex:pageBlock mode="maindetail" >
 <!-- <apex:variable value="{!0}" var="rowNumber" /> -->
  <apex:commandButton value="AddRow" action="{!addrow}" reRender="table">
  </apex:commandButton>
  <apex:commandButton value="Remove Row" action="{!removerow}" reRender="table">
  <!-- <apex:param value="" name="rowNum" assignTo="{!rowNum}" /> -->
  </apex:commandButton>
  
 
  <apex:pageBlockTable value="{!wrapList}" var="wrapVar" id="table">
  <!-- <apex:variable value="{!wrapVar.index}" var="rowNumber" /> -->
   <apex:column >{!wrapVar.index}</apex:column>
   <apex:column headervalue="{!wrapVar.act.Industry}">
    <apex:inputField value="{!wrapVar.act.Industry}" id="modalBankCountry"/>
   </apex:column>
  </apex:pageBlockTable>
   <apex:commandButton value="Save" action="{!saving}" />
 </apex:pageBlock>
 </div>
 </apex:form>
</apex:page>

 
Arpit Jain7Arpit Jain7
Put your controller code also to identify the cause....
DeveloperSudDeveloperSud
Hi Arpit,

Please find this below.
public with sharing class AddnRemoveController {

   
   public List<wrapperClass> wrapList{get;set;}  
   public Integer counter{get;set;}
  
   
   public AddnRemoveController(ApexPages.StandardController controller){
   counter=0;
   wrapList= new List<wrapperClass>();
   wrapperClass wrp= new wrapperClass(new account());
   wrp.index=counter;
   wrapList.add(wrp);  
   }
   
   public pagereference addRow(){
   counter++;
   wrapperclass wrp1= new wrapperclass(new account());
   wrp1.index=counter;
   wrapList.add(wrp1);
   return null;
   }
   
   public pageReference removeRow(){
   
   
    /*
    for(Integer i=0;i<wrapList.size();i++){
            if(wrapList[i].index == rowNum ){
                wrapList.remove(i);     
            }
        }
    */
   if(wrapList.size()>1){  
   wrapList.remove(counter-1);    
   counter--; 
   }    
   return null;
   }
    public PageReference saving(){
        list<Account> updateAccountList;
        updateAccountList = new list<Account>();
        if(!wrapList.isEmpty()){
            for(wrapperClass accountWrapper:wrapList){
                updateAccountList.add(accountWrapper.act);
            }
        }
        if(!updateAccountList.isEmpty()){
            upsert updateAccountList;
        }
       ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Info,'Record Saved Successfully.');
       ApexPages.addMessage(myMsg); 
        return null;
    }
   
   public class wrapperClass{
    public account act{get;set;}
    public integer index{get;set;}
    public wrapperClass(Account a){
    act=a;
    }
   }
}

 
Arpit Jain7Arpit Jain7
Can you provide more info related to your issue with some screen shot to explain what exactly your issue is ?
DeveloperSudDeveloperSud
Let me give you he scenario : use selecting a value from the picklist value in 1st row.Then user is hitting the add row button. now one more row is appearing but it also resetting the value of the first row.
Arpit Jain7Arpit Jain7
I also tried but values were not getting reset.. Attached is screen shotUser-added image