Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent nested data in a Primefaces datatable?

Here is my model :

User.java

public class User {
   //...

   public List<User> getFriends() {
      // ...
   }
}

I would like to build a table of user friends like this :

users.jsf

+----------+------------+
|   USER   |   FRIENDS  |
+----------+------------+
|          |    ALICE   |
|          +------------+        
|   ADAM   |    BOB     |
|          +------------+
|          |    PITT    |
+----------+------------+
|          |            |
....

Since there are many users, it's not possible to dump the user table in one go.

The datatable component is ideal in this case because it has built-in pagination support. It is ideal too because it's possible to sort columns...

Unfortunately, I wasn't able to find through the Primefaces examples a way for changing the rowspan in user columns.

How can I build this datatable ?

Some other OP having this similar issue:

  • Primefaces/JSF Nested Data Table
  • How to do a dataTable containing a subtable with Primefaces in the same row? What's doing with mine?
  • Primefaces dataTable with rowspan
  • ... Add yours?

EDIT
Here is the final solution I came up with.

like image 398
Stephan Avatar asked Nov 30 '12 16:11

Stephan


3 Answers

Just use another data table inside your column :)

<h:column>
    <h:dataTable var="friend" value="#{user.friends}">
        <h:column>
            <h:outputText value="#{friend.name}"/>
        </h:column>
    </h:dataTable>
</h:column>

This is how it looks on my localhost

enter image description here

like image 149
Kerem Baydoğan Avatar answered Oct 19 '22 22:10

Kerem Baydoğan


Another option is to use ui:repeat inside a column to get all the values of a collection.

Example:

<p:dataTable var="user" value="#{userGroupBacking.users}" id="userTable">

    <p:column headerText="User">
        <h:outputText value="#{user.name}" />
    </p:column>

    <p:column headerText="Groups">
        <ui:repeat var="group" value="#{user.groups}">
            <h:outputText value="#{group.name}" /><br />
        </ui:repeat>
...
like image 43
Catfish Avatar answered Oct 19 '22 23:10

Catfish


Primefaces expandable rows should address your need, only you'll need to get creative with the child row component. You could use prime faces data list component as the child row component. It'll look something like:

   <p:row expansion>
    <p:datalist value ="#{yourTableRowVar.friendslist} Var="friend">
    #{friend.firstName}
    </p:datalist>
   </p:row expansion>
like image 43
kolossus Avatar answered Oct 19 '22 22:10

kolossus