0

I am trying to create a VF page with multiple columns of data from one controller. Here is the page so far:. What I need is for each of the Management Types to have it's own column and show all of the status and accounts. I understand I would need 8 columns for each category, and then tow columns under each category to show status and company name. I just cannot figure out if it is even possible. Also, is there a way to make the link on the company name so that it takes you to another vf page showing data for that particular account id, rather than the account page?

Controller:

    public class SortingController{
 // The list of all our accounts has a custom getter to sort immediately
public List<Account> accounts { get {
return sortList(accounts);
} set; }
// The string containing the column to filter by
public String sortBy { get; set; }
// The string containing the direction (ASC, DESC)
public String sortDir { get; set; }
// In the constructor we just define an initial list of accounts
public SortingController()
{
accounts = [Select Id,Name, Overall_Status__c,Account_Management_Type__c from Account where Account_Type_Bucket__c='Customer' order by Account_Management_Type__c] ;
}
public PageReference empty() { return null; }
// The sorting function works on all SObjects sorting on all fields
private List<SObject> sortList(List<SObject> cleanList)
{
// return the clean list if we don't have a sort field defined
if (sortBy == null) { return cleanList; }
// create an empty result List
List<SObject> resultList = new List<SObject>();
// and an object map which will be sorted
Map<Object, List<SObject>> objectMap = new Map<Object, List<SObject>>();
// now loop through the clean list and create the object map
for (SObject item : cleanList) {
if (objectMap.get(item.get(sortBy)) == null) {
objectMap.put(item.get(sortBy), new List<SObject>());
}
objectMap.get(item.get(sortBy)).add(item);
}
// get all the keys from the object map and sort them
List<Object> keys = new List<Object>(objectMap.keySet());
keys.sort();
// now create a result list by adding all objects from the object map
// in the sorted order
for(Object key : keys) {
resultList.addAll(objectMap.get(key));
}
// clear the clean list
cleanList.clear();
// depending on the direction add the items in the correct sorting to the clean list
if (sortDir == 'ASC') {
for (SObject item : resultList) {
cleanList.add(item);
}
} else {
for (Integer i = resultList.size()-1; i >= 0; i--) {
cleanList.add(resultList[i]);
}
}
// return the clean and now sorted list
return cleanList;
}

}

VF Page

