Find your content:

Search form

You are here

How do I create a table with more than one column from a single list?

 
Share

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.


Attribution to: Peter Knolle

Possible Suggestion/Solution #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.


Attribution to: Mike Chale

Possible Suggestion/Solution #2

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>

Attribution to: Phil Rymek

Possible Suggestion/Solution #3

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>

Attribution to: Jeremy Nottingham
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1766

My Block Status

My Block Content