You need to sign in to do that
Don't have an account?
Liz Gibbons 16
Group records by picklist value in table
I am trying to create a table that displays Asset__c records grouped by a picklist field, Category__c. I've got the table displaying the Categories, but cannot get the Assets to display. Here's what is looks like now:
Here's the code I've got so far:
Class
Table
Here's the code I've got so far:
Class
public with sharing class ListViewController { public List<String> categories {get; set;} public List<Asset__c> tessa {get; set;} public ListViewController(){ categories = new List<String>(); Schema.DescribeFieldResult fieldResult = Asset__c.Category__c.getDescribe(); System.debug(fieldResult); List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues(); System.debug('--ple-->> ' + ple); for(Schema.PicklistEntry f : ple){ categories.add(f.getLabel()); } tessa = [select Name, Category__c, LastModifiedDate from Asset__c]; } }
Table
<div class="view" id="list"> <div class="panel panel-default"> <div class="panel-body"> <div class="row"> <div class="col-lg-2"> <div class="form-group"> <input type="text" class="form-control input-sm form-control-flat" placeholder="{!$Label.LBL_Search_Asset}" onkeyup="checksearch(this);"/> </div> </div> </div> <apex:form id="tableAssets"> <table id="basic-datatable" class="table table-bordered"> <thead> <tr> <th id="first-th" class="click-header" onclick="toogleall(this);"><i class="fa fa-plus-square-o" title="{!$Label.LBL_Show_Hide_All}"></i></th> <th width="60%">{!$ObjectType.Asset__c.Fields.Name.Label}</th> <th>{!$ObjectType.Asset__c.Fields.LastModifiedDate.Label}</th> </tr> </thead> <tbody> <apex:variable var="index" value="{!0}" /> <apex:repeat value="{!categories}" var="cat"> <tr class="category-row" id="{!cat}" onclick="togglerows(this);"> <td class="hidden"><label></label></td> <td colspan="3"><i class="fa fa-plus-square-o"></i> <span class="name">{!cat}</span></td> <td class="hidden"><span></span></td> </tr> <apex:repeat value="{!tessa}" var="a"> <tr class="category-in" style="display:none;"> <td class="asset-name"><span>{!a.Name}</span></td> </tr> <apex:variable var="index" value="{!index+1}" /> </apex:repeat> </apex:repeat> </tbody> </table> </apex:form> </div> </div> </div>
--KC
That does get me part of the way there. But now I've got every Asset listed under each Category. I'm aiming to have only those Assets with the Category value appear under it in the table.
-Liz
1. If assets are related to the categories they belong to, use a single query to retrieve categories and assets using a subquery. Then when you use an apex repeat on the categories, you can use another repeat on the category.assets.
2. You create a map of Map<String, List<Asset__c>> where the string is the category name, and then you retrieve it in the visualforce like
<apex:repeat value="{!assetsByCagetory[cat.Name]}" var="a">. You have to do a little work in the controller to set this up.
Both examples are in the code below.
--KC