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.act=lstaccount[i];
        objwrapper.isEdit = false;
       objwrapper.rowNo = lst.size();
       lst.add(objwrapper);
     }
     
  }
 public List<wrapper> lst{get;set;}
 public pagereference editmethid(){
     if(lst.size()>rowIndex){
         lst[rowIndex].isEdit = true;
     }
     return null;
 }

  public pagereference savemethid(){
     if(lst.size()>rowIndex){
         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>
      <apex:commandlink value="Save" rerender="thePB" rendered="{!a.isEdit}" action="{!savemethid}">
         <apex:param name="rowNumber" value="{!a.rowNo}" assignTo="{!rowIndex}"/>
      </apex:commandLink>        
  </apex:column>
  <apex:column HeaderValue="Name">
  <apex:outputField value="{!a.act.name}" rendered="{!NOT(a.isEdit)}"/>
  <apex:inputField value="{!a.act.name}" rendered="{!a.isEdit}"/>
  </apex:column>
  <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>
  <apex:column HeaderValue="Industry">
  <apex:outputField value="{!a.act.industry}" rendered="{!NOT(a.isEdit)}" />
  <apex:inputField value="{!a.act.industry}" rendered="{!a.isEdit}"/>
  </apex:column>
  </apex:pageblocktable>
  </apex:pageBlock>
  </apex:form>
 
</apex:page>

==============
Output:
============

No comments:

Post a Comment