1

I have code such as the following:

<apex:dataTable value="{!myContacts}" var="c" columns="3">
    <apex:column>
        <apex:outputText value="{!c.FirstName}"/><br />
        <apex:outputText value="{!c.LastName}"/><br />
        <apex:outputText value="{!c.Email}"/><br />
    </apex:column>
    <br />
</apex:dataTable>

This will display something like:

John
Smith
js@example.com

Sue
Jones
sjo@example.com

Joe
Mack
jma@example.com

Jane
Doe
jd@example.com

I want to display myList across N (e.g., 3, 4, etc.) different columns, so that the result is like the following (N=3 in this case). Note that the number of columns is not the same as the number of items on the list.

John              Sue                Joe
Smith             Jones              Mack
js@example.com    sjo@example.com    jma@example.com

Jane
Doe
jd@example.com

How can this be done? Preferably there's just some attribute of apex:dataTable or other apex component tag staring me in the face that I'm missing.

Note that specifying columns="3" on the apex:dataTable has no effect -- still just one column.

Thanks.

2
  • 1
    Is the number of columns static or dynamic?
    – Mike Chale
    Sep 28 '12 at 18:42
  • @MikeChale - I was thinking dynamic to have a more general solution, but static is acceptable. Sep 28 '12 at 18:45
6

Unless you need to use the datatable tag for some other reason, this could be done with <apex:repeat> and html table tags:

<table>
<tr>
    <apex:repeat value="{!myContacts}" var="c" />
    <td>
        <apex:outputText value="{!c.FirstName}"/><br />
        <apex:outputText value="{!c.LastName}"/><br />
        <apex:outputText value="{!c.Email}"/><br />
    </td>
    </apex:repeat>
</tr>
</table>
1
  • Thanks Jeremy. Worked perfectly. I had a a less than ideal situation where this absolutely had to be in an apex:pageBlockSectionItem so I just had to nest this inside of an apex:outputPanel inside of the apex:pageBlockSectionItem Oct 8 '12 at 12:01
3

The description in the developers guide doesn't make sense to me based on how it functions, but the rowspan attribute on apex:column gets this done:

<apex:dataTable value="{!myContacts}" var="c">
    <apex:column rowspan="{!myContacts.size}">
        <apex:outputText value="{!c.FirstName}" /><br />
        <apex:outputText value="{!c.LastName}"/><br />
        <apex:outputText value="{!c.Email}"/><br />
    </apex:column>
</apex:dataTable>
1

There are a couple of solutions I've seen:

The Salesforce guide they point out a property, columns, that sets the number of columns in the table. The developer board shows a solution with a custom controller that sets the number of columns dynamically.

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.