TheBlueMan
TheBlueMan

Reputation: 427

Trouble updating datatable with commandlink

I wanted to make a little addition to the sample datatable that they have on primefaces. I wanted to add another column that was to delete some of the data. This condition was set to if the car's color was white or not. I have the delete button rendered correctly and I have checked that the actionListener is being called and deleting the car from the list. However, the datatable is not updated even though it is reflected in the list. Whenever I sort the datatable the list gets updated. I want it to update as soon as I press delete. Here is what I have.

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">

<h:head>
</h:head>

<h:body>
<h:form>
<p:dataTable id="carTable" var="car" value="#{dtBasicView.cars}">
        <f:facet name="header">
                    Car Table
                </f:facet>

        <p:column headerText="Id" sortBy="#{car.id}">
            <h:outputText value="#{car.id}" />
        </p:column>

        <p:column headerText="Year" sortBy="#{car.year}">
            <h:outputText value="#{car.year}" />
        </p:column>

        <p:column headerText="Brand" sortBy="#{car.brand}">
            <h:outputText value="#{car.brand}" />
        </p:column>

        <p:column headerText="Color" sortBy="#{car.color}">
            <h:outputText value="#{car.color}" />
        </p:column>

        <p:column headerText="Delete Option">
            <p:commandLink actionListener="#{dtBasicView.deleteCar(car)}"
                update="carTable" rendered="#{car.isWhite}" value="Delete" ajax="true">
                <h:outputText value="Delete" />
            </p:commandLink>
        </p:column>
    </p:dataTable>
</h:form>
</h:body>

</html>

Here is the java portion.

@ManagedBean(name="dtBasicView")
@ViewScoped
public class BasicView implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Car selectedCar;
    private List<Car> cars;
    private List<Car> oneHundredCars;
    private int numberOfcars=0;

    @ManagedProperty("#{carService}")
    private CarService service;

    @PostConstruct
    public void init() {
        oneHundredCars = service.createCars(10000);
    }

    public void setNumberOfcars(String num) {
        this.numberOfcars = Integer.parseInt(num);
        cars = oneHundredCars.subList(0, numberOfcars);
    }

    public List<Car> getCars() {
        return cars;
    }

    public void setService(CarService service) {
        this.service = service;
    }

    public Car getSelectedCar() {
        return selectedCar;
    }

    public void setSelectedCar(Car selectedCar) {
        this.selectedCar = selectedCar;
    }


    public boolean getIsEmpty() {
        return cars == null || cars.isEmpty();
    }

    public void deleteCar(Car car) {
        cars.remove(car);
    }
}

Upvotes: 2

Views: 2218

Answers (2)

meyquel
meyquel

Reputation: 2214

commandLink must be inside a form, also add this tag process to commandLink

 <p:commandLink actionListener="#{dtBasicView.deleteCar(car)}"
            update="carTable" rendered="#{car.isWhite}" value="Delete" ajax="true">
  <h:outputText value="Delete" process="@this, tabla_carTable" /></p:commandLink>

Upvotes: 0

Avinash Singh
Avinash Singh

Reputation: 3777

You can try using update="@form". Seems like the older version of primefaces suffers from a bug when updating datatable from an action inside datatable.

or You can upgrade to latest Primefaces.

http://blog.primefaces.org/?p=2119

Upvotes: 1

Related Questions