I want to hide "Remove" commandLink at visualforce page when number of rows in pageBlockTable is 1 that means table has only single row.
And I also want to delete additional row created when user click on add commandLink by mistake, without putting any data in the row for required filed i.e. Account Name in my case.
My VF Page code is below
<apex:page showHeader="false" Controller="AddmultipleAccountsController">
<apex:form>
<apex:variable var="rowNum" value="{!0}" />
<apex:pageBlock>
<apex:variable var="rowNum" value="{!0}" />
<apex:pageBlockTable value="{!listAccount}" var="acc">
<apex:column headerValue="No.">
<apex:outputText value="{0,number, ###'.'}">
<apex:param value="{!rowNum+1}" />
</apex:outputText>
</apex:column>
<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:commandLink value="Add" action="{!addAccount}" />
<apex:outputLabel>/</apex:outputLabel>
<apex:commandLink value="Remove" action="{!removeAccount}">
<apex:param value="{!rowNum}" name="index" />
</apex:commandLink>
<apex:variable var="rowNum" value="{!rowNum+1}" />
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons>
<apex:commandButton value="Save" action="{!saveAccount}" />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
My Apex Controller Code is
public class AddmultipleAccountsController {
Account account = new Account();
public list<Account> listAccount{ get; set; }
public Integer rowNum{get;set;}
public AddmultipleAccountsController()
{
listAccount=new list<Account>();
listAccount.add(account);
}
Public void addAccount()
{
Account acc = new Account();
listAccount.add(acc);
}
Public void removeAccount()
{
rowNum = Integer.valueof(system.currentpagereference().getparameters().get('index'));
listAccount.remove(rowNum);
}
public PageReference saveAccount() {
for(Integer i=1; i<listAccount.size(); i++)
{
insert listAccount;
}
return Page.Allaccountssaved;
}
}