How to update row in JSF dataTable
This example is enhancing the previous JSF 2 dataTable example, by adding a “update” function to update row in dataTable.
Update Concept
The overall concept is quite simple :
1. Add a “ediatble” property to keep track the row edit status.
//... public class Order{ String orderNo; String productName; BigDecimal price; int qty; boolean editable; public boolean isEditable() { return editable; } public void setEditable(boolean editable) { this.editable = editable; }
2. Assign a “Edit” link to the end of each row, if clicked, set “ediatble” = true. In JSF 2.0, you can provide the parameter values in the method expression directly, see the edit action below :
//... <h:dataTable value="#{order.orderList}" var="o"> <h:column> <f:facet name="header">Action</f:facet> <h:commandLink value="Edit" action="#{order.editAction(o)}" rendered="#{not o.editable}" /> </h:column>
//... public String editAction(Order order) { order.setEditable(true); return null; }
3. In JSF page, if “ediatble” = true, display the input text box for edit; Otherwise just display the normal output text. A simple trick to simulate the update effect :)
//... <h:dataTable value="#{order.orderList}" var="o"> <h:column> <f:facet name="header">Order No</f:facet> <h:inputText value="#{o.orderNo}" size="10" rendered="#{o.editable}" /> <h:outputText value="#{o.orderNo}" rendered="#{not o.editable}" /> </h:column>
4. Finally, provide a button to save your changes. When you make changes in the input text box and save it, all values will bind to the associated backing bean automatically.
//... <h:commandButton value="Save Changes" action="#{order.saveAction}" /> </h:column>
Example
A JSF 2.0 example to implement the above concept to update row in dataTable.
1. Managed Bean
A managed bean named “order”, self-explanatory.
package com.mkyong; import java.io.Serializable; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name="order") @SessionScoped public class OrderBean implements Serializable{ private static final long serialVersionUID = 1L; private static final ArrayList<Order> orderList = new ArrayList<Order>(Arrays.asList( new Order("A0001", "Intel CPU", new BigDecimal("700.00"), 1), new Order("A0002", "Harddisk 10TB", new BigDecimal("500.00"), 2), new Order("A0003", "Dell Laptop", new BigDecimal("11600.00"), 8), new Order("A0004", "Samsung LCD", new BigDecimal("5200.00"), 3), new Order("A0005", "A4Tech Mouse", new BigDecimal("100.00"), 10) )); public ArrayList<Order> getOrderList() { return orderList; } public String saveAction() { //get all existing value but set "editable" to false for (Order order : orderList){ order.setEditable(false); } //return to current page return null; } public String editAction(Order order) { order.setEditable(true); return null; } public static class Order{ String orderNo; String productName; BigDecimal price; int qty; boolean editable; public Order(String orderNo, String productName, BigDecimal price, int qty) { this.orderNo = orderNo; this.productName = productName; this.price = price; this.qty = qty; } //getter and setter methods } }
2. JSF page
JSF page to display the data with dataTable tag, and create a “edit” link to update the row record.
<?xml version="1.0" encoding="UTF-8"?> <!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:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" > <h:head> <h:outputStylesheet library="css" name="table-style.css" /> </h:head> <h:body> <h1>JSF 2 dataTable example</h1> <h:form> <h:dataTable value="#{order.orderList}" var="o" styleClass="order-table" headerClass="order-table-header" rowClasses="order-table-odd-row,order-table-even-row" > <h:column> <f:facet name="header">Order No</f:facet> <h:inputText value="#{o.orderNo}" size="10" rendered="#{o.editable}" /> <h:outputText value="#{o.orderNo}" rendered="#{not o.editable}" /> </h:column> <h:column> <f:facet name="header">Product Name</f:facet> <h:inputText value="#{o.productName}" size="20" rendered="#{o.editable}" /> <h:outputText value="#{o.productName}" rendered="#{not o.editable}" /> </h:column> <h:column> <f:facet name="header">Price</f:facet> <h:inputText value="#{o.price}" size="10" rendered="#{o.editable}" /> <h:outputText value="#{o.price}" rendered="#{not o.editable}" /> </h:column> <h:column> <f:facet name="header">Quantity</f:facet> <h:inputText value="#{o.qty}" size="5" rendered="#{o.editable}" /> <h:outputText value="#{o.qty}" rendered="#{not o.editable}" /> </h:column> <h:column> <f:facet name="header">Action</f:facet> <h:commandLink value="Edit" action="#{order.editAction(o)}" rendered="#{not o.editable}" /> </h:column> </h:dataTable> <h:commandButton value="Save Changes" action="#{order.saveAction}" /> </h:form> </h:body> </html>
3. Demo
From top to bottom, shows a row record being updated.












Update for the table-style.css :
.order-table{
border-collapse:collapse;
}
.order-table-header{
text-align:center;
background:none repeat scroll 0 0 #E5E5E5;
border-bottom:1px solid #BBBBBB;
padding:16px;
}
.order-table-odd-row{
background:none repeat scroll 0 0 #FFFFFF;
border-top:1px solid #BBBBBB;
}
.order-table-even-row{
background:none repeat scroll 0 0 #F9F9F9;
border-top:1px solid #BBBBBB;
}
.evenRow {
background-color: #F5F5DC;
}
.oddRow {
background-color: #FFE4C4;
}
.celluleLeft {
font-family:verdana,arial,helvetica,sans-serif;
font-size:13px;
text-align: left;
text-transform: uppercase;
}
.celluleRight {
font-family:verdana,arial,helvetica,sans-serif;
font-size:13px;
text-align: right;
}
.celluleCenter {
font-family:verdana,arial,helvetica,sans-serif;
font-size:13px;
text-align: center;
text-transform: uppercase;
}
I merged the three examples in one : ADD, UPDATE, DELETE in one XHTML page and one Java Bean, with many improvements :
Hi Mkyong,
can you give tutor, how can implementation in database jsf-mysql about “Update Concept”.
what the mean :
”
for (Order order : orderList){
order.setEditable(false);
}
”
thank you before.
Hey Mkyong,
This is very helpful example. But i have make a separate class for Order,so i cant get update the inputText from outputText when i m click on edit button. So,what should be the problem? Because i make a separate class for Order that;s why i m not able to change the textbox? Thanks in advance
Hello, first of all thanks for this amazing example!
How can I do to keep non editable one of the registries of the row and the others editables when I press the “edit” button?
Thanks a lot!
Andrew
Sorry, i didn’t implement in JSF before, but jQuery. Hope someone can help you.
Hello,
First, thanks a lot for this helpful tutorial.
I’m running the above over Google App Engine with Java 1.6 and I get the following when loading the page.
I realize it is probably related to the fact of passing a parameter via JSF when calling order.editAction(o). MAybe related to the Google environment.
Please advise.
Thanks,
Hagay.
Problem accessing /orderForm.jsf. Reason:
/orderForm.xhtml @67,71 action=”#{order.editAction(o)}” Error Parsing: #{order.editAction(o)}
Caused by:
javax.faces.view.facelets.TagAttributeException: /orderForm.xhtml @67,71 action=”#{order.editAction(o)}” Error Parsing: #{order.editAction(o)}
at com.sun.faces.facelets.tag.TagAttributeImpl.getMethodExpression(TagAttributeImpl.java:232)
……
Caused by: javax.el.ELException: Error Parsing: #{order.editAction(o)}
at org.apache.el.lang.ExpressionBuilder.createNodeInternal(ExpressionBuilder.java:125)
……
Caused by: org.apache.el.parser.ParseException: Encountered ” “(” “( “” at line 1, column 19.
Was expecting one of:
“}” …
Make sure your environment is using >= JSF2.0 , to support parameter passing in EL method. The common mistake is using old JSF jars and try to use the new EL implementation.
Lovely tutorial. Clean and great explanation.
Unfortunately it’s not working when I’m implementing it with my own code.
Do you know about any know programmering errors with this approach?
How can action attribut take an argument of type order?Kindly reply soon
Oh this is awesome article. Maybe once we could tackle lazy loading (on demand loading). I had run some examples, it’s tough but possible…
Do you mind to share your examples?