Thursday, 19 December 2013

Row edit and save using wrapper class

Here in this example we are displaying the records with edit link.When we click "edit" link then it shows in editable mode there itself then we can update the record there itself.With this we can update record row wise without updating all the rows like in inlineediting.

Controller Page:
public class wrapperclass
 wrapper objwrapper;
 List<wrapper> lstwrapper=new List<wrapper>();
  public integer rowIndex{get;set;}
  public wrapperclass(){
  lst = new list<wrapper>();
     List<Account> lstaccount=[select name,industry,type from account];

     for(integer i=0;i<lstaccount.size();i++)
       objwrapper=new wrapper();
        objwrapper.isEdit = false;
       objwrapper.rowNo = lst.size();
 public List<wrapper> lst{get;set;}
 public pagereference editmethid(){
         lst[rowIndex].isEdit = true;
     return null;

  public pagereference savemethid(){
         update lst[rowIndex].act;  
         lst[rowIndex].isEdit = false;
     return null;

 public class wrapper{
 public Account act{get;set;}
 public integer rowNo {get;set;}
 public boolean isEdit{get;set;}

Page Code:
<apex:page controller="wrapperclass">
  <apex:form >
  <apex:pageBlock id="thePB">
  <apex:pageblocktable value="{!lst}" var="a">
  <apex:column headerValue="Action">
      <apex:commandlink value="Edit" rerender="thePB" rendered="{!NOT(a.isEdit)}" action="{!editmethid}">
          <apex:param name="rowNumber" value="{!a.rowNo}" assignTo="{!rowIndex}"/>
      <apex:commandlink value="Save" rerender="thePB" rendered="{!a.isEdit}" action="{!savemethid}">
         <apex:param name="rowNumber" value="{!a.rowNo}" assignTo="{!rowIndex}"/>
  <apex:column HeaderValue="Name">
  <apex:outputField value="{!}" rendered="{!NOT(a.isEdit)}"/>
  <apex:inputField value="{!}" rendered="{!a.isEdit}"/>
  <apex:column HeaderValue="Type">
  <apex:outputField value="{!a.act.type}" rendered="{!NOT(a.isEdit)}"/>
  <apex:inputField value="{!a.act.type}" rendered="{!a.isEdit}"/>
  <apex:column HeaderValue="Industry">
  <apex:outputField value="{!a.act.industry}" rendered="{!NOT(a.isEdit)}" />
  <apex:inputField value="{!a.act.industry}" rendered="{!a.isEdit}"/>



  1. Hi Balaji,

    Am trying to use a custom controller to parse JSON and render two tables in my visualforce using two wrapper classes being returned. My first table renders successfully but I fail miserably when trying to render the second. Can you please help me understand where I am failing. I am still a novice at coding and struggling with this.

    1. Hi Shivani,

      Can you send your code and visualforce page to my email "".
      Balaji M