Forum
How to: Single list of employees, sorted by department as categories, with multi...
Hi there,
Goal
I’m working on a team-page for a company where about 40 employees are sorted into departments. Their department affiliation is changing a lot due to restructuring. So I decided to make a category set of „departments“, where the client can change/add/create new categories if they need, and choose from the categories for each employee. The employee list is a multiple-item region.
All of this is placed on a single team-page. What I want is a list of departments, and the employees in that department. I got it working so that the heading of the department is only shown if it is different from the last one. Re-ordering employees makes the list work almost completely.
Problem
Some employees are in multiple departments, and this is where my current solution stops working: When I add two or more categories to an employee, using the following template, the heading (h3) is repeated, and the employee is added after the last of his/her categories:
team.php:
<?php perch_content('Büroteam'); ?>
templates/content/buero-team.html:
<perch:before>
<div class="container container--team">
</perch:before>
<perch:if different="team-abteilungen">
<perch:categories id="team-abteilungen" label="Abteilung" set="team-abteilungen" required="true" /><h3 class="abteilung" id="<perch:category id="catSlug" label="Kurzname" />"><perch:category id="catTitle" /></h3></perch:categories>
</perch:if>
<div class="person">
<img src="<perch:content type="image" id="foto" label="Foto" required="true" />" />
<div class="contact-info">
<h3><perch:content type="text" id="vorname" label="Vorname" required="true" /> <perch:content type="text" id="nachname" label="Nachname" required="true" title="true" /></h3>
<perch:if exists="position">
<p><perch:content type="text" id="position" label="Position/Aufgaben" /></p>
</perch:if>
</div> <!-- .contact-info -->
</div> <!-- .person -->
<perch:after>
</div>
</perch:after>
Question
How can I make a list of all employees, where I don’t have to add the same person multiple times with a different category selected, but can select multiples from my categories so that the employee gets repeated automatically for each category he/she is in? So this:
Jane Doe: Category 1 and 3
John Doe: Category 1 and 2
Mark Doe: Category 3
should become this:
Category 1
Jane Doe
John Doe
Category 2
John Doe
Category 3
Mark Doe
Jane Doe
I’m already using perch_categories() to display a list of all categories on top of the page (for jumping to a department quickly), but I couldn’t get the categories list to work with additional parameters for sorting etc. They just didn’t display.
Best regards, Jannis
I think you'll probably need to loop through the categories and fetch the linked employees for each as you go.
Thanks Drew!
Can you tell me how I can do that?
I found this forum entry: https://forum.grabaperch.com/forum/04-16-2015-display-all-categories-with-listdetail-items
But the solution is not completely revealed there. You wrote …
… what do I need to do in both steps? I tried adding this to my team.php, but the list with categories and items disappears completely when I do that:
Ah, I forgot to actually store the array. The items are repeating now:
team.php:
templates/content/team-buero.html:
The only thing wrong is that category names are repeated, so every item is preceded by all it’s categories. Of course „different“ doesn’t work here. This:
becomes this:
While this is still an issue (I can’t get it to work, see above), this is new:
I just noticed that there is no way to let my clients add and edit categories at all. Even when I give their user role all privileges, the Categories tab doesn’t show up in Perch when I log in as that user.
Is this desired behavior, a bug, or me?
Your
team-buero.html
template outputs the categories. I think you can usesuppress="true"
prevent them being output, or use a shortened template for this particular view.How many apps do you have installed?
Thanks Drew. I went with the second solution (it didn’t occur to me to use a separate template). I have no apps installed, just a German translation.
I managed to get what I want: A list of all employees categories, with the employees linked to that category showing below, duplicating if necessary.
team.php:
templates/categories/departments.html (this shows a clickable list at the top, which jumps to the clicked department):
templates/categories/departments-separate.html (these are the category headings, I exclude the company board because this it is shown using a different template):
templates/content/team.html (this shows the employees):
Just wanted to say thanks to Jannis for putting up the code and walking through it as it helped me a lot and saved me several hours of frustration.
The perch documentation is largely very good but this would be a very welcome inclusion in the official docs as I note that several people have asked this question now and I find the categories documentation quite confusing for a newbie.
Oh wow, thanks for the nice feedback, Mike!
Just as a follow-up, this is the end result: https://ried-blitzschutz.de/ueber-uns/das-team.php
The client is happy because they move people in between departments quite often and they don’t want to wait until I can make the change on the site. I’m happy because I don’t have to deal with duplicating 10 lines of markup every couple of weeks and dealing with client revisions. The client already wants to expand the website with Perch to add project showcases that they can manage. And I can do that without having to worry about a whole templating system or a theme, because this website is still mostly static HTML and I can just add Perch where it is needed.
So thank you, Rachel and Drew, for giving us Perch! I’m the happiest customer right now! :)
That's great to hear - thanks!