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
SuvraSuvra 

Retrieving Index of a List in Visualforce page

Hi,

 

Is there any way of accessing the Index of a list on which I am iterating in a page? How to get the current index?

 

Also, if I have another List object in Controller, and I just want to use that list in a page. Say, i have a list of string called "productList" in Controller and it has values like "A","B","C","D". Now in page I have some index value say,3 available in some variable. And with that index I need to access productList[3]. Is it at all possible in VF?

 

 

Thanks in advance,

 

Suvra

wesnoltewesnolte

Hey

 

The only way would be to use an inner class or a list of lists. You need to store the index with the value eg.

 

public Class myController{ 

 

public Class MyClass{

  public String value{get;set;}

public Integer index{get;set;}

 

public MyClass(String v, Integer i){

  value = v;

index = i; 

 

List<MyClass> values{get;set;}

 

 public myController(){

  values= new List<MyClass>();

values.add(new MyClass('A',1)); 

values.add(new MyClass('B',2));  

 

Then in the page

 

<apex:repeat value="{!values}" var="row"> .. etc

 

You could do something similar with a list of lists but it wouldn't be as readable. 

 

Cheers,

Wes 

SuvraSuvra

Hi.. Thanks a lot for your reply.

If it is possible that way, then need one more help from you..

 

Suppose, I have a List of Products "listOfProds" containing Name,Code,Desc columns of Product. Now, depending upon the index value I need to access the element from the List. For example, index = 0; Then I need the 0-th column of List, i.e Name. Is it possible by any chance?? I need to make it dynamic for any column, instead of "listOfproducts[i].Name"

 

With Best Regards,

Suvra

wesnoltewesnolte

Hey

 

Put the list item in the wrapper too then:

 

public Class MyClass{

  public String value{get;set;}

public Integer index{get;set;}

        public Product2 product{get;set;}

 

public MyClass(String v, Integer i,Product2 p){

  value = v;

index = i; 

                product = p;

}

 

<apex:datatable value="{!values}" var="row">

   <apex:outputText value="{!row.product.id}"/>

 

// etc

 

Wes