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
Jon KeenerJon Keener 

Accessing the Index of a List within a dataTable and use of parameters in a commandButton action

I'm attempting to use a dataTable to create an entry grid for users to enter 1-many items.  Screenshots and snippets of code are below:
The issues I'm having in making this fully functional are:
  1. Enabling the "Delete" button on each line.  To enable this, what would be the optimum answer is having a commandButton Action like:
<apex:commandButton action="{!RemoveMaterial(lines.index)}" value="Delete" styleClass="btn" rerender="MaterialGrid" immediate="true"/>

The key difference, the RemoveMaterial(lines.index).

So the first question, is there any way to access the actual index of the List in a column of a dataTable?

The second question is passing parameters in the action parameter of the commandButton.  From previous attempts, I've found that I could pass a parameter in an action call, like shown above.  However, the current documentation (Spring 08) states on page 100 - "To add query string parameters to a commandButton, specify them in the associated action method".  Assuming this is true, how do you do this if not like above, and how would you set up the code in the class to access these parameters.

A third item that I ran into, however I'm planning to work around it for now, is if I make any of the inputFields required, when I add a second+ line, when a required field on a previous line is blank, I get the error message popping up stating that the field is required. I'd rather not yell :smileyhappy: at the user to they hit the "next" button (not shown in screenshot, but it's part of a multistep wizard)

I've got some less elegant alternatives for deleting a row that I might try, like placing regular html input buttons instead of commandButtons, etc., but I'm hoping to solve this without resorting to that, because then the code will be a bit bulkier to support this.  Based on the code involved so far, it should be pretty reusable for other grids.

Thanks for any assistance!

Jon Keener


, without resorting to Javascript onclick events(which I'm going to be investigating after this, but I foresee some of the same issues there also)

The Entry form is working nicely and the Add Another Material is working. 
Entry Form:
Page Snippet
<apex:pageBlockSection title="Material(s) to be included in Sample:" collapsible="false">
 <apex:dataTable value="{!sampleMaterials}" var="lines" styleClass="list" id="MaterialGrid">
    <apex:facet name="header"><CENTER><b>Actions</b></CENTER></apex:facet>
    <apex:commandButton action="{!RemoveMaterial}" value="Delete" styleClass="btn" rerender="MaterialGrid" immediate="true"/>
    <apex:facet name="header"><b>Material Name</b></apex:facet> 
    <apex:inputField value="{!}"/>  
    <apex:facet name="header"><b>Sample Quantity</b></apex:facet>
    <apex:inputField value="{!lines.Sample_Quantity__c}"/>  
    <apex:facet name="header"><b>UOM</b></apex:facet>
    <apex:inputField value="{!lines.Sample_UOM__c}"/>  

<apex:panelGrid columns="1"> 
    <apex:commandButton action="{!AddNewMaterial}" value="Add Another Material" styleClass="btn" rerender="MaterialGrid" immediate="true"/> 

 Controller Snippet
List<Sample_Material__c> sampleMaterials;

public List<Sample_Material__c> getSampleMaterials() {
    if (sampleMaterials == null)
       //Initialize the Object
       sampleMaterials = new List<Sample_Material__c>();
       sampleMaterials.add(new Sample_Material__c());
    return sampleMaterials;

public void AddNewMaterial() {
    if (sampleMaterials != null)
       sampleMaterials.add(new Sample_Material__c());

public void RemoveMaterial() {

How did you resolve this issue?

Yes, I'd like to see the solution too !


you might have to use the apex:param tag to pass parameters into your function.