2

I am looking to create a table, for instance pageBlockTable, with entries from multiple object types. The various objects share some fields, and also have unique fields. The table should display a set from the shared fields. Example:

Headers: ID, Name, Description

Row 1: CAR-1, Opel, blabla

Row 2: PLANE-1, Airbus, blablabl

The problem is obviously that pageBlockTable can only iterate over a single object. How do I solve this?

  • 1
    i think wrapper classes suits your need,but to answer it would be better if you can share some more information regarding objects and their field types which you want to display as a table so that it will help to write some code...Thanks – user4971 Jan 9 '14 at 13:02
10

You could use inner wrapper class as an abstraction of your vehicle object, create and populate list of those custom objects, and finally make the binding in the VF page to use the list of custom objects:

Controller:

public with sharing class YourController {

    public List<Vehicle> vehicles { get; set; }

    public YourController(ApexPages.StandardController controller) {

        vehicles = new List<Vehicle>();

        List<Car> cars = [SELECT Id, Name, Description FROM Car__c];
        List<Plane> planes = [SELECT Id, Name, Description FROM Plane__c];

        for(Car__c car : cars) {
            vehicles.add(new Vehicle(car));
        }

        for(Plane__c plane : planes) {
            vehicles.add(new Vehicle(plane));
        }
    }

    public class Vehicle {
        public Id id { get; set; }
        public Name name { get; set; }
        public string desc { get; set; }

        public Vehicle(Car__c car) {
            this.id = car.Id;
            this.name = car.name;
            this.desc = car.desc;
        }

        public Vehicle(Plane__c plane) {
            this.id = plane.Id;
            this.name = plane.name;
            this.desc = plane.desc;
        }
    }
}


VF page:

<apex:pageBlockTable value="{!vehicles}" var="vehicle">
    <apex:column headerValue="Id" value="{!vehicle.id}" />
    <apex:column headerValue="Name" value="{!vehicle.name}" />
    <apex:column headerValue="Desc" value="{!vehicle.desc}" />
</apex:pageBlockTable>

Hope you'll manage to take it on from here.

  • 2
    +1 but could you add a sample of implements Comparable for completeness ;) – eyescream Jan 9 '14 at 13:51

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.