Search In This Blog

2016-10-29

Output data in 3 columns by repeat in Visualforce


There're a lot way to output a list of strings in 3 columns, like 3*n.

Here are two ways.

First need to get the value of list before output, list should be able to public get. Here is an example.
// [sample] string[]
public String[] getSampleStrings() {
    return new String[]{'ONE','TWO','THREE','FOUR','FIVE','SIX','SEVEN','EIGHT','NINE'};
}
Way 1: Use first and rows in apex:outputPanel
// Repeat sample start (by first and rows)
<apex:outputpanel layout="block">
    <apex:repeat first="1" rows="3" value="{!sampleStrings}" var="sampStr">
        <apex:outputtext style="padding: 0 10px;" value="{!sampStr}">
    </apex:outputtext></apex:repeat>
</apex:outputpanel>
<apex:outputpanel layout="block">
    <apex:repeat first="4" rows="3" value="{!sampleStrings}" var="sampStr">
        <apex:outputtext style="padding: 0 10px;" value="{!sampStr}">
    </apex:outputtext></apex:repeat>
</apex:outputpanel>
  • first="1" means this repeat will start from list[0]
  • rows="3" means repeat 3 rows(times). When first="1", it will output like "list[0] list[1] list[2]". If you want 4 columns, set row="4".
  • first="4" means this repeat will start from list[3]. If you repeat in 4 columns, set first="5".
  • rows="3" means repeat 3 rows(times). When first="1", it will output like "list[0] list[1] list[2]".
It will output like below.
Advantage in controlling the output as you will.

Way 2: Set columns in apex:pageBlockSection
<apex:pageBlockSection columns="3">
    <apex:repeat value="{!sampleStrings}" var="sampStr">
        <apex:outputText value="{!sampStr}" style="padding:0 10px;" />
    </apex:repeat>
</apex:pageBlockSection>
columns="3" means repeat this list in 3 columns. If you repeat in 4 columns, set columns="4".
Output:
Advantage in outputting all data.

No comments:

Post a Comment