function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HarryHHHarryHH 

List of Lists in Visualforce

Hello,

 

this is probably a very stupid question: I have a List of shipping notes, each of which having different items. I now want to display them in one visualforce page somehow like this:

 

shipping note 1; fields of note 1

  item 1of note 1; fields of item 1

  item 2of note 1; fields of item 2

  .

  .

  .

Shipping note 1; fields of note 2

  item 1of note 2; fields of item 1

  item 2 of note 2; fileds of item 2

  .

  .

 .

 .

 

Which Visualforce tags are the right ones for this? Thanks!

 

Harry

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

I'm not 100% sure what you are asking for but I believe you want to know how to use a single SOQL query in order to build your List<List<ShippingItem>>.  I'll explain what I think is your question, and this link explains all of the different types of joins you may want to do in your SOQL if my answer isn't what you want.

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm

 

SELECT name, (SELECT name FROM Shipping-Item) From Shipping-Note WHERE (whatever your criteria is here for shipping-notes)

This will give you a list of 'shipping-notes' and inside of this list you will also have a list of 'Shipping-Items'.

 

My apologies if I am completely off, my lack of sleep from last night has me feeling a little confused today.

All Answers

Damien_Damien_

//Controller

List<List<ShippingNote>> myNotes{get;set;}

 

//Visualfforce Page

<apex:repeat value="{!myNotes}" var="note">

  <apex:repeat value="{!note}" var="item">

    <apex:outputText value="{!item.name}" /><br/>

  </apex:repeat>

</apex:repeat>

 

//You will need to initialize and set your lists somewhere so that they have values for the visualforce page

//This will pretty much just print out the name of each item on a separate line.

//This code will need adjusted to suit your needs.  Hopefully this helps you.

HarryHHHarryHH

Hello Damien,

 

thanks for your reply. One more question: The shipping notes are the master of a Master-Detail Relationship to the shipping items. Therefore I would say, that the list of Lists should be somehow like that: List<List<shipping-item>> with the needed shipping-note__r fields in the SOQL.

What do you mean?

 

Thanks

Harry

Damien_Damien_

I'm not 100% sure what you are asking for but I believe you want to know how to use a single SOQL query in order to build your List<List<ShippingItem>>.  I'll explain what I think is your question, and this link explains all of the different types of joins you may want to do in your SOQL if my answer isn't what you want.

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm

 

SELECT name, (SELECT name FROM Shipping-Item) From Shipping-Note WHERE (whatever your criteria is here for shipping-notes)

This will give you a list of 'shipping-notes' and inside of this list you will also have a list of 'Shipping-Items'.

 

My apologies if I am completely off, my lack of sleep from last night has me feeling a little confused today.

This was selected as the best answer
Ashwin KhedekarAshwin Khedekar
Use apex:pageBlockTable as a child tag of apex:repeat tag to display list of lists in Visualforce page.

Visualforce page :- Name :- vfListOfListCtrl :- Browse for this Visualforce page by appending /apex/vfListOfListCtrl to the URL of the browser :-
<apex:page controller="ListOfListCtrl">
  <apex:form>
    <apex:pageBlock>
      
      <apex:repeat value="{!listOfList}" var="accLst">
        <!-- Each accLst is a collection of 1000 account records. As pageBlockTable can hold a maximum of 1000 records. -->
        <apex:pageBlockTable value="{!accLst}" var="acc">
          <apex:column title="Id" value="{!acc.Id}"/>
          <apex:column title="Name" value="{!acc.Name}"/>
        </apex:pageBlockTable>
      </apex:repeat>
      
    </apex:pageBlock>
  </apex:form>
</apex:page>

The controller class :- Name :- ListOfListCtrl.cls :-
public class ListOfListCtrl
{
    public List<List<Account>> listOfList;
    
    public ListOfListCtrl()
    {
        listOfList = new List<List<Account>>();

        // Insert 10000 first into database :- Accounts with name ListAcc1 to ListAcc10000.
        // Script for this is given later ahead :-        
        List<Account> queriedLst = [Select id, name from Account where name like 'ListAcc%'];
        System.debug(LoggingLevel.INFO, '#### queriedLst size is ' + queriedLst.size());

        // Add List<Account> to listOfList containing 1000 account records.
        // listof1000 will hold the 1000 account records.
        // index 0 to 999, list indexes start from 0. So add 1000 records at a time as apex:pageBlockTable can hold
        // a maximum of 1000 records.
        List<Account> listOf1000 = new List<Account>();
        for(integer i = 0; i <= 999; i++)
        {
            listOf1000.add(queriedLst[i]);
        }
        listOfList.add(listOf1000);

        // Add another List<Account> to listOfList containing 1000 account records        
        // index 1000 to 1999
        listOf1000 = new List<Account>(); // reassign new empty memory space to this variable
        for(integer i = 1000; i <= 1999; i++)
        {
            listOf1000.add(queriedLst[i]);
        }
        listOfList.add(listOf1000);
        // By now 2000 account records have been added to listOfList

        // Again assigning the same 1000 records to listOfList is ok, as duplicates are allowed in List.
        listOf1000 = new List<Account>();
        for(integer i = 1000; i <= 1999; i++)
        {
            listOf1000.add(queriedLst[i]);
        }
        listOfList.add(listOf1000);
        // By now 3000 account records have been added to listOfList
        
        // This should print 3 as three List<Account> have been added so far to listOfList.
        System.debug(LoggingLevel.INFO, '#### listOfList size is ' + listOfList.size());
    }
    
    public List<List<Account>> getListOfList()
    {
         return listOfList;
    }
}

Script for inserting 10000 records into database :- Accounts with name ListAcc1 to ListAcc10000 :-
Run this script from Execute Anonymous in Developer Console :-
List<Account> accLst = new List<Account>();
for(integer i = 1; i <= 10000; i++)
{
    Account a = new Account(Name = 'ListAcc'+i);
    accLst.add(a);
}
insert accLst;
List<Account> queriedLst = [Select id, name from Account where name like 'ListAcc%'];
System.debug(LoggingLevel.INFO, '#### queriedLst size is ' + queriedLst.size());