Example Programs using List, Apex and Visualforce
List of objects demo:
Public class example {
Public list <account> result {set; get;}
Public list example ()
{
Account a1 = new account (name = ‘Sam’ industry = ‘banking’)
Account a2 = new account (name = ‘ram’ industry = ‘energy);
result = new list <account> ();
result. add (a1);
result. add (a2);
}
}
<apex: page controller = “list example”>
<apex: page Block>
<apex: page block Table>
<apex: page>
Create a list of apex class object:
1. Student apex:
Global student
{
Public string name {get; set;}
Public integer age {get; set;}
Public student (string name, integer age)
{
This. name = name;
This. age = age;
}
}
2. List example apex:
Public class list example
{
Public list <student> result {set; get;}
Public list example ().
{
result = new list <student> ();
student s1 = new student (‘sam’,20);
student s2 = new student (‘Ram’,40);
student s3 = new student (Praveen’,40);
result. add (s1);
result. add (s2);
result. add(s3);
}
}
List example visual force:
<apex: page controller = “List example”>
<apex: page block>
<apex: page block Table value = ‘’ {! Result} “var= ‘a’>
<apex: column value = “{! a.name}’/>
<apex: column value = “{! a. age}”/>
<apex: page block Table>
<apex: page>
Write an apex program to generate list of select options:
Public class list select
{
Public string my val {get; set;}
Public list < select option > my option;
Public list <select option > get my options ()
{
Return my options;
}
Public List select ()
{
My options = new List <select option> ();
Select option s = new selection (‘null’,’-none- ‘);
Select option s1 = new select option (‘one’, ‘Jan’);
My options. add (s);
My options. add (s1);
My options. Add (new select option (‘two’,’feb’);
My options. add (new select option (‘three’, ‘mar’);
}
}
<apex: page controller = “list select”>
<apex: from>
<apex: select list size = “1” value = “{! my value}”/
<apex: select options value = “{! My options} “/>
<apex: action support event =” unchanged’’ retender = “one”/>
<apex: select List>
<apex: output label id = “one” > you have selected {! My val}
<apex: output label>
<apex: from>
<apex: page>