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:
============

4 comments:

  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.
    shivanidesai20@gmail.com

    ReplyDelete
    Replies
    1. Hi Shivani,

      Can you send your code and visualforce page to my email "mbalaji105@gmail.com".
      Thanks,
      Balaji M

      Delete
  2. hello balaji sir, can you explain me rendered="{!NOT(a.isEdit)}
    thank you

    ReplyDelete
    Replies
    1. Hello,
      "isEdit" is a property in wrapper we are just using it to hide or show "Edit" and "Save" buttons based on user actions on these buttons.

      Delete

How to include a screen flow in a Lightning Web Component

 Hi, Assume  you have a flow called "Quick Contact Creation" and API Name for the same is "Quick_Contact_Creation". To i...