1

I have generated my own set of data in my Visualforce controller and would like to display this as a table. I have various lists of Strings I would like to use as the columns. For example:

public List<String> dates;
public List<List<String>> columns;
public List<String> columnHeadings;

public List<String> getDates(){
    List<String> theDates = new List<String>('1 Jan', '2 Jan', '3 Jan');
    return theDates;
}

public List<List<String>> getColumns(){
    List<List<String>> listToReturn = new List<List<String>>();
    List<String> column1 = new List<String>('Cat', 'Dog', 'Rat');
    listToReturn.add(column1);
    List<String> column2 = new List<String>('Apple', 'Banana', 'Cherry');
    listToReturn.add(column2);
    return listToReturn;
}

public List<String> getColumnHeadings(){
    List<String> theHeadings = new List<String>('Date', 'Animal of the Day', 'Fruit of the Day');
    return theHeadings;
}

Is there a way I can put these into a Page Block Table or similar? It seems like I can only do it with object data.

Many thanks in advance for help!

2 Answers 2

1

Convert your data structure to a list of inner class objects with the various columns as properties. expose that list as a property on the class and construct your page block table using that list-of-inner-class-objects.

public class vfController {
// represents a logical row.
public class RowObject {
    Id rowId {get; set;}
    Integer foo {get; set;}
    // .. continue adding properties for each column.

}

public List<RowObject> rows {get; set;}

public vfController() {
    //constructor creates list of rowObjects
    this.rows = new List<RowObject>();

    //here you would get your data somehow
    for(sObject s: MyData) {
        RowObject r = new RowObject();
        r.rowId = s.id;
        r.foo = 3;
      this.rows.add(r);
    }

}
}
2
  • Thanks! Could you provide an example of the code I would use?
    – fred
    Commented Apr 28, 2015 at 15:27
  • not handy. here's a not-sure-the-syntax-is-right bit.
    – Kevin P
    Commented Apr 28, 2015 at 15:32
1

Visualforce pages exist in relation to Salesforce objects. If you have data you want to display on a Visualforce page, you'll need to relate it to a Salesforce object. If this is data that is truly unconnected to any of your existing Salesforce data, create a new object as a placeholder.

The technique of using a wrapper class to add extra information beyond what a Salesforce object contains is frequently used in Visualforce/Apex and there's a basic example here.

1
  • This was also helpful. I ended up using a wrapper. Thanks!
    – fred
    Commented Apr 29, 2015 at 14:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .