+ Start a Discussion
Umair Khan 19Umair Khan 19 

Grouping and Subgrouping the data

I have a Requirement of Grouping the data by distributor name and subgrouping by Created Date.
can anyone tell me how to achieve this.
User-added image
Best Answer chosen by Umair Khan 19
Ramesh DepaiahRamesh Depaiah
@Umair

VF Page:
<apex:page controller="Distributor" showHeader="false" >
    
    <apex:repeat value="{!allDistributions}" var="key">
        
        <apex:outputText value="{!Key}" /><br/>
        <apex:repeat value="{!allDistributions[key]}" var="item">
            <apex:outputText value="{!item}" /><br/>
            <apex:repeat value="{!allDistributions[key][item]}" var="option">
                <input type="radio" value="{!option}"/> {!option}

            </apex:repeat>
            
        </apex:repeat>
    </apex:repeat>
</apex:page>

Controller:
public class Distributor {
    public static Map<string,Map<date,List<string>>> allDistributions{get;set;}
   
    public Distributor()
    {        
        Map<string,Map<date,List<string>>> mapOfGroupbyDist=new  Map<string,Map<date,List<string>>>();       
        Map<string,List<Distributor__c>> mapOfDist=new   Map<string,List<Distributor__c>>();
        for(Distributor__c dist: [select id,Name,Options__c,createdDate from Distributor__c ])
        {
            if(!mapOfDist.containsKey(dist.Name))
                mapOfDist.put(dist.Name, new List<Distributor__c>());            
            mapOfDist.get(dist.Name).add(dist);
        }
        for(string distName:mapOfDist.keyset())
        {
            
            if(!mapOfGroupbyDist.containsKey(distName))
                mapOfGroupbyDist.put(distName, new Map<date,List<string>>()); 
            
            Map<date,List<string>> mapOfGroupbyDate=new  Map<date,List<string>>();
            List<Distributor__c> lstDist=mapOfDist.get(distName);
            for(Distributor__c dist:lstDist)
            {
                if(!mapOfGroupbyDate.containsKey(dist.CreatedDate.date()))
                    mapOfGroupbyDate.put(dist.CreatedDate.date(), new List<string>()); 
                mapOfGroupbyDate.get(dist.CreatedDate.date()).add(dist.Options__c);
            }
            
            mapOfGroupbyDist.get(distName).putAll(mapOfGroupbyDate);
        }
        
        allDistributions= mapOfGroupbyDist;
    }
    
}

Thanks
​​​​​​​Ramesh
 

All Answers

Ramesh DepaiahRamesh Depaiah
@Umair
I've replicated this to Lightning component Please let me know if it works
Component:
<aura:component controller="Distributor" 
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId"
                access="global" >
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    
    <aura:attribute name="distributions" type="List" default="[]" />
    Distributions
    <aura:iteration items="{!v.distributions}" var="distribution" indexVar="key">
        <div class="Name-Item">{!distribution.key}</div> 
        <aura:iteration items="{!distribution.value}" var="item" indexVar="key">
            <div class="Date-Item">{!item.key}</div> 
            <aura:iteration items="{!item.value}" var="option" indexVar="key">
                <div class="Option-Item">{!option}</div> 
            </aura:iteration>
        </aura:iteration>
    </aura:iteration>
    
</aura:component>

Js Controller:
({
    doInit : function(component, event, helper) {
        var action = component.get("c.getDistributions");   
        console.log('Distr: ');
        action.setCallback(this, function(resp) {
            const state = resp.getState();
            console.log('Distr: ',state);
            if (state === "SUCCESS") {
                var conts=resp.getReturnValue(); 
                console.log('Distr: ',conts);
                var custs = [];
                for(var key in conts){
                    var items = [];
                    for(var j in conts[key]){
                        items.push({value:conts[key][j], key:j});
                    }
                    custs.push({value:items, key:key});
                }                
                component.set('v.distributions',custs);
                console.log(custs);
            }
        });       
        $A.enqueueAction(action);    
    }
})

Apex Controller:
public class Distributor {
    
