2

I have has list with 20 values. I want to print them out First 10 values in one line and next 10 values in a new line.

VF Code:

<apex:outputtext value="{!listVals}"/>

Apex Code:

Public List<string> getlistVals()
{
List<String> Str = new List<String>();       
    for(integer i=1;i<=20;i++)
    {
    str.add(i);
    }
return str1;
}

}

My Output is: 1 2 3 4 5 6 7 8 9 10

But i want: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Any ideas?

  • I put your actual and expected outputs in a code block so that the new line is appearing correctly. – BarCotter Feb 5 '15 at 14:31
  • Have you tried adding '<br/>' in the list and then setting escape to false e.g. <apex:outputtext value="{!listVals}" escape="false"/>? – BarCotter Feb 5 '15 at 14:34
  • BarCotter: Is it possible to add the <br/> statement in apex code? It would be great if you share some code. – Rv1 Feb 5 '15 at 14:36
3

I don't think you can use an array inside your get list method, since it wouldn't really have a way to break up the values to multiple lines. If your goal is just to output the set of numbers in groups of 10, I would try something like this:

Public String getlistVals()
{
    String str = '';       
    for(integer i=1;i<=20;i++)
    {
        str += i + ' ';
        if(math.mod(i,10)==0)
        {
           str +='\n';
        } 
    }
    return str;
 }
| improve this answer | |
  • Thank u Jim. Inorder to have exact output in SF please replace '\n' with '<br/>'.. In SF \n won't works.. – Rv1 Feb 5 '15 at 14:51
2

If tabular display is acceptable, you could consider using an <apex:panelGrid /> tag for rendering the elements up to a defined number of columns. After the defined number, the table elements wrap to the next row.

apex:panelGrid documentation

The VF might look like this:

<apex:page>
    <apex:panelGrid columns="10" id="yourGrid">
        <apex:repeat value="{!listVals}" var="listItem">
            <apex:outputText value="{!listItem}" />
        </apex:repeat>
    </apex:panelGrid>
</apex:page>
| improve this answer | |
  • Mark: Since we're passing all the values in one list string , panelGrid concept can't be applied now – Rv1 Feb 5 '15 at 14:31
0

You could create two lists, with the correct values in them, and do something like this:

<apex:outputtext value="{!listVals1}"/>

<apex:outputtext value="{!listVals2}"/>
| improve this answer | |
  • Swisher: In my case it is a dynamic thing and can't be handled by creating the lists. – Rv1 Feb 5 '15 at 14:20
  • I guess I don't understand if your example code is conceptual or you literally want to show 1 - 10 on one line and 11 - 20 on another. In either case, I believe Mark's answer is the best answer. – Swisher Sweet Feb 5 '15 at 14:42
0

VF Code:

User escape attribute in tag. The attribute escape the line brakes or VF Code:

Apex Code

Public String getlistVals()
{
    String str = '';       
    for(integer i=1;i<=100;i++)
    {
        str += i + ' ';
        if(math.mod(i,10)==0)
        {
           str +='<Br/>';
        } 
    }
    return str;
 }
| improve this answer | |
  • <apex:outputText value="{!listVals}" escape="false"/> – Vikas Y Feb 5 '15 at 14:54
  • Please use the edit button at bottom left to edit your post to add your comment to update it for completeness. Select all of your code, then click on the pre-formatted text icon to have it display properly as code. Please visit the Help center if you need further assistance with the editor. Thank you. – crmprogdev Feb 5 '15 at 15:14

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.