2

All,

I am trying to use dynamicComponent to build a PageBlockTable.

In the controller I want to iterate over each element and perform certain operations.

My VF page looks something like this

<apex:page standardController="{!OBJECT NAME}" recordSetVar="attrs" extensions="Ext">
<apex:dynamicComponent componentValue="{!dynamicPageBlockTable}"/>

And relevant controller code looks like

public Component.Apex.PageBlockTable getDynamicPageBlockTable() {
    Component.Apex.PageBlockTable pbt = new Component.Apex.PageBlockTable();
    pbt.expressions.value = '{!attrs}';
    pbt.var = 'a';
    Component.Apex.column column = new Component.Apex.column();
    column.expressions.value = '{!a.id}';
    pbt.childComponents.add(column);
    return pbt;
}

With this code, VF page displays all the ids. But I want to iterate over all the values present in {!attrs} and then perform some additional actions.

Is this possible?


Added: Just to elaborate on what I want to do...

This code...

pbt.var = 'a';
Component.Apex.column column = new Component.Apex.column();
column.expressions.value = '{!a.id}';
pbt.childComponents.add(column);

works fine and I can see all the IDs on VF page. But what I want to do is something similar to

for (each id in {!attrs}) { get information like name, owner related to id based on the information build a VF component such as list, checkbox add this component to column }

2
  • not clear what is the problem and what you're trying to achieve – Raul Mar 21 '17 at 14:37
  • Editing the original post with clarification – vetal_king Mar 21 '17 at 14:40
1

What you're trying to do should be done BEFORE you generate the component:

public Component.Apex.PageBlockTable getDynamicPageBlockTable() {
    for (Type t : attrs) {
        // Do your processing on each record
    }

    Component.Apex.PageBlockTable pbt = new Component.Apex.PageBlockTable();
    pbt.expressions.value = '{!attrs}';
    pbt.var = 'a';
    Component.Apex.column column = new Component.Apex.column();
    column.expressions.value = '{!a.id}';
    pbt.childComponents.add(column);
    return pbt;
}
2
  • Thanks Toby for your reply. The variable "attr" I am not able to access in the "for (Type t: attrs)" loop. More specifically for each attribute: 1. check the datatype (String, Data, Dropdown) 2. Build the appropriate UI component (inputField, SelectList) 3. Render it in the table. In short, I want to build a VF page dynamically based on the "type" metadata stored for each attribute in the table – vetal_king Mar 22 '17 at 10:47
  • I can understand that, I'm not sure why you can't access the attrs variable though? That variable has to exist in the public context within your controller for it to be used in VF, even if it is dynamically. That being said, it almost sounds like you don't actually want to use a pageblocktable. If you're generating a form you should be using pageblocksection and generate a list of pageBlockSection items. Either way, the answer stays the same. You have to have the list of attributes stored somewhere in your controller so you can loop through it. – gNerb Mar 22 '17 at 13:25
0

Thanks Toby for your help... this is how we finally solved it... relevant section of the code...

 <apex:page standardController="<STD_CONTROLLER_NAME>" recordSetVar="attrs" extensions="<EXTENSION>" sidebar="false" docType="html-5.0" >
  ......
  <apex:pageBlock>

    <apex:pageblockSection >
      <apex:pageBlockTable value="{!dataTestList}" var="a" styleclass="outBorder" width="550px" id="assetAttributeTable">

        <apex:column styleclass="inBorder" >
          <apex:facet name="header">Data Value</apex:facet>
          <apex:inputText rendered="{!a.dataType =='STRING'}" value="{!a.dataValue}"  onblur="<VALIDATION_JS>"/>
          <apex:inputText rendered="{!a.dataType =='NUMBER'}" value="{!a.dataValue}" onFocus="saveOriginalValue(this.value)" onblur="<VALIDATE_JS_TO_TEST_NUMBER>"/>
          <apex:inputText rendered="{!a.dataType =='DATETIME'}" value="{!a.dataValue}" required="true" onFocus="saveOriginalValue(this.value)" onblur="<VALIDATE_JS_FOR_DATETIME>"/>
    </apex:pageblockSection >
...

and the controller (again relevant code)

 public List <MetaDataForAttribute> getdataTestList() {

    String strSQL = <SOQL to retrieve metadata for all the relevant attributes>
    MetaDataForAttribute dataTestList;
    for (CONFIG_ATTR_VALUES__c attr : Database.query(strSQL)) 
    {
        MetaDataForAttribute singleAttr = new MetaDataForAttribute();
        singleAttr.dataType = attr.CONFIG_ATTR_ID
        dataTestList.add(singleAttr);
    }    
    return dataTestList ;
}

Class MetaDataForAttribute contains a lot of information such as permissible range; drop down values if the control should be rendered as a drop down etc. The JS routines would validate number, string, datetime etc.. With this approach we could also render dropdowns whenever required.

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.