function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jeff Hutchinson 17Jeff Hutchinson 17 

how to use apex:variable and apex:param to dynamically assign delete buttons to rows of a table within a table?

Hi all,
I am trying to create a table within a table. Each row in both tables has an add and delete button to add and remove rows. The buttons for the outer table and the button to add rows to the inner table function correctly. The problem is that I am unable to delete rows from the inner table. I basically used the same code for both the inside and outside tables so I don't understand why one works and the other does not. From what I can tell the index number (deleteButtonNumber) for the delete commandLinks is always equal to the highest index of the last group in the inner table. My code is as follows.

Visualforce Page:
<apex:page id="AddItems" showHeader="true" sidebar="true" standardController="Formulary__c" extensions="AddItemsExtension">
    <apex:messages />
    <apex:form id="form">
        <apex:pageBlock title="Add Items Page" id="block">

            <apex:pageBlockButtons>
                <!-- Continue Button -->
                <apex:commandButton value="Continue" action="{!continueButton}"/>
                <!-- Cancel Button -->
                <apex:commandButton value="Cancel" action="{!cancelButton}"/>
                <!-- Add Group Button -->
                <apex:commandButton value="Add Group" action="{!addNewGroupToList}"/>
            </apex:pageBlockButtons>


            <apex:variable value="{!0}" var="groupNumber"/>
            <apex:pageBlockSection collapsible="false" columns="1">
                <apex:pageBlockTable value="{!groupList}" var="groupRecord" columnsWidth="10%,10%,10%,70%">
                    <apex:column headerValue="Group" width="100px">
                        {!'Group ' + TEXT(groupNumber + 1) }<br/>
                    </apex:column>

                    <!-- Add Product Link -->
                    <apex:column>
                        <apex:commandLink value="Add product to group" action="{!addProductButton}">
                            <apex:param value="{!groupNumber}" assignTo="{!buttonNumber}" name="buttonNumber"/>
                        </apex:commandLink>
                    </apex:column>

                    <!-- Delete Link -->
                    <apex:column>
                        <apex:commandLink value="Delete This Group" action="{!deleteGroupButton}">
                            <apex:param value="{!groupNumber}" assignTo="{!buttonNumber}" name="buttonNumber"/>
                        </apex:commandLink>
                    </apex:column>

                    <!-- Item List -->
                    <apex:variable value="{!0}" var="productNumber"/>
                    <apex:column headerValue="Item List">
                        <apex:pageBlockTable value="{!groupRecord.prods}" var="product">
                            <apex:column>
                                <apex:commandLink value="Delete Product" action="{!deleteProductButton}">
                                    <apex:param value="{!productNumber}" assignTo="{!deleteButtonNumber}" name="deleteButtonNumber"/>
                                    <apex:param value="{!groupNumber}" assignTo="{!buttonNumber}" name="buttonNumber"/>
                                </apex:commandLink>
                            </apex:column>
                            <apex:column>
                                <apex:outputText value="{!product.Product_Name__c}"/> <br/>
                            </apex:column>
                            <apex:variable var="productNumber" value="{!productNumber + 1}"/>
                        </apex:pageBlockTable>
                    <apex:variable var="groupNumber" value="{!groupNumber + 1}"/>
                    </apex:column>

                </apex:pageBlockTable>
            </apex:pageBlockSection>

        </apex:pageBlock>
    </apex:form>
</apex:page>
Apex Extension:
public class AddItemsExtension {

    ApexPages.standardController controller = null;
    public Integer groupNumber {get; set;}
    public Integer productNumber {get; set;}
    public List<DiscountGroup> groupList {get; set;}
    public List<Formulary__c> productList {get; set;}
    public Integer buttonNumber {get; set;}
    public Integer deleteButtonNumber {get{ return deleteButtonNumber;} set {deleteButtonNumber = value; System.debug('deleteButtonNumber set to ' + deleteButtonNumber);}}
    public String searchTerm {get; set;}
    public List<Formulary__c> searchResults {get; set;}
    public String formList {get; set;}

    public AddItemsExtension(ApexPages.StandardController controller) {
        this.controller = controller;
        groupList = new List<AddItemsExtension.DiscountGroup>();
        addNewGroupToList();
    }

    // Continue Button OnClick method
    public PageReference continueButton() {
        PageReference nextPage = Page.ProductQuery;
        nextPage.setRedirect(false);

        return nextPage;
    }

    // Cancel Button OnClick method
    public PageReference cancelButton() {
        return controller.cancel();
    }

    // Back Button OnClick method
    public PageReference backButton() {
        searchTerm = '';
        PageReference lastPage = Page.AddItems;
        lastPage.setRedirect(false);
        return lastPage;
    }

    // Add Group Button OnClick method
    public void addNewGroupToList() {
        DiscountGroup newGroup = new DiscountGroup();
        newGroup.prods = new List<Formulary__c>();
        newGroup.requireAll = true;
        newGroup.quantity = 1;
        groupList.add(newGroup);
    }

    public void deleteGroupButton() {
        groupList.remove(buttonNumber);
    }

    public void deleteProductButton() {
        System.debug('buttonNumber: ' + buttonNumber +
                    '\ndeleteButtonNumber: ' + deleteButtonNumber +
                    '\ngroupNumber: ' + groupNumber);
        groupList[buttonNumber].prods.remove(deleteButtonNumber);
    }

    // Add Product Button OnClick method
    public PageReference AddProductButton() {
        searchTerm = '';
        PageReference nextPage = Page.ProductQuery;
        nextPage.setRedirect(false);
        return nextPage;
    }

    public void search() {
        searchResults = new List<Formulary__c>();
        String temp = '%' + searchTerm + '%';
        searchResults = [SELECT Id, Product_Name__c FROM Formulary__c WHERE Product_Name__c LIKE :temp AND Formulary_Status__c != 'Discontinued' ORDER BY Product_Name__c ASC];
    }

    public List<SelectOption> getformChoices() {
        List<SelectOption> options = new List<SelectOption>();
        for (Formulary__c form : searchResults) {
            options.add(new SelectOption(form.Id, form.Product_Name__c));
        }
        return options;
    }

    public PageReference addItem() {
        Formulary__c tempForm = [SELECT Id, Product_Name__c FROM Formulary__c WHERE Id = :formList];
        groupList[buttonNumber].prods.add(tempForm);
        PageReference lastPage = Page.AddItems;
        lastPage.setRedirect(false);
        return lastPage;

    }

    public class DiscountGroup {
        public List<Formulary__c> prods {get; set;}
        public boolean requireAll {get; set;}
        public Integer quantity {get; set;}
    }
}
Any help is appreciated.
Rajesh Varma MudunuriRajesh Varma Mudunuri
Hi 

Please look into the below link it can help you 

https://success.salesforce.com/answers?id=90630000000hf07AAA

Thanks