I am trying to create a Matrix table something like below

enter image description here

I have a problem in looping the apex repeat. I have a MAP defined like MAP<Priority,MAP<Status,integer>> , I am unable to iterate it and show it on VF page in a table.Kindly help me with the below scenario

My Controller Code:

public class tableProrityStatus_ctrl {

    public List<string> status{get;set;}
    public List<AggregateResult> lstPriorityLow = new List<AggregateResult>();
    public List<AggregateResult> lstPriorityHigh = new List<AggregateResult>();
    public List<AggregateResult> lstPriorityMedium = new List<AggregateResult>();
    public Map<String,integer> getPLowStatus = new Map<String,integer>();
    public Map<String,integer> getPMediumStatus = new Map<String,integer>();
    public Map<String,integer> getPHighStatus = new Map<String,integer>();
    public Map<String,Map<String,integer>> mapTable {get;set;}

    public tableProrityStatus_ctrl(){
    mapTable = new Map<String,Map<String,integer>>();
        status = new list<string>();
        Schema.DescribeFieldResult fieldResult = Test_Case__c.Status__c.getDescribe();
        System.debug('The value Schema.DescribeFieldResult------------->'+ fieldResult);
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues(); 
        system.debug('--ple -->>'+ple);     

        for( Schema.PicklistEntry f : ple){        
        System.debug('The value of status------------>'+status);

    lstPriorityLow = [Select Count (ID),Status__c total from Test_Case__c where Priority__c='Low' group by Status__c];
    System.debug('The value of lstPriorityLow------------>'+lstPriorityLow);
    getPLowStatus = processingPrority(lstPriorityLow);

    lstPriorityMedium = [Select Count (ID),Status__c total from Test_Case__c where Priority__c='Medium' group by Status__c];
    System.debug('The value of lstPriorityMedium------------>'+lstPriorityMedium);
    getPMediumStatus = processingPrority(lstPriorityMedium);

    lstPriorityHigh = [Select Count (ID),Status__c total from Test_Case__c where Priority__c='High' group by Status__c];
    System.debug('The value of lstPriorityHigh------------>'+lstPriorityHigh);
    getPHighStatus = processingPrority(lstPriorityHigh);

    System.debug('The value of getPLowStatus------------>'+getPLowStatus);
    System.debug('The value of getPMediumStatus------------>'+getPMediumStatus);
    System.debug('The value of getPHighStatus------------>'+getPHighStatus);


    System.debug('The value of mapTable------------>'+mapTable);

    public Map<String,integer> processingPrority(List<AggregateResult> getneededValues){

        Map<String,integer> mapMethodReturn = new Map<String,integer>();
        Map<String,integer> checkmap = new Map<String,integer>();

        if(getneededValues.size() != 0){            
                for(AggregateResult ar:getneededValues){ 
        System.debug('The value of checkmap------------>'+checkmap);

            for(string strput : status){
        System.debug('The value of mapMethodReturn------------>'+mapMethodReturn);  
            for(String str:status){
        system.debug('The value of mapMethodReturn---------------->'+mapMethodReturn);
        return mapMethodReturn;
} }

VF Page :

<apex:page controller="tableProrityStatus_ctrl" >
  <apex:pageBlock id="table">
<apex:repeat value="{!mapTable}" var="outerKey">
                   <apex:pageBlockTable value="{!mapTable[outerKey]}" var="innerkey"> 
                   <apex:repeat value="{!status}" var="str">
                    <apex:column headervalue="{!str}" > 
                    <apex:outputText value="{!mapTable[outerKey][str]}"/>


I am priority in the Map getting looped and displaying multiple times, screen shot of the output

enter image description here

Thanks for the help

  • You shouldn't need to use a repeat at all when you output your table. All the values you're outputting should be from aggregate results. It should only take one pass to go through the the entire table. It appears that you're repeating for each time you've put something into your maps when you only need to get the final aggregate values. – crmprogdev Nov 11 '16 at 21:17
  • @crmprogdev Thanks, even i though that , but i am not sure how to declare the variable for looping through the nested map key. I am unable to declare apex:variable and use it as a reference input for another repeat ! Like [link] (<apex:variable value="{!mapTable}" var="outerKey"> <apex:pageblocktable value="{!mapTable[outerKey]}" var="innerkey"> <apex:repeat value="{!mapTable[outerKey][innerkey]}" var="innerkey">)_italic_ bold code – Venkatesh Kumar Sakthivel Nov 12 '16 at 3:59

The issue you're having is that declared your class as Map<String,Map<String,integer>> instead of a zero length list which is causing difficulty pulling what you need from it. Instead (using pseudo-code), your class needs to be declared along the lines as follows:

list<MyClass>classListName = string[]titles + list< string + list<AggregateResult>string[n]>; 

Where string and string[n] represent the final values for low, med, high & total. String[]titles is a string array of your table column header values.

You'll probably want to create a separate class that will return the list with the final values for low, med, high & total that you use to pass in the names of those strings into the your main class with.

Once you do that, you'll only need to iterate through the list that you've declared in your main class.

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.