Sunday, September 19, 2010

Scrollable datatable in JSF

Today we will see one of  the important and useful features of  JSF Framework Scrollable datatable.
This is very useful to improve look of you web page.When we click on DisplayDetails button table will be populated.
Steps for creating Scrollable datatable.
1)Create datatable.jsp file
datatable.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%--
This file is an entry point for JavaServer Faces application.
--%>
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>JSP Page</title>
</head>
<body>
<h:form >
<jc:AlertMessage />
<h:panelGroup>
<h:commandButton id="cb_save" value="Display Details" style="position:absolute;left:490px;" action="#{scroll.bt_SAVEActionPerformed}" > </h:commandButton>
</h:panelGroup>
<h:panelGroup id="pn_DETAILS_GRP" style="overflow:auto;position:absolute;top:70px;left:400px;width:300px;height:150px;solid black">
<h:dataTable id="tb_USER_DETAILS" border="1" var="userDtls" value="#{scroll.userTable}" style="width:300px;height:150px">
<h:column id="SlNo">
<f:facet name="header">
<h:outputText value="Sl No" style="font-size:10pt" ></h:outputText>
</f:facet>
<h:outputText value="#{userDtls.userSlNo}" style="font-size:8pt"/>
</h:column>
<h:column id="firstName">
<f:facet name="header">
<h:outputText value="First Name" style="font-size:10pt"/>
</f:facet>
<h:outputText value="#{userDtls.userFirst_Name}" style="font-size:8pt"/>
</h:column>
<h:column id="lastName">
<f:facet name="header">
<h:outputText value="Last Name" style="font-size:10pt"/>
</f:facet>
<h:outputText value="#{userDtls.userLast_Name}" style="font-size:8pt"/>
</h:column>
</h:dataTable>
</h:panelGroup>
</h:form>
</body>
</html>
</f:view>
----------------------------------------------------------------------------------------------------------------------------
2) Create a package subin and inside subin package create javaclass file DataTable.java
DataTable.java
/*
* To change this template, choose Tools
Templates
* and open the template in the editor.
*/
package subin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
*
* @author SubinSuresh
*/
public class DataTable {

private HashMap userDtlsMap = new HashMap();
private List userTable = new ArrayList();

public void bt_SAVEActionPerformed() {
System.out.println("Debug bt_SAVEActionPerformed");
for (int i = 0; i < 15; i++) {
userDtlsMap = new HashMap();
System.out.println("Debug i"+i);
userDtlsMap.put("userSlNo", "" + (i + 1));
userDtlsMap.put("userFirst_Name", "subin");
userDtlsMap.put("userLast_Name", "suresh");
userTable.add(userDtlsMap);
}
}

/**
* @return the userDtlsMap
*/
public HashMap getUserDtlsMap() {
return userDtlsMap;
}

/**
* @param userDtlsMap the userDtlsMap to set
*/
public void setUserDtlsMap(HashMap userDtlsMap) {
this.userDtlsMap = userDtlsMap;
}
/**
* @return the userTable
*/

public List getUserTable() {
return userTable;
}

/**
* @param userTable the userTable to set
*/

public void setUserTable(List userTable) {
this.userTable = userTable;
}
}

-------------------------------------------------------------------------------------------------------------------
3)Mapping in faces-config.xml file
<managed-bean>
<managed-bean-name>scroll</managed-bean-name>
<managed-bean-class>subin.DataTable</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>



JSF Related topics: JCaptcha in JSF, Integrating Richfaces with JSF,Getting client and server sessionId in JSF and more.....

1 comment:

  1. Excelent!! very usefull. Thanks.
    Nicolas (Neuquen - Argentina)

    ReplyDelete