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
Jim BoudreauxJim Boudreaux 

DataTable as Related List

Alright, here's another one.
I am making a visualforce page that displays data from a custom object and its two related lists. I successfully used the <apex:relatedlist> tag by setting the list="MonitoringContacts__r" which is how you specify a Child Relationship Name.
While it worked, the rendered result maintained Salesforce's look and feel which is what i am trying to avoid here.
What I want is a list of data from a related list, but i want to control what the final product looks like. So I settled on a DataTable. This is the code I used to render a DataTable containing Related List info, using the Child Relationship naming convention mentioned earlier (I only have one column here for simplicity)

<apex:datatable value="{!Monitoring_Agreement__c.MonitoringContacts__r}" var="Contact">
      <apex:facet name="caption">table caption</apex:facet>
      <apex:facet name="header">table header</apex:facet>
      <apex:facet name="footer">table footer</apex:facet>
                  <apex:facet name="header">Password</apex:facet>
                  <apex:facet name="footer">column footer</apex:facet>
                  <apex:outputText value="{!Monitoring_Agreement__c.MonitoringContacts__r.password__c}"/>

Most of this is apparently right because I only get an error for the last part, the part where I set the OutputText value="{!Monitoring_Agreement__c.MonitoringContacts__r.password__c}"
The error I get is:

ErrorError: The class 'java.util.ArrayList' does not have the property 'password__c'.

I have gotten this error before, so I must be misunderstanding something.

What i am looking for from you guys and gals is
  1. How do I display a Related List in a DataTable?
  2. How do I specify elements of an ArrayList so as to avoid the above error?
Thanks in advance....
<apex:pageBlockTable value="{!Monitoring_Agreement__c.MonitoringContacts__r}" var="Contact">
      <apex:facet name="caption">table caption</apex:facet>
      <apex:facet name="header">table header</apex:facet>
      <apex:facet name="footer">table footer</apex:facet>
      <apex:column footerValue="column footer" value="{!Contact.password__c}"/>

I know I just responded to your other thread but yep, pageBlockTable is the way to go.

First, pageBlockTable and dataTable are iterators.  Think of it like a java "for each" loop:
for (Contact c : ListofContacts) {


You are now free to use c in your loop.  Same applies here, you supply a var called Contact that represents one contact in the list (value attribute) you supplied.

Also notice that I collapsed the footer facet and removed the header facet entirely.  pageBlockTable will automatically generate your headers for you if you're using the value attribute bound to an sobject field (though you can still supply it if you want using headerValue, and you can still use the facet if you wanted to put an image or a link, for example, as your column header)
ok so I saw that you don't want the salesforce styles.

You can replace pageBlockTable with dataTable, and then you'll have to add headerValue back into your column.  Everything else should still work!
Jim BoudreauxJim Boudreaux
Yup, that worked just dandy, thanks!
Jim BoudreauxJim Boudreaux
New issue: Is there a way to Order the contents of the DataTable by a certain field?

I'm guessing that you're using the standardController, so you'll want to also add an extension.  Then for dataTable you'll do value="{!contactList}".

In your extension create a method called public MonitoringContacts__c[] getContactList() that does a query for all of your monitoring contacts using the ORDER BY operator in SOQL.
Jim BoudreauxJim Boudreaux
Is that something I can do in a production org? Because I am not fully up to speed on Eclipse and moving things from dev orgs to prod orgs just yet.
Jim BoudreauxJim Boudreaux
Furthermore, is there any way to use SOQL queries in the VF markup? Like in the {! } notation.
No, there is no support (and none planned because most of us think something like SOQL does not belong in the markup layer - this is one of the major anti patterns learned from other frameworks like JSP). With that said we are working on introducing the concept of filter objects as a first class meta definition and you will be able to bind to a Filter (something like {!$Filter.myFilter} is what I expect it might look like) in an upcoming release.
Jim BoudreauxJim Boudreaux

In that case, how about this:

If I bind a dataTable to a Related List, <apex:datatable value="{!Custom_Object__c.Related_Object__r}" var="variable" />, is there a way to have the entries iterated in the datatable do so in the same order in which they are listed in the related list?

To illustrate, I have a custom object that has a related list of people to call in an emergency. The custom object for these people has a custom field that indicates the order in which they should be called. The related list orders them according to that field. Given that there is no way to order data in the VF markup, wouldn't it make sense that the data that is retrieved is done so in the same manner that the related list is done, so that my datatable returns data in the same order that the related list does on my standard salesforce UI?

Good point!
Interested in the outcome of this point.
Wouldn't it also make sense to add an "order by" component in the apex:pageBlockTable tag?
Thanks and regards