5

I have a couple of lists in my controller. And i have a table. The first element in both lists correspond to each other and i want them to show up side by side on the pageblocktable. The problem is there is no iterator for them and they just print the complete list in every column for each list.

<apex:column value="{!string}" />
<apex:column value="{!date}" />

I tried making it so that is there anyway to use an iterator in the pageblocktable to achieve what i want? or will i have to use something else.

<apex:column value="{!string[i]}" />
<apex:column value="{!date[i]}" />
6

Time for the infamous 'wrapper' inner class!

Assuming there is a 1:1 correspondence between the string list and the integer list ....

Your controller (assumes the pageblocktable is displayed after the string and integer lists are instantiated; if not, the getter for myWrapperList needs adjusting)

private List<String> sList;
private List<Integer> iList;

public List<MyWrapper>  myWrapperList {
  get {
       if (myWrapperList = null) {
          myWrapperList = new List<MyWrapper> ();
          for (integer i = 0; i < sList.size(); i++)
              myWrapperList.add(new MyWrapper(sList[i],iList[i]));
      }
      return myWrapperList;
  }
 private set;
}

public class MyWrapper {
   public String str {get; set;}
   public Integer int {get; set;}
   public MyWrapper(String s, Integer i) {
      this.str = s;
      this.int = i;
}

The VF pageBlocktable thus becomes

<apex:pageblockTable value="{!myWrapperList}" var="mw">
  <apex:column value="{!mw.str}"/>
  <apex:column value="{!mw.int}"/>
</apex:pageBlockTable>
  • One error you have is in your line myWrapperList.add(sList[i],iList[i]); it needs to add the object itself not the lists. So you would need MyWrapper(sList[i],iList[i]); Mywrapperlist.add(myWrapper); – James May 16 '14 at 16:59
  • sheesh -- I typed too fast; I got sloppy. Fixed in code above. Thanks – cropredy May 16 '14 at 17:34
3

You can also approach without a wrapper class:

lists

Lists.page

<apex:page controller="ListsController">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!Indexes}" var="i">
            <apex:column value="{!Foos[i]}" />
            <apex:column value="{!Bars[i]}" />
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

ListsController.cls

public with sharing class ListsController {

    public List<String> Foos {get; set;}
    public List<String> Bars {get; set;}

    public ListsController() {
        this.Foos = new List<String>{'one', 'two', 'three', 'four', 'five'};
        this.Bars = new List<String>{'a', 'b', 'c', 'd', 'e'};
    }

    //you need only avail this sequence
    public List<Integer> getIndexes() {
        List<Integer> indexes = new List<Integer>();
        for (Integer i = 0; i < this.Foos.size(); i++) indexes.add(i);
        return indexes;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.