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
Sujendran Sundarraj 8Sujendran Sundarraj 8 

how to use map in the vf page to display both key and value

Hi I have an requirement, that my client want me to create a report like vf page to group by the cases based on the status. the status value should be collapsable and on expanding the section it has to display the list of cases under that status. I have done half of it but still getting the issue to display the count of cases next to the status. can anyone please help me? 
my code is 

VF: 
<apex:page controller="reporttobe" showHeader="false" >
    <apex:pageBlock title="report area">
    <apex:repeat value="{!result}" var="r">
    <apex:pageBlockSection title="{!r}:Here I want to display the count of cases basec on status">

    </apex:pageblockSection>
    </apex:repeat>
    </apex:pageBlock>
</apex:page>


Controller: 
public class reporttobe {
public map<String, Decimal> result{get; set;}


list<AggregateResult> stlist;
public reporttobe(){
stlist = [select status stat, count(casenumber) caseno from case group by status];


result = new map<String, Decimal>();
for(aggregateresult aggResult: stlist){
  result.put((String)aggResult.get('stat'), (Decimal)aggResult.get('caseno'));

}
system.debug(result);

}
}


Please help. 

Thank you.
Regards, 
Sujendran. 
 
Ramesh DRamesh D
@Sujendran
Please try using List
public class reporttobe {
public List<CaseData> result{get; set;}


list<AggregateResult> stlist;
public reporttobe(){
stlist = [select status stat, count(casenumber) caseno from case group by status];


result = new List<CaseData>();
for(aggregateresult aggResult: stlist){
CaseData casedat=new CaseData();
casedat.status=(String)aggResult.get('stat');
casedat.count=(Decimal)aggResult.get('caseno')
  casedat.add(CaseData);

}
system.debug(result);

}

public class CaseData{
public string status{get;set;} 
public decimal count{get;set;} 
}
}
<apex:page controller="reporttobe" showHeader="false" >
    <apex:pageBlock title="report area">
    <apex:repeat value="{!result}" var="r">
    <apex:pageBlockSection title="{!r.Count}:Here I want to display the count of cases basec on status">

    </apex:pageblockSection>
    </apex:repeat>
    </apex:pageBlock>
</apex:page>

I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D
 
Sujendran Sundarraj 8Sujendran Sundarraj 8
Hello Ramesh, 
Got it. Thank you so much. 
When the user clicks the pageblock section, I need to pass the title(status ) to the controller. Can we pass the title to controller. Please help me to fix it.
Thank you. 
Regards, 
Sujendran. 
Ramesh DRamesh D
@Sujendran,

Create a variable and assign value from VF
public String strStatus {get;set;}

Public void MyMethod(){
   string mystatus= strStatus;
   //Playaround
  }
Add a button something like this and assign the status to it 
<apex:commandButton value="Save" action="{!MyMethod}">
            <apex:param name="Status"
                value="{!r.Status}"
                assignTo="{!strStatus }"/>
        </apex:commandButton>

Thanks,
Ramesh D
Deepali KulshresthaDeepali Kulshrestha
Hi Sujendran,

try this below code:-
I have done this and it runs fine and change it according to your need.
-------------VF Page-------------
<apex:page controller="reporttobe" showHeader="false" >
    <apex:pageBlock title="report area">
        <apex:repeat value="{!result}" var="r">
            <apex:pageBlockSection title="{!r}:Here I want to display the count of cases basec on status">
                    {!result[r]}
            </apex:pageblockSection>
        </apex:repeat>
    </apex:pageBlock>
</apex:page>

-------------Apex Controller-----------------

public class reporttobe {
    public map<String, Decimal> result{get; set;}


    list<AggregateResult> stlist;
    public reporttobe(){
        stlist = [select status stat, count(casenumber) caseno from case group by status];


        result = new map<String, Decimal>();
        for(aggregateresult aggResult: stlist){
            result.put((String)aggResult.get('stat'), (Decimal)aggResult.get('caseno'));

        }
        system.debug(result);

    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha