0

I have a PDF document that is taking in dynamic data. I want to write some conditionals to edit how some of the data is displayed. I obviously can not do this with javascript because by the time the PDF loads the data, is it not on the client side anymore. I have tried inline visualforce conditionals but no luck.

Is there a way around this?

My goal is to have a phone list with each person sorted by department with the department name as the header. Also, if there is a department with no users, then do not display the header for that department.

<!---Controller--->
Public phoneListController(){
    allPeople = [SELECT Name,Title, Department, Extension, Phone FROM User ORDER BY Department ASC];
    system.debug('this is the list: ' + allPeople);
}

<!---Visualforce PDF--->
    <apex:repeat value="{!allPeople}" var="person">
        <th colspan="5">{!person.Department}</th>
        <tr>
            <td>{!person.Name}</td>
            <td>{!person.Title}</td>
            <td>{!person.Extension}</td>
            <td>{!person.Phone}</td>
        </tr>
    </apex:repeat>

What I am getting caught up on is how to display the department name so it is just listed once on top of the group of users WITHOUT having to put a filter on the query. Right now with this code, the department is listed on top of every user listed.

What I want it to look like:

What I want it to look like

What it does look like:

what it does look like

0

1 Answer 1

2

Its all about the data...

Use a Map:

Class

Public phoneListController(){}

public Map<String,User[]> allPeople{
    get{
         Map<String,User[]> tmp = new Map<String,User[]>();
         for(User u : [SELECT Name,Title, Department, Extension, Phone FROM User ORDER BY Department ASC]){
              //A while ago we had a discussion on performance and this was found to be better of a if..then.. block with contains key
              User[] uList = tmp.get(u.Department);
              if(uList == null) uList = New User[]{};
              uList.add(u);
              //Add mack to the map
              tmp.put(u.Department,uList);
         }
         return tmp;
    }
    set;
}

VF

<apex:repeat value="{!allPeople}" var="depts">
     <th colspan="5">{!depts}</th>
<apex:repeat value="{!allPeople[depts]}" var="person">

    <tr>
        <td>{!person.Name}</td>
        <td>{!person.Title}</td>
        <td>{!person.Extension}</td>
        <td>{!person.Phone}</td>
    </tr>
</apex:repeat>
</apex:repeat>

Basically get a Map of Departments to Users and structure the repeats to user the Keys and then the values

4
  • thank you @eric you are totally right. I didn't think to use a Map. This works great.
    – Olivia
    Commented Dec 20, 2016 at 22:55
  • @Olivia - Schweeet....Glad it worked for you. Been a long day and thought for sure it would be full of typos....
    – Eric
    Commented Dec 20, 2016 at 22:56
  • I have a follow up question for you- if I wanted to filter the Sales Department by Managers and add the manager name into the Department key, would a map inside of a map be the best avenue to go down?
    – Olivia
    Commented Dec 21, 2016 at 17:09
  • @Olivia - At that point I would consider using a wrapper class
    – Eric
    Commented Dec 21, 2016 at 17:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .