Thứ Tư, 16 tháng 5, 2012

Datatable with row editing and deleting in Primefaces 3

JSF Page

<?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:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:p="http://primefaces.org/ui">

    <h:head>
        <title>Summary Report Page</title>
    </h:head>
    <h:body>
        <h:form id="form">
            <p:growl id="messages" showDetail="true"/> 
            <p:dataTable id="basic" var="car" value="#{tableBean.carsSmall}" editable="true">
                <p:ajax event="rowEdit" listener="#{tableBean.updateItem(car)}"  />
                <p:column headerText="Model" style="width:125px">
                    <p:cellEditor>
                        <f:facet name="output">
                            <h:outputText value="#{car.model}" />
                        </f:facet>
                        <f:facet name="input">
                            <p:inputText value="#{car.model}" style="width:100%"/>
                        </f:facet>
                    </p:cellEditor>
                </p:column>

                <p:column headerText="Year" style="width:125px">
                    <p:cellEditor>
                        <f:facet name="output">
                            <h:outputText value="#{car.year}" />
                        </f:facet>
                        <f:facet name="input">
                            <p:inputText value="#{car.year}" style="width:100%" label="Year"/>
                        </f:facet>
                    </p:cellEditor>
                </p:column>

                <p:column>
                    <f:facet name="header">
                        Manufacturer
                    </f:facet>
                    <h:outputText value="#{car.manufacturer}" />
                </p:column>

                <p:column>
                    <f:facet name="header">
                        Color
                    </f:facet>
                    <h:outputText value="#{car.color}" />
                </p:column>
                <p:column>
                    <f:facet name="header">
                        Delete
                    </f:facet>
                    <p:commandButton icon="ui-icon-close" title="remove from cart"
                                     actionListener="#{tableBean.removeItem(car)}" update="@form"/>
                </p:column>
                <p:column>
                    <f:facet name="header">
                        Edit
                    </f:facet>
                    <p:rowEditor/>
                </p:column>
            </p:dataTable>
        </h:form>
    </h:body>
</html>
Managed Bean:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean(name = "tableBean")
@ViewScoped
public class TableBean implements Serializable {
   
    private final static String[] colors;
    private final static String[] manufacturers;
    private List<Car> carsSmall;
   
    static {
        colors = new String[10];
        colors[0] = "Black";
        colors[1] = "White";
        colors[2] = "Green";
        colors[3] = "Red";
        colors[4] = "Blue";
        colors[5] = "Orange";
        colors[6] = "Silver";
        colors[7] = "Yellow";
        colors[8] = "Brown";
        colors[9] = "Maroon";
       
        manufacturers = new String[10];
        manufacturers[0] = "Mercedes";
        manufacturers[1] = "BMW";
        manufacturers[2] = "Volvo";
        manufacturers[3] = "Audi";
        manufacturers[4] = "Renault";
        manufacturers[5] = "Opel";
        manufacturers[6] = "Volkswagen";
        manufacturers[7] = "Chrysler";
        manufacturers[8] = "Ferrari";
        manufacturers[9] = "Ford";
    }
   
    public TableBean() {
        carsSmall = new ArrayList<Car>();
        populateRandomCars(carsSmall, 9);
    }
   
    private void populateRandomCars(List<Car> list, int size) {
        for (int i = 0; i < size; i++) {
            list.add(new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor()));
        }
    }
   
    public void removeItem(Car car) {
        carsSmall.remove(car);
    }
   
    public void updateItem(Car car) {
        System.out.println("New value: " + car.getModel());
    }
   
    public List<Car> getCarsSmall() {
        return carsSmall;
    }
   
    private int getRandomYear() {
        return (int) (Math.random() * 50 + 1960);
    }
   
    private String getRandomColor() {
        return colors[(int) (Math.random() * 10)];
    }
   
    private String getRandomManufacturer() {
        return manufacturers[(int) (Math.random() * 10)];
    }
   
    private String getRandomModel() {
        return UUID.randomUUID().toString().substring(0, 8);
    }
}

web.xml

<context-param>
        <param-name>com.sun.faces.expressionFactory</param-name>
        <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
    </context-param>

Note: using E.L 2.2 (el-api-2.2.jar ; el-impl-2.2.jar) in Tomcat 6 lib folder


Không có nhận xét nào:

Đăng nhận xét