I'm facing some troubles with JSF and Datatables. In my bean I have a list of objects that are showed in the view by relying on a Datatable. The object are very simple, each of them is composed by an integer (the id) and a String (the comment).
The final aim is to print the full list of objects to the user that will edit one or more rows (via an h:inputtext field) at the same time and then submit his changes via a button. And this is the problem. I already used "successfully" Datatables by editing one row per time by relying on Edit/Save button on each row, but in this case is not applicable as the final user will face with more that 500 rows in totals.
Here is the code that I used (simplified and extracted)
Bean:
@ManagedBean
@RequestedScope
public class VlanBean {
private DataModel<VlanWrapper> model;
private List<VlanWrapper> vlans = new ArrayList<VlanWrapper>();
public VlanBean() {
vlans.add(new VlanWrapper(1, ""));
vlans.add(new VlanWrapper(2, "Prova2-2"));
vlans.add(new VlanWrapper(3, ""));
vlans.add(new VlanWrapper(4, ""));
vlans.add(new VlanWrapper(5, ""));
vlans.add(new VlanWrapper(6, "Prova3"));
vlans.add(new VlanWrapper(7, ""));
vlans.add(new VlanWrapper(8, ""));
}
public void commitChanges()
{
System.out.println("Before Model ");
for (VlanWrapper vw : model)
{
System.out.println("ModelOF " + vw.getId() + " - " + vw.getProject() + " - changed: " + vw.changed + ";");
}
}
public DataModel<VlanWrapper> getModel() {
if (model == null)
model = new ListDataModel<VlanWrapper>(vlans);
return model;
}
public void setModel(DataModel<VlanWrapper> model) {
this.model = model;
}
public class VlanWrapper
{
private boolean changed;
private int id;
private String project;
public VlanWrapper(int id, String prj)
{
this.id = id;
this.project = prj;
changed = false;
}
public String getProject() {
return project;
}
public void setProject(String project) {
System.out.println("Setting PRJ..");
changed = true;
this.project = project;
}
public int getId() {
return id;
}
public boolean isChanged() {
return changed;
}
}
}
View:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
</h:head>
<h:body>
<f:view>
<h:panelGroup>
<h:form>
<h:commandButton value="Commit changes to DB" action="#{VlanBean.commitChanges}">
</h:commandButton>
</h:form>
<h:dataTable value="#{VlanBean.model}" var="vlans">
<h:column><f:facet name="header">ID</f:facet>#{vlans.id}</h:column>
<h:column><f:facet name="header">Project</f:facet>
<h:inputText value="#{vlans.project}" immediate="true"/>
</h:column>
</h:dataTable>
</h:panelGroup>
</f:view>
</h:body>
</html>
The problem is that all the changes I do on the view are not reflected in the bean and I haven't find any useful hint/post around (some where suggesting ajax forced update, other javascript, but I didn't make any of them working and no one was really close to the use I desired).
I also tried to go for ui:repeat, but I have the same behavior. Any ideas/suggestion? I'm not obliged to used Datatable, so I'm opened to other solution/approach!
Thanks in advace, Alberto