0

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.

enter image description here 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;
    }
}
1

The rendered attribute on apex:commandLink is what you need to use:

<apex:commandLink 
     value="Remove" 
     action="{!removeAccount}" 
     rendered="{!listAccount.size != 1}">
  • Thanks BarCotter, my first issue is resolved but my second requirement still pending. And I have 1 more requirement at same page that Add commandlink will be shown for last row only. – Krishan Gopal Jul 22 '15 at 9:15
  • I don't see anything obvious that would cause the links to only appear on the last row. If you create a new question and add the details then someone should be able to help you out. – BarCotter Jul 22 '15 at 9:30
  • Actually Add CommandLink shown after each and every row which is not looking good, I want to shown in last row only. – Krishan Gopal Jul 22 '15 at 11:11
  • @KrishanGopal Instead of having 'Add' link in every row, put it below the table, in the table footer maybe. – ajinkyah Jul 22 '15 at 11:18
  • really very thanks Mr. BarCotter my issue resolved completly except to put some value in Account Name to delete additional row in table. – Krishan Gopal Jul 22 '15 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.