ads

Thursday, 11 August 2016

Add Dynamic Rows To Save Multipel Records


Controller Code :-


public class  AccountRowAdd
{
    public List<Account> AccountList {get;set;}
    public List<Account> AccountAddList {get;set;}
    public String AccountName {get;set;}
    public Integer rowNumber{get;set;}
    
    public AccountRowAdd ()
    {
        String sql = 'SELECT Name, Industry,Website FROM Account';
        AccountList = Database.Query(sql);
        AccountAddList = new List<Account>();
        AccountAddList.add(new Account());
    }
        
    public void AddRowAccount()
    {
        AccountAddList.add(new Account());
    }
    
    public void delRowAccount()
    {
        rowNumber= Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        AccountAddList .remove(rowNumber);   
    }    
    
    public void saveMembAccount()
    {
        insert AccountAddList;
    }
}


Visualforce Page :-


<apex:page showChat="false" wizard="true" sidebar="false" controller="AccountRowAdd" >
<!-- Javascript -->
<script type = "text/javascript">
    function windowClose()
    {
        self.close();
    }
</script>
<!-- End of Javascript-->
<apex:form >
 <apex:pageBlock id="membAddAccount" title="Enter Account Information">   
    <apex:variable var="rowNumAccount" value="{!0}"  />               
        <apex:pageblockSection >
            <apex:pageBlockTable value="{!AccountAddList}" var="Acn">
                <apex:facet name="footer">
                    <apex:commandLink value="Add Row" action="{!addRowAccount}" reRender="membAddAccount"/>
                </apex:facet>
                <apex:column headerValue="No." style="width:20px; text-align:center;" headerClass="centertext">
                    <apex:outputText value="{0,number, ###}" style="text-align:center;">   
                        <apex:param value="{!rowNumAccount+1}" />   
                    </apex:outputText>
                </apex:column>            
                <apex:column headerValue="Account Name">
                    <apex:inputField value="{!Acn.Name}"/>
                </apex:column>
                <apex:column headerValue="Account Industry">
                    <apex:inputField value="{!Acn.Industry}"/>
                </apex:column>
                <apex:column headerValue="Website">
                    <apex:inputField value="{!Acn.Website}"/>
                </apex:column>
                <apex:column headerValue="Delete" >
                    <apex:commandLink style="font-size:15px; font-weight:bold; text-align:center;color:brown;" value="X" action="{!delRowAccount}" reRender="membAddAccount,temp" rendered="{!rowNumAccount>0}">
                        <apex:param value="{!rowNumAccount}" name="index" />
                    </apex:commandLink>
                    <apex:variable var="rowNumAccount" value="{!rowNumAccount+1}"/>
                </apex:column>                
            </apex:pageBlockTable>                    
        </apex:pageblockSection>        
        <apex:pageblockSection columns="1" >
            <apex:pageblockSectionItem >
                <apex:commandButton value="Save" action="{!saveMembAccount}" onComplete="windowClose();"/>
                <apex:commandButton value="Cancel" onclick="windowClose();" />
            </apex:pageblockSectionItem>         
        </apex:pageblockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

Output Screen 1 :-




Output Screen 2 :-



Output Screen 3 :-


No comments:

Post a Comment