0

I am trying to iterate map<Integer,List> on the vf page using apex:PageBlockTable. Here List contains multiple accounts. I am getting each value of map in a single row. How do I get each account in single row using map and apex:PageBlockTable.

Visualforce Page:

 <apex:page controller="ShowAccountsController">
   <apex:form >
      <apex:commandButton value="Show Accounts" action="{!displayAccounts}" />
    <apex:pageBlock title="Accounts" id="accountPageBlock"> 
        <apex:inputFile  value="{!fileBody}" />
        <apex:pageBlockTable value="{!mapOfPageNumberAndRecords}" var="indexValue" >
           <apex:repeat value="{!mapOfPageNumberAndRecords[indexValue]}" var="ac">
           <apex:column>
               <apex:facet name="header"> Account Name</apex:facet>
                <apex:outputText value="{!ac.Name}"/>
            </apex:column>
            <apex:column>
                 <apex:facet name="header"> Account Id</apex:facet>
                <apex:outputText value="{!ac.Id}"/>
            </apex:column>
            </apex:repeat>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>

</apex:page> Controller:

public class ShowAccountsController {
Public Integer Page_Size;
Public Map<Integer,List<Account>> mapOfPageNumberAndRecords  {get;set;}
Public List<Account> emptyList {get;set;}
Public Integer currentPageNumber {get;set;}
Public Integer Counter;
Public Integer Page_Number;
public Blob fileBody { get; set; }
public ShowAccountsController(){
    currentPageNumber = 1;
    Page_Size = 1000;
    Counter = 0;
    Page_Number =0;
    mapOfPageNumberAndRecords = new Map<Integer,List<Account>>();
    emptyList = new List<Account>();
    mapOfPageNumberAndRecords.put(null, new List<Account>());
    //retrieveAccountRecords(); 
    system.debug('mapOfPageNumberAndRecords is '+mapOfPageNumberAndRecords);
}
public void displayAccounts(){
    retrieveAccountRecords(); 
}
public void retrieveAccountRecords(){
    List<Account> listOfAccountsPerPage = new List<Account>();
    List<Account> listOfAccountRecords = [select id,name,Industry from account];
    Integer totalRecordsProcessed = 0;
    Integer remainingRecords = 0;
    if(listOfAccountRecords.size()<Page_Size){
         Page_Number = Page_Number+ 1;
        mapOfPageNumberAndRecords.put(Page_Number,listOfAccountRecords);
    }
    else{
        for(Account accountRec : listOfAccountRecords){
            totalRecordsProcessed++;
            Counter++;
            listOfAccountsPerPage.add(accountRec);
            if(Counter == Page_Size){
                system.debug('Counter is '+Counter);
                system.debug('totalRecordsProcessed is '+totalRecordsProcessed);
                Page_Number = Page_Number+ 1;
                mapOfPageNumberAndRecords.put(Page_Number,listOfAccountsPerPage);
                remainingRecords = listOfAccountRecords.size() - totalRecordsProcessed;
                if(remainingRecords <= Page_Size && remainingRecords!=0){
                     Page_Size = listOfAccountRecords.size()-totalRecordsProcessed;
                }
                system.debug('Page_Size is '+Page_Size);
                Counter =0;
                listOfAccountsPerPage = new List<Account>();
            }
        }
    }
}

my output Looks like this: enter image description here

But I want each account in a list in each row. Any help?

1 Answer 1

0

apex:pageBlockTable already acts as an iterator, so you don't need (nor want) apex:repeat to get involved here. Every instance of apex:column creates a new column, so you end up with a multiplicative effect, as you've observed. You can also remove quite a bit of "fluff" that you have in your code. This minimum example should work.

<apex:pageBlockTable value="{!mapOfPageNumberAndRecords}" var="indexValue" >
    <apex:column headerValue="Account Name">
        {!mapOfPageNumberAndRecords[indexValue].Name}
    </apex:column>
    <apex:column headerValue="Account Id">
        {!mapOfPageNumberAndRecords[indexValue].Id}
    </apex:column>
</apex:pageBlockTable>
1
  • thanks for helping. when I used the code you mentioned I am getting the below error. Can you please help me? Incorrect parameter type for subscript. Expected Number, received Text Error is in expression '{!mapOfPageNumberAndRecords[indexValue].Name}' in component <apex:column> in page showaccounts
    – Niharika B
    Commented May 24, 2022 at 13:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .