+ Start a Discussion
Satish PrajapatSatish Prajapat 

Display List on VF page from apex class

  • I have class A and I want to display List<A> on VF Page.
  • // Class A
    public with sharing class A
    {
        public String p;
        public String q;
        public String r;
        public String s;
        public String t;
    }
    
    // My Apex Code
    public class MyApexClass
    {
        public void m1()
        {
            StaticResource sr = [select id,body from StaticResource Where Name = 'MyFile'];
            String contents = sr.body.toString();
            List<A> list_obj = new List<A>();
            for(String line : contents.split('\n')) 
            {
                String[] myArray= line.split('#');
                A a = new A();
                a.p = myArray[0];
                a.q  = myArray[1];
                a.r  = myArray[2];
                a.s  = myArray[3];
                a.t  = myArray[4];
                list_obj.add(a) ;
            }
        }
    }

     

 
Best Answer chosen by Satish Prajapat
SaketJoshiSaketJoshi
You can use <apex:repeat> block in order to display list contents on the VF page. For instance, this will display value on the page -
public with sharing class MyApexClass {
    public List<A> list_obj = new List<A> () {get;set;}

    public MyApexClass() {
        // add items to the list list_obj
    }

    public with sharing class A {
        public String p {get;set;}
    }
}

<apex:page controller="MyApexClass">
    <apex:repeat value="{!list_obj}" var="obj">
        {!obj.p}
    </apex:repeat>
</apex:page>

For more info. on apex repeat - https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_repeat.htm (https://developer.salesforce.com/forums/?id=906F0000000BSriIAG)
https://developer.salesforce.com/forums/?id=906F0000000BSriIAG

All Answers

Prithviraj_ChavanPrithviraj_Chavan
Hi Satish,
// My Apex Code
public class MyApexClass
{
    public void m1()
    {
        StaticResource sr = [select id,body from StaticResource Where Name = 'MyFile'];
        String contents = sr.body.toString();
        List<A> list_obj = new List<A>();
        for(String line : contents.split('\n')) 
        {
            String[] myArray= line.split('#');
            A a = new A();
            a.p = myArray[0];
            a.q  = myArray[1];
            a.r  = myArray[2];
            a.s  = myArray[3];
            a.t  = myArray[4];
            list_obj.add(a) ;
        }
    }
    public with sharing class A
    {
        public String p;
        public String q;
        public String r;
        public String s;
        public String t;
    }
}

 
Satish PrajapatSatish Prajapat

Hello Prithviraj,
What is the benefit if I use this inside the class.

Still my doubt is present that how to display List on VF Page.
 

Prithviraj_ChavanPrithviraj_Chavan
Hi.. can you please write some output which shows how exctly u want it on page so that I can help you
SaketJoshiSaketJoshi
You can use <apex:repeat> block in order to display list contents on the VF page. For instance, this will display value on the page -
public with sharing class MyApexClass {
    public List<A> list_obj = new List<A> () {get;set;}

    public MyApexClass() {
        // add items to the list list_obj
    }

    public with sharing class A {
        public String p {get;set;}
    }
}

<apex:page controller="MyApexClass">
    <apex:repeat value="{!list_obj}" var="obj">
        {!obj.p}
    </apex:repeat>
</apex:page>

For more info. on apex repeat - https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_repeat.htm (https://developer.salesforce.com/forums/?id=906F0000000BSriIAG)
https://developer.salesforce.com/forums/?id=906F0000000BSriIAG
This was selected as the best answer