mbauer
mbauer

Reputation: 345

JSF Primefaces datatable doesn't show the right values

I have a problem with primefaces datatable. I'm getting the data from my database and it's saved in a list. That's works great! But the datatable just displays one column instead of four and this column is copied in every row.

<h:form id="userslistform">
        <p:dataTable id="userlist" var="users" value="#{userBean.usersList}"
            rowKey="#{userBean.username}">
            <p:column headerText="ID">
                <h:outputText value="#{userBean.id}" />
            </p:column>
            <p:column headerText="Username">
                <h:outputText value="#{userBean.username}" />
            </p:column>
            <p:column headerText="Firstname">
                <h:outputText value="#{userBean.firstname}" />
            </p:column>
            <p:column headerText="Surname">
                <h:outputText value="#{userBean.surname}" />
            </p:column>
        </p:dataTable>
    </h:form>

Bean:

@Named
@SessionScoped
@ManagedBean(name = "userBean")
public class UserBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private String username;
    private String password;
    private String firstname;
    private String surname;
    private Long id;
    private String email;
    private Long treeId;
    private Boolean isadmin;
    private List<User> usersList;
    private User selectedUser;

    private UserDataQuery query = new UserDataQuery();

    //getter and setters of every attribute
    ...

    @PostConstruct
    public void init() {
        usersList = query.getAllUsers();
    }

}

DAO:

public class UserDataQuery {

    private EntityManagerFactory enf;
    private EntityManager em;

    public UserDataQuery() {
        enf = Persistence.createEntityManagerFactory("xxxx");
        em = enf.createEntityManager();
        em.getTransaction().begin();
    }

    public List<User> getAllUsers() {

        List<User> usersList = em.createNamedQuery("User.findAll", User.class)
                .getResultList();

        return usersList;

    }
    ...
}

Upvotes: 0

Views: 1137

Answers (1)

Predrag Maric
Predrag Maric

Reputation: 24403

You need to use value from var inside your table, to display the data

<h:form id="userslistform">
    <p:dataTable id="userlist" var="users" value="#{userBean.usersList}"
        rowKey="#{userBean.username}">
        <p:column headerText="ID">
            <h:outputText value="#{users.id}" />
        </p:column>
        <p:column headerText="Username">
            <h:outputText value="#{users.username}" />
        </p:column>
        <p:column headerText="Firstname">
            <h:outputText value="#{users.firstname}" />
        </p:column>
        <p:column headerText="Surname">
            <h:outputText value="#{users.surname}" />
        </p:column>
    </p:dataTable>
</h:form>

var value represents a reference to current row, at the time the table is rendered. With #{userBean.<attribute>} you were always displaying values that were fixed. And, now you don't need those attributes in the bean itself, just the list (unless, of course, you need them for another purpose).

Upvotes: 3

Related Questions