Dynamically deleting rows in apex pageBlock

Visualforce page:

<apex:page showChat="false" wizard="true" sidebar="false" controller="MemberPopup" >
<!-- Javascript -->
<script type = "text/javascript">
    function winClose()
    {
        self.close();
    }
</script>
<!-- End of Javascript-->
<apex:form >
    <apex:pageBlock >
        <apex:pageblockSection >
            <apex:pageblocktable value="{!memberList}" var="mem">
                <apex:column title="Name" value="{!mem.Name}"/>
                <apex:column title="eMail Id" value="{!mem.E_Mail_Id__c}"/>
            </apex:pageblocktable>
        </apex:pageblockSection>
    </apex:pageBlock>
   
    <apex:variable var="rowNum" value="{!0}"  />
   
    <apex:pageBlock id="membAdd" >  
    <apex:variable var="rowNum" value="{!0}"  />              
        <apex:pageblockSection >
            <apex:pageBlockTable value="{!memberAddList}" var="memb">
                <apex:facet name="footer">
                    <apex:commandLink value="Add Row" action="{!addRow}" reRender="membAdd"/>
                </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="{!rowNum+1}" />  
                    </apex:outputText>
                </apex:column>           
                <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:column headerValue="Delete" >
                    <apex:commandLink style="font-size:15px; font-weight:bold; text-align:center;color:red;" value="X" action="{!delRow}" reRender="membAdd,temp" rendered="{!rowNum>0}">
                        <apex:param value="{!rowNum}" name="index" />
                    </apex:commandLink>
                    <apex:variable var="rowNum" value="{!rowNum+1}"/>
                </apex:column>               
            </apex:pageBlockTable>                   
        </apex:pageblockSection>       
        <apex:pageblockSection columns="1" >
            <apex:pageblockSectionItem >
                <apex:commandButton value="Save" action="{!saveMemb}" onComplete="winClose();"/>
                <apex:commandButton value="Cancel" onclick="winClose();" />
            </apex:pageblockSectionItem>        
        </apex:pageblockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

Apex controller:

public class MemberPopup

{
    public List<Member__c> memberList {get;set;}
    public List<Member__c> memberAddList {get;set;}
    public String memberName {get;set;}
    public Integer rowNum{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());
    }
   
    public void delRow()
    {
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        memberAddList.remove(rowNum);  
    }   
   
    public void saveMemb()
    {
        insert memberAddList;
    }
}

No comments:

Post a Comment