Component Generation

Dynamic field references are useful when you do not know what fields to display. Component generation comes into play when you do not know what object to render. It allows the construction of a Visualforce page from Apex code.

To start using component generation, add one or more dynamicComponent elements to your Visualforce page. This serves as the container into which the generated components are injected. The dynamicComponent is bound to a controller method, specified in the componentValue attribute, that must return an instance of Component.Apex.* to be rendered.

Listings 7.17 and 7.18 show a controller and page that leverage component generation to display a detail component bound to the user’s selection of one of three object types. A dynamic SOQL statement is generated using the list of accessible fields from the selected object type to retrieve the most recently modified record. The generated detail component is bound to its result.

Listing 7.17 Visualforce Controller Using Dynamic Components


public class MyPageController7_18 {
  public SObject record { get; set; }
  public String selectedObject { get; set; }
  public List<SelectOption> getAvailableObjects() {
    List<SelectOption> options = new List<SelectOption>();
    options.add(new SelectOption('Project__c', 'Project'));
    options.add(new SelectOption('Timecard__c', 'Timecard'));
    options.add(new SelectOption('Contact', 'Contact'));
    return options;
  }
  public PageReference refresh() {
    Schema.SObjectType targetType =
      Schema.getGlobalDescribe().get(selectedObject);
    Map<String, Schema.SobjectField> fields =
      targetType.getDescribe().fields.getMap();
    List<String> queryFields = new List<String>();
    for (String s : fields.keySet()) {
        if (fields.get(s).getDescribe().isAccessible()) {
          queryFields.add(s);
        }
    }
    String soql = 'SELECT ';
    for (String s : queryFields) {
      soql += s + ', ';
    }
    soql = soql.substring(0, soql.length() - 2);
    soql += ' FROM ' + selectedObject;
    soql += ' ORDER BY LastModifiedDate DESC LIMIT 1';
    try {
      record = Database.query(soql);
    } catch (QueryException e) {}
    return null;
  }
  public Component.Apex.Detail getComponent() {
    Component.Apex.Detail result =
      new Component.Apex.Detail();
    result.expressions.subject = '{!record.Id}';
    result.title = false;
    result.relatedList = false;
    return result;
  }
}


Listing 7.18 Visualforce Page Using Dynamic Components


<apex:page controller="MyPageController7_18">
  <apex:form >
    <apex:selectList value="{!selectedObject}" size="1">
      <apex:selectOptions value="{!availableObjects}"/>
    </apex:selectList>
    <apex:commandButton value="Refresh" action="{!refresh}" />
  </apex:form>
  <apex:dynamicComponent componentValue="{!component}"/>
</apex:page>



Note

Component generation is not a viable substitute for standard static Visualforce pages. Its use should be strictly limited to user interfaces that adapt to user actions in ways that can’t be coded in static markup.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.144.229