Adding row data dynamically in datatable in JSF
This code is useful when making order bill dynamically.
default.xhtml
<?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">
<h:head>
<h:outputStylesheet library="css" name="table-style.css" />
</h:head>
<h:body>
<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">Item</f:facet>
#{o.itemNo}
</h:column>
<h:column>
<f:facet name="header">Product Name</f:facet>
#{o.productName}
</h:column>
<h:column>
<f:facet name="header">Price</f:facet>
#{o.price}
</h:column>
<h:column>
<f:facet name="header">Quantity</f:facet>
#{o.qty}
</h:column>
<h:column>
<f:facet name="header">Line Total</f:facet>
#{o.lineTotal}
</h:column>
<h:column>
<f:facet name="header">Action</f:facet>
<h:commandLink value="Delete" action="#{order.deleteAction(o)}" />
</h:column>
</h:dataTable>
<br/><br/>
<h:outputLabel value="Grand Total: #{order.grandTotal}"></h:outputLabel>
<h3>Enter Order</h3>
<table>
<tr>
<td>Item:</td>
<td><h:inputText size="10" value="#{order.itemNo}" /></td>
</tr>
<tr>
<td>Product Name :</td>
<td><h:inputText size="20" value="#{order.productName}" /></td>
</tr>
<tr>
<td>Price :</td>
<td><h:inputText size="10" value="#{order.price}" /></td>
</tr>
<tr>
<td>Quantity :</td>
<td><h:inputText size="5" value="#{order.qty}" /></td>
</tr>
</table>
<h:commandButton value="Add" action="#{order.addAction}" />
</h:form>
</h:body>
</html>
OrderBean.java
package com.lftech.bean;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
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;
String itemNo;
String productName;
BigDecimal price;
Integer qty;
BigDecimal lineTotal = new BigDecimal("0.0");
BigDecimal grandTotal = new BigDecimal("0.0");
public String getItemNo() {
return itemNo;
}
public void setItemNo(String itemNo) {
this.itemNo = itemNo;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getQty() {
return qty;
}
public void setQty(Integer qty) {
this.qty = qty;
}
public BigDecimal getGrandTotal() {
return grandTotal;
}
public void setGrandTotal(BigDecimal grandTotal) {
this.grandTotal = grandTotal;
}
public BigDecimal getLineTotal() {
return lineTotal;
}
public void setLineTotal(BigDecimal lineTotal) {
this.lineTotal = lineTotal;
}
private static final ArrayList<Order> orderList = new ArrayList<Order>();
public ArrayList<Order> getOrderList() {
return orderList;
}
public String addAction(){
Order order = new Order(this.itemNo, this.productName,
this.price, this.qty, this.lineTotal);
orderList.add(order);
lineTotal = order.returnLineTotal();
grandTotal = grandTotal.add(lineTotal);
clearField();
return null;
}
public String deleteAction(Order order) {
orderList.remove(order);
grandTotal = grandTotal.subtract(order.lineTotal);
return null;
}
private void clearField() {
setItemNo("");
setProductName("");
setQty(new Integer("0"));
setPrice(new BigDecimal("0.0"));
}
}
Order.java
package com.lftech.bean;
import java.math.BigDecimal;
public class Order {
String itemNo;
String productName;
BigDecimal price;
Integer qty;
BigDecimal lineTotal;
public Order(String orderNo, String productName,
BigDecimal price, Integer qty, BigDecimal lineTotal) {
this.itemNo = orderNo;
this.productName = productName;
this.price = price;
this.qty = qty;
this.lineTotal = lineTotal;
}
public String getItemNo() {
return itemNo;
}
public void setItemNo(String itemNo) {
this.itemNo = itemNo;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getQty() {
return qty;
}
public void setQty(Integer qty) {
this.qty = qty;
}
public BigDecimal getLineTotal() {
lineTotal = new BigDecimal(qty).multiply(price);
return lineTotal;
}
public void setLineTotal(BigDecimal lineTotal) {
this.lineTotal = lineTotal;
}
public BigDecimal returnLineTotal() {
return new BigDecimal(qty).multiply(price);
}
}
default.xhtml
<?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">
<h:head>
<h:outputStylesheet library="css" name="table-style.css" />
</h:head>
<h:body>
<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">Item</f:facet>
#{o.itemNo}
</h:column>
<h:column>
<f:facet name="header">Product Name</f:facet>
#{o.productName}
</h:column>
<h:column>
<f:facet name="header">Price</f:facet>
#{o.price}
</h:column>
<h:column>
<f:facet name="header">Quantity</f:facet>
#{o.qty}
</h:column>
<h:column>
<f:facet name="header">Line Total</f:facet>
#{o.lineTotal}
</h:column>
<h:column>
<f:facet name="header">Action</f:facet>
<h:commandLink value="Delete" action="#{order.deleteAction(o)}" />
</h:column>
</h:dataTable>
<br/><br/>
<h:outputLabel value="Grand Total: #{order.grandTotal}"></h:outputLabel>
<h3>Enter Order</h3>
<table>
<tr>
<td>Item:</td>
<td><h:inputText size="10" value="#{order.itemNo}" /></td>
</tr>
<tr>
<td>Product Name :</td>
<td><h:inputText size="20" value="#{order.productName}" /></td>
</tr>
<tr>
<td>Price :</td>
<td><h:inputText size="10" value="#{order.price}" /></td>
</tr>
<tr>
<td>Quantity :</td>
<td><h:inputText size="5" value="#{order.qty}" /></td>
</tr>
</table>
<h:commandButton value="Add" action="#{order.addAction}" />
</h:form>
</h:body>
</html>
OrderBean.java
package com.lftech.bean;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
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;
String itemNo;
String productName;
BigDecimal price;
Integer qty;
BigDecimal lineTotal = new BigDecimal("0.0");
BigDecimal grandTotal = new BigDecimal("0.0");
public String getItemNo() {
return itemNo;
}
public void setItemNo(String itemNo) {
this.itemNo = itemNo;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getQty() {
return qty;
}
public void setQty(Integer qty) {
this.qty = qty;
}
public BigDecimal getGrandTotal() {
return grandTotal;
}
public void setGrandTotal(BigDecimal grandTotal) {
this.grandTotal = grandTotal;
}
public BigDecimal getLineTotal() {
return lineTotal;
}
public void setLineTotal(BigDecimal lineTotal) {
this.lineTotal = lineTotal;
}
private static final ArrayList<Order> orderList = new ArrayList<Order>();
public ArrayList<Order> getOrderList() {
return orderList;
}
public String addAction(){
Order order = new Order(this.itemNo, this.productName,
this.price, this.qty, this.lineTotal);
orderList.add(order);
lineTotal = order.returnLineTotal();
grandTotal = grandTotal.add(lineTotal);
clearField();
return null;
}
public String deleteAction(Order order) {
orderList.remove(order);
grandTotal = grandTotal.subtract(order.lineTotal);
return null;
}
private void clearField() {
setItemNo("");
setProductName("");
setQty(new Integer("0"));
setPrice(new BigDecimal("0.0"));
}
}
Order.java
package com.lftech.bean;
import java.math.BigDecimal;
public class Order {
String itemNo;
String productName;
BigDecimal price;
Integer qty;
BigDecimal lineTotal;
public Order(String orderNo, String productName,
BigDecimal price, Integer qty, BigDecimal lineTotal) {
this.itemNo = orderNo;
this.productName = productName;
this.price = price;
this.qty = qty;
this.lineTotal = lineTotal;
}
public String getItemNo() {
return itemNo;
}
public void setItemNo(String itemNo) {
this.itemNo = itemNo;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getQty() {
return qty;
}
public void setQty(Integer qty) {
this.qty = qty;
}
public BigDecimal getLineTotal() {
lineTotal = new BigDecimal(qty).multiply(price);
return lineTotal;
}
public void setLineTotal(BigDecimal lineTotal) {
this.lineTotal = lineTotal;
}
public BigDecimal returnLineTotal() {
return new BigDecimal(qty).multiply(price);
}
}
Comments
Post a Comment