dataTable, filter, sort, new record don't work

UI Components for JSF
Post Reply
superhero
Posts: 5
Joined: 12 Mar 2015, 16:26

17 Aug 2016, 09:30

Hello,

i use the dataTable for creating a table. If i used filter, sortBy, new record separatly it works great. But when i use the filter function and type something in, then delete the typed text and klick on the new Record Button it doesn't works.

The Cource Code is mostly from Primefaces Showcase. Do somebody knew the solution for this problem.


index.xhtml

Code: Select all

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>DataTable</title>
    </h:head>
    <h:body>

        <h:form id="form">

            <p:growl id="msgs" showDetail="true"/>       

            <p:dataTable 
                var="car" 
                value="#{dtEditView.carList}" 
                rows="50"
                widgetVar="carTable"
                emptyMessage="No cars found."
                paginator="true" 
                paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                rowsPerPageTemplate="10,50,100"
                currentPageReportTemplate="(Car {startRecord} - {endRecord} of {totalRecords}, Page: {currentPage}/{totalPages})"                
                filteredValue="#{dtEditView.filteredCarList}"
                selection="#{dtEditView.selectedCarList}"                
                id="carDataTable"
                rowKey="#{car.id}"
                editable="true" 
                style="margin-bottom:20px"
                stickyHeader="true">

                <f:facet name="header">                   

                    <p:commandButton value="Add Car" update="carDataTable" icon="ui-icon-document" action="#{dtEditView.addNewCar()}" />

                    <p:commandButton value="Delete Car" update="carDataTable" icon="ui-icon-trash" action="#{dtEditView.deleteCars()}" >
                        <p:confirm header="Warnung" message="All selected Cars will be deleted?" icon="ui-icon-alert" />
                    </p:commandButton>

                    <p:confirmDialog global="true">
                        <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
                        <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
                    </p:confirmDialog>
                </f:facet>

                <p:ajax event="rowEdit" listener="#{dtEditView.onRowEdit}" update=":form:msgs" />
                <p:ajax event="rowEditCancel" listener="#{dtEditView.onRowCancel}" update=":form:msgs" />

                <p:column selectionMode="multiple" style="width:16px;text-align:center"/>

                <p:column headerText="Id" sortBy="#{car.id}" filterBy="#{car.id}" filterMatchMode="contains">
                    <p:cellEditor>
                        <f:facet name="output">
                            <h:outputText value="#{car.id}" />
                        </f:facet>
                        <f:facet name="input">
                            <p:inputText id="modelInput" value="#{car.id}" style="width:100%"/>
                        </f:facet>
                    </p:cellEditor>
                </p:column>

                <p:column headerText="Year" sortBy="#{car.year}" filterBy="#{car.year}" filterMatchMode="contains">
                    <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 headerText="Brand" sortBy="#{car.brand}" filterBy="#{car.brand}" filterMatchMode="contains">
                    <p:cellEditor>
                        <f:facet name="output">
                            <h:outputText value="#{car.brand}" />
                        </f:facet>
                        <f:facet name="input">
                            <h:selectOneMenu value="#{car.brand}" style="width:100%">
                                <f:selectItems value="#{dtEditView.brands}" var="man" itemLabel="#{man}" itemValue="#{man}" />
                            </h:selectOneMenu>
                        </f:facet>
                    </p:cellEditor>
                </p:column>

                <p:column headerText="Color" sortBy="#{car.color}" filterBy="#{car.color}" filterMatchMode="contains">
                    <p:cellEditor>
                        <f:facet name="output">
                            <h:outputText value="#{car.color}" />
                        </f:facet>
                        <f:facet name="input">
                            <h:selectOneMenu value="#{car.color}" style="width:100%">
                                <f:selectItems value="#{dtEditView.colors}" var="color" itemLabel="#{color}" itemValue="#{color}" />
                            </h:selectOneMenu>
                        </f:facet>
                    </p:cellEditor>
                </p:column>

                <p:column style="width:32px">
                    <p:rowEditor />
                </p:column>

            </p:dataTable>

        </h:form>

    </h:body>
</html>
EditView.java

Code: Select all

package car;

import java.io.Serializable;
import java.util.List;
import java.util.ListIterator;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.RowEditEvent;

@SessionScoped
@ManagedBean(name = "dtEditView")
public class EditView implements Serializable {

