Monday 25 July 2011

Adding Rows Dynamically to Page block Table

Now i am going to explain a simple example,i.e how we can add rows dynamically to Pageblocktable,how to save records.
Screen:-
   <apex:page controller="multiAccountInsert">
<apex:form >
<apex:sectionHeader title="flexandsalesforceblog" subtitle="AddRows" help="Http://flexandsalesforce.blogspot.com"/>
   <apex:pageBlock title="Add row Dynamically" >
     <apex:pageBlockButtons location="bottom">
         <apex:commandButton value="Save" action="{!save_close}" rerender="error"/>
       </apex:pageBlockButtons>
       <Div align="right">
             <apex:inputText value="{!num}" style="width:45px"/>
             <apex:commandButton value="Add rows" action="{!add_rows}"/> 
       </Div>
       <br/>
      <apex:pageBlockTable value="{!accts}" var="a" id="table">
         <apex:column headerValue="Name">
               <apex:inputField value="{!a.Name}"/>
          </apex:column>
          <apex:column headerValue="phone">
             <apex:inputField value="{!a.phone}"/>
         </apex:column>
         <apex:column headerValue="website">
             <apex:inputField value="{!a.website}"/>
         </apex:column>
       </apex:pageBlockTable>
   </apex:pageBlock>
    </apex:form>
</apex:page>


AddrowController:-



public class addrowCon

  public integer num { get; set; }
  public List<Account> accts {get; set;} 
  public addrowCon()
  {
     accts = new List<Account>(); 
     accts.add(new Account());    
  }
   public PageReference add_rows() {
    for(integer i=0;i<num ;i++)
        {
          accts.add(new Account());    
        }
        
        return null;
     }
  public PageReference save_close()
  {       insert accts;
     PageReference home = new PageReference('/home/home.jsp');
     home.setRedirect(true);
     return home;
  }
}

No comments:

Post a Comment