I ran into a problem with a datatable update. Simply I don't know how should I update the table after rowEdit event is finished to keep the order. I read many topics on the net, but I haven't found the solution yet.
In theory this should work: update=":form:fruits".
But it doesn't re-sort the entries of table. So I tried to update the whole form: update=":form".
In this case all entries are gone except the edited one with no design.
Primefaces version: 5.1.
A very simple code to reproduce the problem.
Fruit.java:
Code: Select all
public class Fruit {
private Integer id;
private String name;
public Fruit(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Fruit)) {
return false;
}
return id == ((Fruit) o).getId();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Code: Select all
@ManagedBean(name = "fruits")
@ViewScoped
public class Fruits implements Serializable {
private static final long serialVersionUID = 1L;
private ArrayList<Fruit> list = new ArrayList<Fruit>();
{
list.add(new Fruit(1, "pineapple"));
list.add(new Fruit(2, "orange"));
list.add(new Fruit(3, "banana"));
list.add(new Fruit(4, "lemon"));
list.add(new Fruit(5, "apple"));
}
public void onRowEdit(RowEditEvent event) {
Fruit fruit = (Fruit) event.getObject();
list.set(list.indexOf(fruit), fruit);
}
public ArrayList<Fruit> getList() {
return list;
}
public void setList(ArrayList<Fruit> list) {
this.list = list;
}
}
Code: Select all
<h:head />
<h:body>
<h:form id="form">
<p:dataTable id="fruits" value="#{fruits.list}" var="fruit"
editable="true" sortBy="#{fruit.name}">
<p:ajax event="rowEdit" listener="#{fruits.onRowEdit}" update=":form" />
<p:column sortBy="#{fruit.name}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{fruit.name}"></h:outputText>
</f:facet>
<f:facet name="input">
<p:inputText value="#{fruit.name}"></p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
<p:column>
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>