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
Javier CGJavier CG 

Commandbutton on every row of pageBlockTable only call method in the first row

I have a pageBlockTable that show a list of variables that exist in the controller class.
On each row, I have put an Erase button in the last column to delete that element of the list. The button passes the Id of the element (the list contains the id as one of its data) to delete.
The method works well, but only the first-row button calls the method when the user clicks on it.
If you click on the other rows buttons, no action is taken.
But if you click the first button and that row is deleted, then the new first button (previous second button) runs well, but not the other ones, and so on.
This is the pageblocktable code:
<apex:pageblocktable value="{!ListVariable}" var="Var" id="TableName" rowClasses="odd,even" styleClass="tableClass" border="1" cellspacing="0" cellpadding="5" width="100%">
    <apex:column>
        <apex:facet name="header">NAME</apex:facet>
        <apex:outputText value="{!Var.Name}"/>
    </apex:column>
    <apex:column >
        <apex:commandButton value="Delete" action="{!DeleteItem}" reRender="TableName" immediate="true" oncomplete="alert('Completed');">
            <apex:param name="Id" value="{!Var.Id}" assignto="{!IdToDelete}" />
        </apex:commandButton>
    </apex:column>
</apex:pageblocktable>
And this is the method in the controller:
public void DeleteItem()
    {
        integer i = 0;

        while(i < ListVariable.size())
        {
            if(ListVariable.get(i).Id == IdToDelete)
            {
                ListVariable.remove(i);
                return;
            }
        }
    }
The IdToDelete is initialized in the controller too.
The code is correct in the controller because if you click on the first button, the method does the correct thing, but I don´t know why the other buttons don´t call the method.
All the code that I have seen is the same as mine.

Some idea?
Christan G 4Christan G 4
I think your issue has something to do with the <apex:param> component. May I ask what is that component for?
Javier CGJavier CG
The VF page is a customized one to load products on an opportunity because the client needs several specific behaviours that the standard one doesn´t fulfil.
The client fills the product data in a form above that table and adds it to the list. After all the required products have been added to the list, he confirms to add all of them to the opportunity.
The param in the button contains the id of the product to erase it from the list and it is used to pass the parameter to the method when the button is clicked. The controller has that public variable to look for in the list inside the method to delete the product in the list. The list contains information of product and pricebookentry together, to show it in the table.
The code showed for the table is just one column to ease your reading.