    @AuraEnabled
    public static  Map<string,Map<date,List<string>>> getDistributions()
    {        
        Map<string,Map<date,List<string>>> mapOfGroupbyDist=new  Map<string,Map<date,List<string>>>();       
        Map<string,List<Distributor__c>> mapOfDist=new   Map<string,List<Distributor__c>>();
        for(Distributor__c dist: [select id,Name,Options__c,createdDate from Distributor__c ])
        {
            if(!mapOfDist.containsKey(dist.Name))
                mapOfDist.put(dist.Name, new List<Distributor__c>());            
            mapOfDist.get(dist.Name).add(dist);
        }
        for(string distName:mapOfDist.keyset())
        {
            
            if(!mapOfGroupbyDist.containsKey(distName))
                mapOfGroupbyDist.put(distName, new Map<date,List<string>>()); 
            
            Map<date,List<string>> mapOfGroupbyDate=new  Map<date,List<string>>();
            List<Distributor__c> lstDist=mapOfDist.get(distName);
            for(Distributor__c dist:lstDist)
            {
                if(!mapOfGroupbyDate.containsKey(dist.CreatedDate.date()))
                    mapOfGroupbyDate.put(dist.CreatedDate.date(), new List<string>()); 
                mapOfGroupbyDate.get(dist.CreatedDate.date()).add(dist.Options__c);
            }
            
            mapOfGroupbyDist.get(distName).putAll(mapOfGroupbyDate);
        }
        
        return mapOfGroupbyDist;
    }
    
}

Result:​​​​​​​
User-added image


Thanks
​​​​​​​Ramesh
Umair Khan 19Umair Khan 19
thank you so much, Ramesh, But I will be more helpful if you tell me how to archive this in visualforce as I also need that radio button based on that some actions will be performed and I am very new to lightning so won't be able to complete my full requirement on lightning Platform. 
Ramesh DepaiahRamesh Depaiah
@Umair

VF Page:
<apex:page controller="Distributor" showHeader="false" >
    
    <apex:repeat value="{!allDistributions}" var="key">
        
        <apex:outputText value="{!Key}" /><br/>
        <apex:repeat value="{!allDistributions[key]}" var="item">
            <apex:outputText value="{!item}" /><br/>
            <apex:repeat value="{!allDistributions[key][item]}" var="option">
                <input type="radio" value="{!option}"/> {!option}

            </apex:repeat>
            
        </apex:repeat>
    </apex:repeat>
</apex:page>

Controller:
public class Distributor {
    public static Map<string,Map<date,List<string>>> allDistributions{get;set;}
   
    public Distributor()
    {        
        Map<string,Map<date,List<string>>> mapOfGroupbyDist=new  Map<string,Map<date,List<string>>>();       
        Map<string,List<Distributor__c>> mapOfDist=new   Map<string,List<Distributor__c>>();
        for(Distributor__c dist: [select id,Name,Options__c,createdDate from Distributor__c ])
        {
            if(!mapOfDist.containsKey(dist.Name))
                mapOfDist.put(dist.Name, new List<Distributor__c>());            
            mapOfDist.get(dist.Name).add(dist);
        }
        for(string distName:mapOfDist.keyset())
        {
            
            if(!mapOfGroupbyDist.containsKey(distName))
                mapOfGroupbyDist.put(distName, new Map<date,List<string>>()); 
            
            Map<date,List<string>> mapOfGroupbyDate=new  Map<date,List<string>>();
            List<Distributor__c> lstDist=mapOfDist.get(distName);
            for(Distributor__c dist:lstDist)
            {
                if(!mapOfGroupbyDate.containsKey(dist.CreatedDate.date()))
                    mapOfGroupbyDate.put(dist.CreatedDate.date(), new List<string>()); 
                mapOfGroupbyDate.get(dist.CreatedDate.date()).add(dist.Options__c);
            }
            
            mapOfGroupbyDist.get(distName).putAll(mapOfGroupbyDate);
        }
        
        allDistributions= mapOfGroupbyDist;
    }
    
}

Thanks
​​​​​​​Ramesh
 
This was selected as the best answer
Umair Khan 19Umair Khan 19
@Ramesh
Thank you, This really helped..... :)