2

I'm using visalforce page and apex class for rendering the table on the browser. I want to display 6 records per row. I have total 4 rows & 5 columns. How I can use the repeat function to limit 6 records per row and remaining will be on next row.

<apex:repeat value="{!Appot}" var="string" id="Vers">
    <td class='tblSet'>
        <apex:outputText value="{!string}" id="T"/>
    </td>
</apex:repeat> 

From apex code I'm passing an array of Integer.

Thanks for help!

  • Does it have to be a table that you're adding it to? If not, a css solution might work for you. – Nick Cook Aug 24 '16 at 3:03
2

You can use apex:variable to iterate throught your data, and for each sixth element rerender break line

public class repeatCon {
    public String[] getStrings() {
        return new String[]{'one','two','three','four','five','six','seven'};
    }
}

<apex:page controller="repeatCon" id="thePage">
    <apex:variable var="i" value="{!0}"/>
    <apex:repeat value="{!strings}" var="string">
        <apex:variable var="i" value="{!i+1}"/>
            <apex:outputText value="{!string}"/>&nbsp;
            <apex:outputText value="<br/>" rendered="{!MOD(i, 3)==0}" escape="false"/>
    </apex:repeat>
</apex:page>

Output:

enter image description here

| improve this answer | |
  • The documentation says that reassigning an apex:variable in an iteration component is unsupported. Does it work though? – martin Aug 24 '16 at 3:33
  • @martin, seems working, can differs on version I suppose. Works for 36 and 37 – Ilya Lepesh Aug 24 '16 at 3:36
  • Interesting. They might have updated the functionality without updating their documentation. Otherwise I'm not sure why they went out of their way to say it isn't supported. Good to know it works though. – martin Aug 24 '16 at 3:40
1

If you want to do this in Visualforce, you could change your data structure from a list of plain Integers to a list of a class that holds the integer and the integer's position in the list.

public class MyController {

    public list<MyInteger> ilist {get; set;}

    public class MyInteger{
        public Integer Index {get; set;}
        public Integer Value {get; set;}
        MyInteger(Integer i, Integer v){Index = i; Value = v;}
    }

    public MyController(){
        ilist = new list<MyInteger>();
        list<Integer> OriginalList = new list<Integer>{42,786,12,2,75,95,654,21,35,78,35,75};
        for (Integer i = 0 ; i < OriginalList.size() ; i++) ilist.add(new MyInteger(i,OriginalList[i]));
    }

}

Then you could conditionally render a new row for every 6th element in the list.

<table>
    <apex:repeat value="{!ilist}" var="i">
        <apex:outputText value="<tr>" escape="false" Rendered="{!MOD(i.Index,6)==0}"/>        
        <td>{!i.Value}</td>
    </apex:repeat>
</table>
| improve this answer | |
0

Another idea would be to use a List<List<Type__c>> to hold your values.

Controller:

//recs = list of your records
List<List<Type__c>> appotList = new List<List<Type__c>>();
for (integer j = 0, index = 0, recSize = recs.size();j < recSize;++index)
{
    appotList.add(new List<Type__c>());
    for (integer i = 0; i < 6 && j < recSize; ++i)
    {
        appotList[index].add(recs[j++]);
    }
}

Visualforce:

<apex:repeat value="appotList" var="innerList">
    <tr>
        <apex:repeat value="innerList" var="record">
            <td class='tblSet'>
               <apex:outputText value="{!record}" id="T"/>
            </td>
        </apex:repeat>
    </tr>
</apex:repeat>
| improve this answer | |

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.