Primefaces: ajax navigation and dataTable checkbox selection

user2219927 Source

I am experiencing checkbox selection problem in my application. I have a dataTable on one page(index.xhtml). On same page there is a ajax button and when user click on it application should navigate to another page(detail.xhtml). Detail page contains return button for navigating back to index.xhtml. Navigation works but when user return from detail page, row checkboxes in dataTable are not checked when user click on it(header checkbox selecting all rows works). When i repeat scenario(aka visiting detail page and return back), checkboxes are working again. After third repeat, they are not working again(so every second navigation they do not work). When I use ajax="false" or faces-redirect=true on buttons, everything works.

Using Mojarra 2.10.19, PF 3.5 and Glassfish 3.2.1

For simplicity i re-create the problem by simple example:

index.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"      
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui" >

<h:head></h:head>   
<h:body>
    <h:form>
        <p:commandButton value="Add" action="add" />
        <p:dataTable id="cars" var="car" value="#{tableBean.mediumCarsModel}" 
                     selection="#{tableBean.selectedItems}" >

            <p:column selectionMode="multiple" style="width: 2%" />

            <p:column headerText="Model">
                #{car.model}
            </p:column>

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

detail.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui" >

<h:head></h:head>

<h:body>
     <h:form>
      <p:commandButton value="Return" action="return" /> 
     </h:form>    
</h:body>

TableBean.java

@ManagedBean
@SessionScoped
public class TableBean {    
    private final static String[] manufacturers;

    static { 
      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";
    }

    private List<Car> carsSmall;    
    private CarDataModel mediumCarsModel;
    private List<Car> selectedItems; 

    public TableBean() {
        carsSmall = new ArrayList<Car>();
        populateRandomCars(carsSmall, 5);
        mediumCarsModel = new CarDataModel(carsSmall);
    }

    private void populateRandomCars(List<Car> list, int size) {
        for (int i = 0; i < size; i++) {
            list.add(new Car(manufacturers[i]));
        }
    }

    public List<Car> getSelectedItems() {
        return selectedItems;
    }

    public void setSelectedItems(List<Car> selectedItems) {        
        this.selectedItems = selectedItems;
    }

    public CarDataModel getMediumCarsModel() {        
        return mediumCarsModel;
    }
}

CarDataModel.java

public class CarDataModel extends ListDataModel<Car> implements SelectableDataModel<Car> {  

    public CarDataModel(List<Car> data) {
        super(data);
    }

    @Override
    public Car getRowData(String rowKey) { 
        List<Car> cars = (List<Car>) getWrappedData();

        for(Car car : cars) {
            if(car.getModel().equals(rowKey)){                
                return car;
            }
        }
        return null;
    }

    @Override
    public Object getRowKey(Car car) {
        return car.getModel();
    }
}

Car.java

public class Car implements Serializable {

    private String model;

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public Car(String model) {
        this.model = model;
    }
}

faces-config.xml

<navigation-rule>
    <from-view-id>/index.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>add</from-outcome>
        <to-view-id>/detail.xhtml</to-view-id>
    </navigation-case>       
</navigation-rule>   

<navigation-rule>
    <from-view-id>/detail.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>return</from-outcome>
        <to-view-id>/index.xhtml</to-view-id>
    </navigation-case>       
</navigation-rule> 
javaajaxjsfprimefaces

Answers

answered 5 years ago Thiha Maung #1

You many need to remove navigation-rule from faces-config.xml to try the following...

index.xhtml

<p:commandButton value="Add" action="#{tableBean.redirectToDetail}" />

detail.xhtml

<p:commandButton value="Return" action="#{tableBean.redirectToIndex}" />

TableBean.java

@ManagedBean(name = "tableBean")
...
...
...
public String redirectToDetail() {
    return "detail?faces-redirect=true";
}
public String redirectToIndex() {
    return "index?faces-redirect=true";
}

comments powered by Disqus