Tuesday, November 15, 2016

VisualForce Page to Dynamically Add and Delete Records as a Rows in the Page.


Page:

<apex:page controller="Addrowscontroller">
<apex:form >
    <apex:pageBlock id="pb">
    <apex:variable var="rowNumber" value="{!0}"/> 
        <apex:pageBlockTable value="{!listaccount}" var="acc">
            <apex:column headerValue="Account Name">
            <apex:inputField value="{!acc.Name}"/>
            </apex:column>
            <apex:column headerValue="Account Number">
            <apex:inputField value="{!acc.AccountNumber}"/>
            </apex:column>
            <apex:column headerValue="Account Type">
            <apex:inputField value="{!acc.Type}"/>
            </apex:column>
            <apex:column headerValue="Industry">
            <apex:inputField value="{!acc.Industry}"/>
            </apex:column>
            <apex:column headerValue="Action" >
            <apex:commandButton value="Delete" action="{!deleteRow}" reRender="pb">
            <apex:param name="rowIndex" value="{!rowNumber}"/>
            </apex:commandButton>
            <apex:variable var="rowNumber" value="{!rowNumber}"/>
            </apex:column>
        </apex:pageBlockTable>
        <apex:pageBlockButtons >
            <apex:commandButton value="Add Account Row" action="{!addAccount}"/>
            <apex:commandButton value="Save Accounts" action="{!saveAccount}"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>
</apex:page>

Controller:

public class Addrowscontroller{

  Account account =new Account();
  Public list<account> listAccount {get;set;}
  public Integer rowIndex {get;set;}
  public List<account> delattendeeList =new List<account>();
  public account del;
  
  public Addrowscontroller(){
      listAccount = new list<Account>();
      listAccount.add(Account);
  }
  public void addAccount(){
      Account acc = new Account();
      listAccount.add(acc);
  }
  public pagereference saveAccount(){
      Insert listAccount;
      return null;
  }
  public PageReference deleteRow(){
    rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
    del = listAccount.remove(rowIndex);
    delattendeeList.add(del);
    return null;
  }
}

No comments:

Post a Comment