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
hemmhemm 

pageBlockTable from collection of name/value pairs

Hi,

 

Before I post an idea on Idea Exchange, I am curious if there is a way to create a collection of name/value pairs (like a Map) and use that collection as the value for a pageBlockTable.

 

I am working in a situation where I am using 100% dynamic Apex.  My code has no actual ties to real sObjects like Account, Contact, etc, but I am querying those records using dynamic Apex and loading custom classes with the results.  So I have all the data, but it's not actually tied to any object's metadata.  I have that allbeing dynamically worked.

 

I am trying to output some data to a pageBlockTable and I am having trouble getting it to work off unless I build a List<string> where string actually has HTML in it.(e.g. <td>value1</td><td>value2</td>:smileywink:.  I don't like this.

 

I tried seeing if I could construct an sObject using new sObject(), but that isn't allowed because it needs a sObjectType (I am not using those).  So I tried simulating an sObject using a Map<string,string> and having a List of those, List<map<string,string>>, but that didn't go so well.

 

Suggestions?
JimRaeJimRae

If you are already using a wrapper class (custom class) model for holding the data, you can create a list of your custom objects, and use that as the data source for your pageblocktable.

 

 

hemmhemm

The problem is that they are not salesforce objects in code.  It's just data.  In one query it might be Accounts, the next might be Leads, the next might be Tasks, etc. I will not know the actual sobject it all is tied to, but I determine it dyanmically to perform a dynamic soql query.

 

Another catch is that I don't know the exact fields being queried.  In one query, it might be 3 fields, the next might be 30.  What I do have is a bunch of name/value pairs that I need to get into a pageBlockTable.

 

Make sense?

JimRaeJimRae

Could you post your code, or at least a sample of it?

What are you holding the output of your query in now?

 

hemmhemm

I gotta go with a sample.  After all the complexity of determining what to query is finished and I get results back, I am in a situation where I have a class similar to the following.

 

 

public class aRecord { public String recordId {get; set;}{recordId='';} public map<string,string> nameValPairs {get; set;}{nameValPairs = new map<string,string>();} public String html {get; set;}{html = '';} }

 

 The above is an inner class in my controller.  In my controller is a property of

 

 

public List<aRecord> records {get; set;}{records=new List<aRecord>();}

 

So this means my VF Page has access to the "records" property and that's what I want to base the table off of.  I have successfully been able to do this if I fill the html property of my inner class to have a bunch of TD elements in a string.  However, I've never been able to use the name/value pairs.

 

I know the above construct does not work, but am trying to figure out something that does without building HTML strings in my controller and outputting those.  I am open to suggestions of reorganizing my collections to make it work with pageBlockTable, but I will not be able to tie those collections to any actual sObjects and I don't know how many nameValue pairs will be in the map.

 

The only semi-solution I have is below using the html property, but it's not very Visualforce-ish.  It's a hack.

 

 

 

<apex:pageBlock title="Data"> <apex:pageBlockTable value="{!records}" var="row"> <apex:column > <apex:outputText escape="false" value="{!row.html}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlock>

 

 

 

michaelforcemichaelforce

I know what you mean, I have run into the same problem before.  I ended up creating a custom object just for the purpose of creating some instances on the fly and displaying those records in a table... which it sounds like will not work for you as your project is far more dynamic in nature.

 

I know it isn't a silver bullet... but a step up from creating an HTML string might be to just create a list of values and then display them in a panelGrid component.

hemmhemm

Let me know more about the panelGrid piece.  Got any samples?  Any that use panelGrid and render the data to look like a pageBlock table?  I feel like I'll be getting into CSS classes there and the new UI might screw them up.

 

I've used the component before, but I can see the trick being getting the "columns" property correct so it's the right sized table.

 

TriesteTrieste

I know this is a super dead thread, but Google did find it when I was looking for the same answer.  I have since discovered how to do this on my own and I am now sharing.

 

In the following code, 'allMembers' is a Map<Integer, Object>

 

<apex:repeat id="theOuterTable" value="{!allMembers}" var="member" >
<apex:outputText value="{!allMembers[member]}" />
</apex:repeat>

 

The var ends up being the key from the map.  Then you can easily get from the map using the bracket notation.