[Help] Add row to top of datatable

UI Components for JSF
Post Reply
itssaurav2004
Posts: 2
Joined: 15 Nov 2010, 21:47

15 Nov 2010, 22:03

I created a simple layout to add a row to a database. Everything is working fine. But I want the newly added row to appear on the top instead at last.

How can it be acheived ?
Is it possible to sort the data by a hidden field (like primary key) before rendering the table ?


Other than that is there any way to a have delete button like edit button when cell editor is used?


Here's my codes

Code: Select all

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
        xmlns:p="http://primefaces.prime.com.tr/ui"
        xmlns:f="http://java.sun.com/jsf/core">

<h:head>
    <title>List Users</title>
    <link type="text/css" rel="stylesheet" href="resources/themes/cupertino/skin.css" />

</h:head>

<h:body>


    <h:form>

        <p:panel header="Creat New Record">
            <h:panelGrid columns="2">
                <h:outputLabel value="Login :" for="txt_login"></h:outputLabel>
                <h:inputText id="txt_login" value="#{loginUserManagedBean.loginUser.login}" required="true"/>

                <h:outputLabel value="Email :" for="txt_email"></h:outputLabel>
                <h:inputText id="txt_email" value="#{loginUserManagedBean.loginUser.email}" required="true"/>

                <h:outputLabel value="Password :" for="txt_password"></h:outputLabel>
                <h:inputText id="txt_password" value="#{loginUserManagedBean.loginUser.password}" required="true"/>

                <h:outputLabel value="Salt :" for="txt_salt"></h:outputLabel>
                <h:inputText id="txt_salt" value="#{loginUserManagedBean.loginUser.salt}" required="true"/>


                <p:commandButton value="Create" action="#{loginUserManagedBean.createLoginUser}" update="users" />

                <p:ajaxStatus style="width:16px;height:16px;">
                    <f:facet name="start">
                        <h:graphicImage value="../design/ajaxloading.gif" />
                    </f:facet>
                    <f:facet name="complete">
                        <h:outputText value="" />
                    </f:facet>
                </p:ajaxStatus>
                
            </h:panelGrid>
        </p:panel>


        <p:dataTable id="users" var="user" value="#{loginUserManagedBean.loginUsers}" rowEditListener="#{loginUserManagedBean.updateLoginUser}" widgetVar="loginuser"
                 paginator="true" rows="10"
                 paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                 rowsPerPageTemplate="5,10,15" >
            <f:facet name="header">
                In-Cell Editing Yahoo!!!
            </f:facet>

            <p:column headerText="Login" style="width:200px">
                        <h:outputText value="#{user.login}" />
            </p:column>

            <p:column headerText="Email" style="width:200px">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{user.email}" />
                    </f:facet>
                    <f:facet name="input">
                        <h:inputText value="#{user.email}" />
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column headerText="Password" style="width:200px">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{user.password}" />
                    </f:facet>
                    <f:facet name="input">
                        <h:inputText value="#{user.password}" />
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column headerText="Salt" style="width:12em">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{user.salt}" />
                    </f:facet>
                    <f:facet name="input">
                        <h:inputText value="#{user.salt}" />
                    </f:facet>
                </p:cellEditor>
            </p:column>
            

            <p:column headerText="Options">
                <p:rowEditor />
            </p:column>

        </p:dataTable>
    </h:form>

</h:body>
</html>

Code: Select all

package managed;

import entity.LoginUser;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import org.primefaces.event.RowEditEvent;
import session.LoginUserFacade;


/**
 *
 * @author kumarsaurav
 */
@ManagedBean
@SessionScoped
public class LoginUserManagedBean implements Serializable {

    @EJB
    LoginUserFacade loginUserFacade;

    private LoginUser loginUser;

    private List<LoginUser> loginUsers;
    

    public LoginUserManagedBean() {
        loginUser = new LoginUser();
    }

    @PostConstruct
    private void populateLoginUsers() {
        loginUsers = loginUserFacade.findAll();
    }

    public void createLoginUser() {
        loginUserFacade.create(loginUser);
        loginUser = new LoginUser();
        populateLoginUsers();
    }


    public void updateLoginUser(RowEditEvent e) {
        loginUserFacade.edit((LoginUser)e.getObject());
    }

    public LoginUser getLoginUser() {
        return loginUser;
    }

    public void setLoginUser(LoginUser loginUser) {
        this.loginUser = loginUser;
    }

    public List<LoginUser> getLoginUsers() {
        return loginUsers;
    }

    public void setLoginUsers(List<LoginUser> loginUsers) {
        this.loginUsers = loginUsers;
    }
}

KingdomHeart
Posts: 280
Joined: 25 Jun 2010, 01:18

16 Nov 2010, 06:09

Dont think it is too difficult. Just sort the data base on the time you create that object like TimeStamp. If pull data out of database, you can do the sort on the select statement.

Mojarra 2.1.10, Glassfish 3.1, Tomcat 7, Primeface 3.3.1
IE 6-8, FireFox 10-13
MAC OSX Lion, Windows XP, 7

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 25 guests