<apex:page controller="SortingController">
    <apex:sectionHeader title="Accounts" subtitle="Any Employee, Any Customer, Any Time" />
    <apex:form >
        <apex:pageBlock title="Customer Accounts">
            <apex:pageBlockTable value="{!accounts}" var="account" width="50%">
                <apex:column width="5%">           
                    <apex:facet name="header" >
                        <apex:commandLink action="{!empty}">
                            <apex:outputText value="{!$ObjectType.Account.Fields.Account_Management_Type__c.Label}" />
                            <apex:outputText escape="false" rendered="{!sortBy=='Account_Management_Type__c'&&sortDir=='ASC'}">
                                <img src="/s.gif" alt="Sorted Ascending" class="sortAsc" title="Sorted Ascending" />
                            </apex:outputText>
                            <apex:outputText escape="false" rendered="{!sortBy=='Account_Management_Type__c'&&sortDir=='DESC'}">
                                <img src="/s.gif" alt="Sorted Descending" class="sortDesc" title="Sorted Descending" />
                            </apex:outputText>
                            <apex:param value="Overall_Status__c" name="column" assignTo="{!sortBy}" />
                            <apex:param value="{!IF(sortDir=='ASC'&&sortBy=='Account_Management_Type__c', 'DESC', 'ASC')}" name="direction" assignTo="{!sortDir}" />
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLink value="/{!account.Id}" >
                        <apex:outputText value="{!account.Account_Management_Type__c}"/>
                    </apex:outputLink>
                </apex:column>
                <apex:column width="1%">           
                    <apex:facet name="header" >
                        <apex:commandLink action="{!empty}">
                            <apex:outputText value="{!$ObjectType.Account.Fields.Overall_Status__c.Label}" />
                            <apex:outputText escape="false" rendered="{!sortBy=='Overall_Status__c'&&sortDir=='ASC'}">
                                <img src="/s.gif" alt="Sorted Ascending" class="sortAsc" title="Sorted Ascending" />
                            </apex:outputText>
                            <apex:outputText escape="false" rendered="{!sortBy=='Overall_Status__c'&&sortDir=='DESC'}">
                                <img src="/s.gif" alt="Sorted Descending" class="sortDesc" title="Sorted Descending" />
                            </apex:outputText>
                            <apex:param value="Overall_Status__c" name="column" assignTo="{!sortBy}" />
                            <apex:param value="{!IF(sortDir=='ASC'&&sortBy=='Overall_Status__c', 'DESC', 'ASC')}" name="direction" assignTo="{!sortDir}" />
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLink value="/{!account.Id}" >
                        <apex:outputText value="{!account.Overall_Status__c}" escape="false"/>
                    </apex:outputLink>
                </apex:column>
               <apex:column width="30%">
                   <apex:facet name="header" >
                       <apex:commandLink action="{!empty}">
                           <apex:outputText value="{!$ObjectType.Account.Fields.Name.Label}" />
                           <apex:outputText escape="false" rendered="{!sortBy=='Name'&&sortDir=='ASC'}">
                               <img src="/s.gif" alt="Sorted Ascending" class="sortAsc" title="Sorted Ascending" />
                           </apex:outputText>
                           <apex:outputText escape="false" rendered="{!sortBy=='Name'&&sortDir=='DESC'}">
                               <img src="/s.gif" alt="Sorted Descending" class="sortDesc" title="Sorted Descending" />
                           </apex:outputText>
                           <apex:param value="Name" name="column" assignTo="{!sortBy}" />
                           <apex:param value="{!IF(sortDir=='ASC'&&sortBy=='Name', 'DESC', 'ASC')}" name="direction" assignTo="{!sortDir}" />
                       </apex:commandLink>
                   </apex:facet>
                   <apex:outputLink value="/{!account.Id}" >
                        <apex:outputText value="{!account.Name}" />
                   </apex:outputLink>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
   </apex:form>
</apex:page>

1 Answer 1

1

You already have a Map<object, List<sObject>> concept in your code. So you could reuse it or build a similar Map<String, List<sObject>> where the key will be Account_Management_Type__c. This would be much easier if this field would be a lookup and not a picklist or whatever it seems to be (you could use subqueries). Anyway - you could "bucket" them per Management Type and store in some helper variable list of types.

Code below shouldn't be a part of your end solution, it's just to demnostrate an idea.

Map<String, List<sObject>> accsBucketedByType = new Map<String, List<sObject>>{
'Key (WIG)' => new List<sObject>(), // assuming you've filled them in your loop
'Active Project' => new List<sObject>(),
'Active Opportunity > $50K' => new List<sObject>(),
'Partner-Managed' => new List<sObject>(),
'TAM-Managed' => new List<sObject>(),
'TSE-Managed' => new List<sObject>(),
'Inactive' => new List<sObject>()
};

You can get all keys of such map with keySet() but you might not like the order they're in and decide to sort them just to be safe.

Set<String> keys = accsBucketedByType.keyset();
System.debug(keys);
List<String> keysAlphabetically = new List<String>();
keysAlphabetically.addAll(keys);
keysAlphabetically.sort();
System.debug(keysAlphabetically);

Finally - Visualforce supports map[key] syntax to access items in maps. So you could have an <apex:repeat> walking on keysAlphabetically variable and within it you could get to the map and list of sobjects to display in the table.


As for your second question: you already have

<apex:outputLink value="/{!account.Id}" >
    <apex:outputText value="{!account.Name}" />
</apex:outputLink>

What you're after is pretty similar, 2 possible ways are

<apex:outputLink value="/apex/somePage?id={!account.Id}" >
    <apex:outputText value="{!account.Name}" />
</apex:outputLink>

or

<apex:outputLink value="/apex/somePage" >
    <apex:param name="id" value="{!account.Id}" />
    <apex:outputText value="{!account.Name}" />
</apex:outputLink>
1
  • Thanks for this - however I am at a loss to figure out exactly what I should do to get the picklist values as column headers. Can you show me some code that I could use? Jul 3, 2013 at 21:44

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.