Add row rerender on a list with input fields looses data

Having a list in your controller that is displayed as input fields either through pageblock or repeat and you need to add another row to the list via a button.  Chances are when you enter data on any input field on any row and you hit Add Row button that has a rerender attribute that makes the ajax call, the previous entered data will be lost as per code sample below.

public class MyController {
public list<myOject__c> objList {get;set;}
public MyController() {
objList = new List<myObject__c>();
}
public void addRow(){
myObject__c newObj = new myObject__c();
objList.add(newObj);
}
}
VF:
<apex:pageblock id=”myList”>
<apex:pageblockSection value=”{!objList}” var=”obj”>
<apex:column>
<apex:inputField value=”{!obj.Name” />
</apex:column>
</apex:pageBlockSection>
<apex:commandButton value=”Add Row” action=”{!addRow}” rerender=”myList”/>
</apex:pageblock>
The simple solution is to add the attribute immediate=”false” to the commandButton tag. This forces the data to be retained.

Leave a Reply

Your email address will not be published. Required fields are marked *