Bart1990
Bart1990

Reputation: 255

Create a dynamic table from a java.util.Map

I'm developing a web application in JSF 2.0. In my application I need a table for viewing presences.

I have a Map<Date, List<Presence>> in my 'PresenceService' bean.

The Presence object has a Person object (with name and surname) and a boolean present/not present.

I'd like to create a table that looks like this:

                 monday     tuesday        wednesday    ...
Name Person 1    present    not present    present      ...
Name Person 2    present    present        present      ...
...              ...        ...            ...          ...

Which JSF components could I use to create such a table? Or should I not be using a Map for this? I tried creating a table myself with <ui:repeat>'s but failed to do so. As wel the colums as the rows can be dynamic since the user can choose a period and a range of persons.

EDIT: solution (see answer Balus C)

This works like a charm:

        <table>
            <tr>
                <th>Name</th>
            <ui:repeat value="#{aanwezigheidController.siAanwezigheidService.datums}" var="datumHeader">
                <th>
                <h:outputText value="#{datumHeader}">
                    <f:convertDateTime pattern="dd/MM/yyyy" />
                </h:outputText>
                </th>
            </ui:repeat>
            </tr>
            <ui:repeat value="#{aanwezigheidController.siAanwezigheidService.aanwezigheidSiRijen}" var="aanwRij">
                <tr>
                    <td>#{aanwRij.persoon.naam} #{aanwRij.persoon.voornaam}</td>

                <ui:repeat value="#{aanwezigheidController.siAanwezigheidService.datums}" var="datumCell">
                    <td>
                    <h:outputText value="aanwezig" rendered="#{aanwRij.aanwezighedenMap[datumCell].aanwezig}" />
                    <h:outputText value="afwezig" rendered="#{!aanwRij.aanwezighedenMap[datumCell].aanwezig}" />
                    </td>
                </ui:repeat>

                </tr>
            </ui:repeat>
        </table>

Upvotes: 2

Views: 672

Answers (1)

BalusC
BalusC

Reputation: 1108742

A double nested <ui:repeat> should be fine. The standard JSF implementation does not offer something like <h:columns> to represent dynamic columns, but component libraries RichFaces and PrimeFaces have respectively <rich:columns> and <p:columns> for exactly this purpose. You may want to take a look at it.

With alone a Map<Date, List<Presence>>, it is not possible to pick a specific person to present the row data for all columns. You basically need a Map<Date, Map<Person, List<Presence>>, or a separate List<Date> representing the dynamic columns and a separate List<Presence> where the Presence in turn has Person and Map<Date, Boolean> properties so that a specific date can be picked for the columns.

Upvotes: 3

Related Questions