Dynamically adding rows in apex pageBlock

Visualforce code:

<apex:page sidebar="false" controller="MemberPopup" >
<apex:form >
    <apex:pageBlock id="membAdd" >                 
        <apex:pageblockSection >
            <apex:pageBlockTable value="{!memberAddList}" var="memb">
                <apex:column headerValue="Member Name">
                    <apex:inputField value="{!memb.Name}"/>
                </apex:column>
                <apex:column headerValue="Mobile Number">
                    <apex:inputField value="{!memb.Mobile_Number__c}"/>
                </apex:column>
                <apex:column headerValue="eMail Id">
                    <apex:inputField value="{!memb.E_Mail_Id__c}"/>
                </apex:column>
            </apex:pageBlockTable>
            <br/><apex:commandLink value="Add Row" action="{!addRow}" reRender="membAdd"/>       
        </apex:pageblockSection>       
        <apex:pageblockSection columns="1" >
            <apex:pageblockSectionItem >
                <apex:commandButton value="Save" />
                <apex:commandButton value="Cancel" />
            </apex:pageblockSectionItem>        
        </apex:pageblockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

Apex Code:

public class MemberPopup
{
    public List<Member__c> memberList {get;set;}
    public List<Member__c> memberAddList {get;set;}
    public String memberName {get;set;}  
    
    public MemberPopup()
    {
        String sql = 'SELECT Name, E_Mail_Id__c FROM Member__c';
        memberList = Database.Query(sql);
        memberAddList = new List<Member__c>();
        memberAddList.add(new Member__c());
    }
   
    public void AddRow()
    {
        memberAddList.add(new Member__c());
    } 
}


Before adding rows:





After adding rows:

7 comments:

  1. The fields which i select those field only display in vf page,how to do that?

    ReplyDelete
  2. what is the use of dynamic soql?explain in brief

    ReplyDelete
    Replies
    1. Check this - http://www.infallibletechie.com/2015/09/dynamic-soql.html

      Delete
  3. explain the differance between these two statements
    memberAddList = new List();
    memberAddList.add(new Member__c());

    ReplyDelete
  4. This above code doesn't work.. what if there are already some rows in the custom object

    ReplyDelete
  5. I am facing difficulty in creating dynamic rows in popup. I think the rerender is not working . Please help me whether am missing anything.

    ReplyDelete