I am currently trying to do a simple list of inputs with adding/removing primefaces p:commandButton
.
I am using PrimeFaces 6.2 on Glassfish 4.1.1 with Mojarra 2.2.12.
ExampleBean.java
package /* myPackage */;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javafx.util.Pair;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@Named(value = "exampleBean")
@SessionScoped
public class ExampleBean implements Serializable {
private List<Pair<Integer, Integer>> list;
@PostConstruct
public void init() {
list = new ArrayList<>();
addNewItem();
}
public void addNewItem() {
list.add(new Pair<>(1, 300));
}
public List<Pair<Integer, Integer>> getList() {
return list;
}
public void setList(List<Pair<Integer, Integer>> list) {
this.list = list;
}
}
example.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:jsf="http://xmlns.jcp.org/jsf">
<h:head>
<title>Example</title>
<meta charset="utf-8" />
</h:head>
<h:body>
<h:form id="example-form">
<div jsf:id="example-container">
<ui:repeat value="#{exampleBean.list}" var="item" varStatus="status">
<div>
<p:inputText value="#{item.key}" />
<p:inputText value="#{item.value}" />
<p:commandButton
value="Delete"
actionListener="#{exampleBean.list.remove(item)}"
process="@this"
update="example-form:example-container"
rendered="#{!status.first}" />
<p:commandButton
value="Add"
actionListener="#{exampleBean.addNewItem()}"
process="@this"
update="example-form:example-container"
rendered="#{status.first}" />
</div>
</ui:repeat>
</div>
</h:form>
</h:body>
</html>
With the rendered
attribute of each of my p:commandButton
, I want to display the add button only on the first item, and the delete button on all items except the first one (making it undeletable).
My problem here, is that using rendered="#{status.first}"
on the adding button make the whole thing not working.
<p:commandButton ... rendered="#{status.last}" /> <!-- actionListener called -->
<p:commandButton ... rendered="#{true}" /> <!-- actionListener called -->
<p:commandButton ... rendered="#{status.first}" /> <!-- actionListener NOT called -->
With rendered="#{status.first}"
, a button click do not call actionListener
but trigger the update
.
I do not have any idea what could change between displaying it in the first item rather than in others or last one.