    private List<Car> carList;
    private List<Car> selectedCarList;
    private List<Car> filteredCarList;

    private final CarService service = new CarService();

    @PostConstruct
    public void init() {
        carList = service.createCars(20);
    }

    public List<Car> getCarList() {
        return carList;
    }

    public List<Car> getSelectedCarList() {
        return selectedCarList;
    }

    public void setSelectedCarList(List<Car> selectedCarList) {
        this.selectedCarList = selectedCarList;
    }

    public List<Car> getFilteredCarList() {
        return filteredCarList;
    }

    public void setFilteredCarList(List<Car> filteredCarList) {
        this.filteredCarList = filteredCarList;
    }

    public List<String> getBrands() {
        return service.getBrands();
    }

    public List<String> getColors() {
        return service.getColors();
    }

    public void addNewCar() {
        carList.add(0, service.getNewCar());
    }

    public void deleteCars() {
        String ausgabe = "";

        try {
            ausgabe = selectedCarList.size() + "Records deleted.";
            Car car = new Car();

            ListIterator<Car> listIterator = selectedCarList.listIterator();

            while (listIterator.hasNext()) {
                car = listIterator.next();
                carList.remove(car);
            }
        } catch (Exception e) {
            System.out.println(e);
        }

        FacesMessage msg = new FacesMessage("Delete", ausgabe);
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void onRowEdit(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Car Edited", ((Car) event.getObject()).getId());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void onRowCancel(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Edit Cancelled", ((Car) event.getObject()).getId());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

}
CarService.java

Code: Select all

package car;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import java.util.Arrays;

@ApplicationScoped
@ManagedBean(name = "carService")
public class CarService {

    private final static String[] colors;

    private final static String[] brands;

    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";

        brands = new String[10];
        brands[0] = "BMW";
        brands[1] = "Mercedes";
        brands[2] = "Volvo";
        brands[3] = "Audi";
        brands[4] = "Renault";
        brands[5] = "Fiat";
        brands[6] = "Volkswagen";
        brands[7] = "Honda";
        brands[8] = "Jaguar";
        brands[9] = "Ford";
    }

    public Car getNewCar() {
        Car newCar = null;
        try {
            newCar = new Car(getRandomId(), getRandomBrand(), getRandomYear(), getRandomColor(), getRandomPrice(), getRandomSoldState());
        } catch (Exception e) {
            System.out.println(e);
        }
        return newCar;
    }

    public List<Car> createCars(int size) {
        List<Car> list = new ArrayList<Car>();
        for (int i = 0; i < size; i++) {
            list.add(new Car(getRandomId(), getRandomBrand(), getRandomYear(), getRandomColor(), getRandomPrice(), getRandomSoldState()));
        }

        return list;
    }

    private String getRandomId() {
        return UUID.randomUUID().toString().substring(0, 8);
    }

    private int getRandomYear() {
        return (int) (Math.random() * 50 + 1960);
    }

    private String getRandomColor() {
        return colors[(int) (Math.random() * 10)];
    }

    private String getRandomBrand() {
        return brands[(int) (Math.random() * 10)];
    }

    public int getRandomPrice() {
        return (int) (Math.random() * 100000);
    }

    public boolean getRandomSoldState() {
        return (Math.random() > 0.5) ? true : false;
    }

    public List<String> getColors() {
        return Arrays.asList(colors);
    }

    public List<String> getBrands() {
        return Arrays.asList(brands);
    }
}
Car.java

Code: Select all

package car;

import java.io.Serializable;

public class Car implements Serializable {
    
    public String id;
    public String brand;
    public int year;
    public String color;
    public int price;
    public boolean sold;

    public Car() {}
    
    public Car(String id, String brand, int year, String color) {
        this.id = id;
        this.brand = brand;
        this.year = year;
        this.color = color;
    }
    
    public Car(String id, String brand, int year, String color, int price, boolean sold) {
        this.id = id;
        this.brand = brand;
        this.year = year;
        this.color = color;
        this.price = price;
        this.sold = sold;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }

    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }

    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }

    public boolean isSold() {
        return sold;
    }
    public void setSold(boolean sold) {
        this.sold = sold;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Car other = (Car) obj;
        if ((this.id == null) ? (other.id != null) : !this.id.equals(other.id)) {
            return false;
        }
        return true;
    }
}

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 53 guests