function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
prakash_sfdcprakash_sfdc 

Dynamic Fields on VF page

Hi,

 

Suppose I have the list of fields of an Object using:

 

Map<String, Schema.SObjectField> FMap = Schema.SObjectType.CustomObject__c.fields.getMap();

CustomObject__c tempRecord=new CustomObject__c();

List<String> mainList=new List<String>();

Set<String> tempSet=FMap.keySet();

for(String s:tempSet)

mainList.add(s);

 

Now, Can you show all these fields on the VF page in Input mode ?

 

In other words, assume the first field is Text, so I should get <apex:inputText>. At present, we can assume that all fields are text.

what can be possible VF code. I was thinking something with the repeat tag.

 

 

Adam HowarthAdam Howarth

Hey,

 

You can use dynamic components. So creating the controls in the controller and applying the fields to the controls you need.

 

A basic example is below:

 

=====================
Visualforce page
============================

<apex:page standardcontroller="YourController" extensions="DynamicComponentExample">
    <apex:form >
           <apex:dynamicComponent componentValue="{!pageBlock}"/>
    </apex:form>
</apex:page>


Controller:
=============

public Component.Apex.PageBlock getpageBlock(){

        Component.Apex.PageBlock pb=new Component.Apex.PageBlock();
        Component.Apex.PageBlockTable  ptable=new Component.Apex.PageBlockTable();

        Component.Apex.Repeat repeat= new Component.Apex.Repeat( var = 'f');
        repeat.expressions.value='{!mainList}';

        Component.Apex.OutputField oppTotal = new Component.Apex.OutputField();
        oppTotal.expressions.value='{!CustomObject__c[f]}';
        repeat.childComponents.add(oppTotal);

        ptable.childcomponents.add(repeat);
        pb.childcomponents.add(repeat);

        return pb;
    }

 

Something along these lines should work. Its hard to give a complete answer without more code.

 

Cheers,

Adam

souvik9086souvik9086

Hi,

 

Yes you are right. Try that in repeat tag

<apex:repeat value="{!mainList}" var="ml" >

<table>

<tr>
<td style="width:95px;border-bottom:1px solid #E3DEB8;">

<apex:inputText value="{!ml}" style="width: 95px;"/>

</td>

</tr>

</table>

</apex:repeat>

 

If this post solves your problem , please mark it as solution.

 

Thanks