+ Start a Discussion
Scott.MScott.M 

Reference Index in Repeat

Is there a way to reference the index number of an object in a repeat component. For example like the following

 
Code:
<apex:repeat value="Account.Contacts" var="Contact" id="contacts_list">
  Array Index : {![some variable that gives the index of the element in the array]}
</apex:repeat>

Thanks!

 

Ron HessRon Hess
no, i don't know of any feature like this
Scott.MScott.M
Thanks for the response Ron,

The reason I would like this is so that I can remove objects from the related list

Code:
<apex:repeat value="{!PartOrderParts}" var="PartOrderPart" id="partList">
  <apex:inputField value="{!PartOrderPart.Qwark__Part__c}" />
  <apex:commandButton action="#" value="Remove"  action="{!removePartOrderPart}" rerender="counter" status="counterStatus">
    <apex:param name="ref" value="{}"/>
  </apex:commandButton>
  <p />
</apex:repeat>

//the function to remove the parts
public PageReference removePartOrderPart() {
   Integer ref = Integer.valueOf(System.currentPageReference().getParameters().get('ref'));
   System.debug(ref);
   this.PartOrderParts.remove(ref);
   return null;
}

 This is on a create so the list items haven't been inserted into salesforce yet so they don't have an id. There maybe a better way to reference the objects in the array. Any suggestions would be helpful!

dchasmandchasman
We typically solve this by going with a more object oriented approach of binding the command button action to a method on the actual row object (PartOrderPart in your case). Something like this:

Code:
<apex:repeat value="{!PartOrderParts}" var="PartOrderPart" id="partList">
  <apex:inputField value="{!PartOrderPart.Qwark__Part__c}" />
  <apex:commandButton value="Remove" action="{!partOrderPart.remove}" rerender="counter" status="counterStatus"/>
  <p />
</apex:repeat>

class PartOrderPart {
 ...
PartOrderPart(PartOrder partOrder) {
this.partOrder = partOrder;
}

public void removePartOrderPart() { partOrder.removePartOrderPart(this); }
...

private final PartOrder partOrder;
}

 

Scott.MScott.M
That makes sense except what would the partOrder.removePartOrderPart(this) look like since passing 'this' still won't give you the index of the element to remove in the PartOrder array of PartOrderParts?

Steve ChadbournSteve Chadbourn

I'd do it this way

Code:
<apex:column >
   <apex:commandButton action="{!removePart}" value="Remove" rerender="partList">
      <apex:param name="partId" value="{!partOrderPart.Id}"/>
   </apex:commandButton>
</apex:column>


 

Then in the controller:

Code:
public null removePartt()
{
    String partId = System.currentPageReference().getParameters().get('partId');

    // code to remove part here
}


 

Steve ChadbournSteve Chadbourn

Slight typo in the controller code - should be:

public void removePartt()
{
    String partId = System.currentPageReference().getParameters().get('partId');

    // code to remove part here
}



Message Edited by Steve Chadbourn on 05-27-2008 02:32 PM