Forum

Post 29.06.2012   # 1
Subject Problem with Navigation in DataGird

Hello Everybody.

I am using Struts2 and populating my data into the DataGrid view..if i simple display in into my web page i am getting good result and also the navigation from page 1 to page 2 so on is also working well..

But when i displaying the DataGird in tabbedpanel the navigation is not working for me..please do help..

Here is my code for that grid view and also the corresponding Tag Java Class

Display.jsp:

--------------------

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
 <%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="/WEB-INF/datagrid.tld" prefix="grd" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.sql.DataSource" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%
int intCurr = 1;
int intSortOrd = 0;
String strTmp = null;
String strSQL = null;
String strSortCol = null;
String strSortOrd = "ASC";
boolean blnSortAsc = true;
strSQL = "SELECT NAME,PASSSWORD,EMAIL FROM STUDENT";
Connection objCnn    = null;
Class      objDrvCls = null;
Context    ctx       = null;
Context    envCtx    = null;
DataSource ds        = null;
 
 Class.forName("com.ibm.db2.jcc.DB2Driver");
objCnn=DriverManager.getConnection("url","username","password");

strTmp = request.getParameter("txtCurr");
try
{
    if (strTmp != null)
        intCurr = Integer.parseInt(strTmp);
}
catch (NumberFormatException NFEx)
{
}
strSortCol = request.getParameter("txtSortCol");
strSortOrd = request.getParameter("txtSortAsc");
if (strSortCol == null) strSortCol = "PASSSWORD";
if (strSortOrd == null) strSortOrd = "ASC";
blnSortAsc = (strSortOrd.equals("ASC"));
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Home Page</title>
<head>
<SCRIPT type="text/javascript">
    window.history.forward();
    function noBack() { window.history.forward(); }
</SCRIPT>
<link REL="StyleSheet" HREF="../css/GridStyle.css">
<script LANGUAGE="javascript">
function doNavigate(pstrWhere, pintTot)
{
    var strTmp;
    var intPg;

    strTmp = document.frmMain.txtCurr.value;
    intPg = parseInt(strTmp);
    if (isNaN(intPg)) intPg = 1;

    if ((pstrWhere == 'F' || pstrWhere == 'P') && intPg == 1)
    {
        alert("You are already viewing first page!");
        return;
    }
    else if ((pstrWhere == 'N' || pstrWhere == 'L') && intPg == pintTot)
    {
        alert("You are already viewing last page!");
        return;
    }

    if (pstrWhere == 'F')
        intPg = 1;
    else if (pstrWhere == 'P')
        intPg = intPg - 1;
    else if (pstrWhere == 'N')
        intPg = intPg + 1;
    else if (pstrWhere == 'L')
        intPg = pintTot;

    if (intPg < 1)
        intPg = 1;
    if (intPg > pintTot)
        intPg = pintTot;
    document.frmMain.txtCurr.value = intPg;
    document.frmMain.submit();
}

function doSort(pstrFld, pstrOrd)
{
    document.frmMain.txtSortCol.value = pstrFld;
    document.frmMain.txtSortAsc.value = pstrOrd;
    document.frmMain.submit();
}
function goToPage(element) {
    document.frmMain.txtCurr.value = element.options[element.selectedIndex].value;
    document.frmMain.submit();
}
</script>
</head>
<body onload="noBack();"
    onpageshow="if (event.persisted) noBack();" onunload="">
<h1>Welcome <s:property value="uname"/> </h1>
<h3>Home Page Content Goes here</h3>
<form NAME="frmMain" METHOD="post" action="../home/DeleteTable.jsp">
        <grd:dbgrid id="tblStat" name="tblStat" width="100" pageSize="5" currentPage="<%=intCurr%>"
                    border="0" cellSpacing="1" cellPadding="2" dataMember="<%=strSQL%>" dataSource="<%=objCnn%>"
                    cssClass="gridTable">
           
            <grd:gridpager imgFirst="../images/First.gif" imgPrevious="../images/Previous.gif" imgNext="../images/Next.gif" imgLast="../images/Last.gif"></grd:gridpager>
           
            <grd:gridsorter sortColumn="<%=strSortCol%>" sortAscending="<%=blnSortAsc%>"></grd:gridsorter>
           
            <grd:rownumcolumn headerText="S NO" width="5" HAlign="right"></grd:rownumcolumn>
           
          
           
            <grd:numbercolumn dataField="NAME" headerText="NAME" width="10" />
                <grd:textcolumn dataField="PASSSWORD" headerText="PASSSWORD" width="50" sortable="true" ></grd:textcolumn>
                <grd:textcolumn dataField="EMAIL" headerText="EMAIL" width="50" sortable="true"></grd:textcolumn>
     
      <grd:imagecolumn headerText="DELETE" width="5" HAlign="center" imageSrc="../images/delete.jpeg" linkUrl="doDelete.action?name={NAME}" imageBorder="0" imageWidth="16" imageHeight="16" alterText="Click to edit"/>
</grd:dbgrid>
<input TYPE="hidden" NAME="txtCurr" VALUE="<%=intCurr%>">
<input TYPE="hidden" NAME="txtSortCol" VALUE="<%=strSortCol%>">
<input TYPE="hidden" NAME="txtSortAsc" VALUE="<%=strSortOrd%>">
</form> 
   
   
</body>
</html>
<%
try
{
    if (objCnn != null)
        objCnn.close();
}
catch (SQLException SQLExIgnore)
{
}
if (objCnn != null) objCnn = null;
if (envCtx != null) envCtx = null;
if (ctx != null) ctx = null;
%>

GridPager.java:

--------------------------

/*------------------------------------------------------------------------------
 * PACKAGE: com.freeware.gridtag
 * FILE   : GridPager.java
 
 *------------------------------------------------------------------------------
 * Change Log:
 *-----------------------------------------------------------------------------*/
package com.freeware.gridtag;
 
import java.io.IOException;
 
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
 
/**
 * This class represents the grid pager.
 
 */
public final class GridPager extends TagSupport {
    private String mstrImgFirst;
    private String mstrImgPrevious;
    private String mstrImgNext;
    private String mstrImgLast;
    private String mstrAddImage;
    private String mstrAddAction;
    private String mstrScriptFunction;
    private String mstrSearchBy;
    private String mstrSearchFunction;
    
    public GridPager() {
        super();
        this.mstrImgFirst = "First.gif";
        this.mstrImgPrevious = "Previous.gif";
        this.mstrImgNext = "Next.gif";
        this.mstrImgLast = "Last.gif";
        this.setAddImage("Add.png");
        
    }
    
/*------------------------------------------------------------------------------
 * Getters
 *----------------------------------------------------------------------------*/
    public String getImgFirst() {
        return this.mstrImgFirst;
    }
    
    public String getImgPrevious() {
        return this.mstrImgPrevious;
    }
    
    public String getImgNext() {
        return this.mstrImgNext;
    }
    
    public String getImgLast() {
        return this.mstrImgLast;
    }
    public String getAddImage() {
        return mstrAddImage;
    }
    public String getAddAction() {
        return mstrAddAction;
    }
    
    public String getScriptFunction(){
        return mstrScriptFunction;
    }
    
    public String getSearchBy() {
        return mstrSearchBy;
    }
    
    public String getSearchFunction(){
        return mstrSearchFunction;
    }
/*------------------------------------------------------------------------------
 * Setters
 *----------------------------------------------------------------------------*/
    public void setImgFirst(String pstrImgFirst) {
        this.mstrImgFirst = pstrImgFirst;
    }
    
    public void setImgPrevious(String pstrImgPreviosu) {
        this.mstrImgPrevious = pstrImgPreviosu;
    }
    
    public void setImgNext(String pstrImgNext) {
        this.mstrImgNext = pstrImgNext;
    }
    
    public void setImgLast(String pstrImgLast) {
        this.mstrImgLast = pstrImgLast;
    }
    public void setAddImage(String pstrAddImage) {
        this.mstrAddImage = pstrAddImage;
    }
    public void setAddAction(String pstrAddAction) {
        this.mstrAddAction = pstrAddAction;
    }
    public void setScriptFunction(String pstrScriptFunction){
        this.mstrScriptFunction = pstrScriptFunction;
    }
    
    public void setSearchBy(String pstrSearchBy) {
        this.mstrSearchBy = pstrSearchBy;
    }
    
    public void setSearchFunction(String pstrSearchFunction){
        this.mstrSearchFunction = pstrSearchFunction;
    }
/*------------------------------------------------------------------------------
 * Overridden methods
 * @see javax.servlet.jsp.tagext.Tag
 *----------------------------------------------------------------------------*/
    /* (non-Javadoc)
     * @see javax.servlet.jsp.tagext.Tag#doEndTag()
     */
    public int doEndTag() throws JspException {
        DBGrid objTmp = null;
        
        try {
            objTmp = (DBGrid) getParent();
            objTmp.setPager(getCopy());
        } catch (ClassCastException CCEx) {
            throw new JspException("Error: ImageColumn tag is not a child of DBGrid", CCEx);
        } finally {
            if (objTmp != null) objTmp = null;
        }
        return EVAL_PAGE;
    }
    
    /* (non-Javadoc)
     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
     */
    public int doStartTag() throws JspException {
        if (!(this.getParent() instanceof DBGrid))
            throw new JspException("Error: Column tag needs to be a child of DBGrid!");
        
        // This tag does not have body contents.
        return SKIP_BODY;
    }
    
/*------------------------------------------------------------------------------
 * Methods
 *----------------------------------------------------------------------------*/
    public void renderPager(int pintCurrPage, int pintTotRec, int pintPageSize) throws JspException {
        int          intPages = 0;
        StringBuffer objBuf = null;
        int recordStart = 0;
        int recordEnd = 0;
        int curRecord = 0;
        String pageRecordsInfo = null;
        String javaScriptFunction = null;
        try {
            if ((pintTotRec % pintPageSize) == 0)
                intPages = pintTotRec / pintPageSize;
            else
                intPages = (pintTotRec / pintPageSize) + 1;
            
            /**
             * Calculation to render the current page records information
             */
            if(pintCurrPage == 1){
                if(pintTotRec == 0){
                    pageRecordsInfo = "(0 - 0 of "+(pintTotRec+1)+")";
                } else if(pintTotRec >= pintPageSize){
                    pageRecordsInfo = "(1 - "+pintPageSize+" of "+(pintTotRec+1)+")";
                }else {
                    recordEnd = pintTotRec % pintPageSize;
                    pageRecordsInfo = "(1 - "+recordEnd+" of "+(pintTotRec+1)+")";
                }
                
            }else if(pintCurrPage == intPages){
                
                recordEnd = pintTotRec % pintPageSize;
                
                if(recordEnd == 0){
                    curRecord = (pintTotRec-pintPageSize)+1;
                    recordEnd = (curRecord+pintPageSize)-1;
                    pageRecordsInfo = "("+curRecord+" - "+recordEnd+" of "+(pintTotRec+1)+")";
                }else{
                    curRecord = (pintTotRec-recordEnd)+1;
                    pageRecordsInfo = "("+curRecord+" - "+pintTotRec+" of "+(pintTotRec+1)+")";
                }
                
            }else if((pintCurrPage != 1) && (pintCurrPage != intPages)){
                curRecord = ((pintCurrPage-1) * pintPageSize)+1;
                recordEnd = ((pintCurrPage-1) * pintPageSize)+pintPageSize;
                pageRecordsInfo = "("+curRecord+" - "+recordEnd+" of "+(pintTotRec+1)+")";
            }
            
            if(getScriptFunction() != null){
                javaScriptFunction = "doNavigate";
            }else{
                javaScriptFunction = "doNavigate";
            }
            
            objBuf = new StringBuffer();
            objBuf.append("
<table BORDER=0 CELLSPACING=0 CELLPADDING=0 ");
            objBuf.append("WIDTH=\"100%\">
\r\n");
            //objBuf.append("
<tr CLASS=\"gridPager gridPageOfPage\">
\r\n");
            objBuf.append("
<tr>
\r\n");
            objBuf.append("
<td ALIGN=\"left\" WIDTH=\"50%\">\r\n");
            objBuf.append("<a HREF=\"javascript:"+javaScriptFunction+"('F', " + intPages +
                    ")\">\r\n");
            objBuf.append("<img SRC=\"" + this.mstrImgFirst + "\" BORDER=0></a>\r\n");
            objBuf.append("<a HREF=\"javascript:"+javaScriptFunction+"('P', " + intPages +
                    ")\">\r\n");
            objBuf.append("<img SRC=\"" + this.mstrImgPrevious + "\" BORDER=0></a>\r\n");
            
            objBuf.append("<span class=\"gridPager\">"+pageRecordsInfo+"</span>\r\n");
            
            objBuf.append("<a HREF=\"javascript:"+javaScriptFunction+"('N', " + intPages +
                    ")\">\r\n");
            objBuf.append("<img SRC=\"" + this.mstrImgNext + "\" BORDER=0></a>\r\n");
            objBuf.append("<a HREF=\"javascript:"+javaScriptFunction+"('L', " + intPages +
                    ")\">\r\n");
            objBuf.append("<img SRC=\"" + this.mstrImgLast + "\" BORDER=0></a>\r\n");
            
            // START:For Adding Button
            if(!"".equals(this.getAddAction()) && this.getAddAction()!=null) {
                objBuf.append("<a HREF=\"");
                objBuf.append(resolveFields(this.getAddAction()));
                objBuf.append("\" style=\"padding-left=30px;\">\r\n");
                objBuf.append("<img SRC=\"" + this.getAddImage() + "\" BORDER=\"0\" WIDTH=\"51\" HEIGHT=\"20\">");
                objBuf.append("</a>\r\n");
            }
            //END: For Adding Button
            
            //START: For Adding Search Capability
            if(!"".equalsIgnoreCase(this.getSearchBy()) && this.getSearchBy()!=null) {
                objBuf.append("<span class=\"gridPager\">&nbsp; Search By: </span>");
                objBuf.append("<input type=text  name='searchBy' class='inputTextBlue'  value='" + this.getSearchBy() + "' " + " onkeypress='return handleEnter(this,event);'>");
                objBuf.append("<input type='button' name='searchButton' class='buttonBg' value='Search' onclick='"+this.getSearchFunction()+"();'>");
            }
            //END: For Adding Search Capability
            objBuf.append("</td>
\r\n");
            
            // START:For Adding Button
            /*objBuf.append("
<td ALIGN=\"left\" WIDTH=\"20%\">\r\n");
            if(!"".equals(this.getAddAction()) && this.getAddAction()!=null) {
                objBuf.append("<a HREF=\"");
                objBuf.append(resolveFields(this.getAddAction()));
                objBuf.append("\">\r\n");
                objBuf.append("<img SRC=\"" + this.getAddImage() + "\" BORDER=\"0\" WIDTH=\"24\" HEIGHT=\"12\">");
                objBuf.append("</a>\r\n");
            }
            objBuf.append("</td>
\r\n");*/
            //END: For Adding Button
            objBuf.append("
<td CLASS=\"gridPageOfPage\" WIDTH=\"25%\" >\r\n");
            objBuf.append("Go To Page:");
            objBuf.append("<select id='goToPageGrid' name='goToPageGrid' value='"+pintCurrPage+"' onchange='goToPage(this);'>\r\n");
            //System.err.println("pintCurrPage---"+pintCurrPage);
            objBuf.append("<option value=''>Select</option>");
            for(int index=1;index<=intPages;index++) {
                objBuf.append("<option value='"+index+"'>"+index+"</option>");
            }
            objBuf.append("</select>");
            objBuf.append("</td>
\r\n");
            objBuf.append("
<td ALIGN=\"right\" WIDTH=\"25%\" ");
            objBuf.append("CLASS=\"gridPageOfPage\">\r\n");
            if(pintTotRec == 0){
                objBuf.append("Page 0 of " + intPages);
            }else{
                objBuf.append("Page " + pintCurrPage + " of " + intPages);
            }
            objBuf.append("</td>
\r\n");
            objBuf.append("
</tr>
\r\n");
            objBuf.append("
</table>
\r\n");
            this.pageContext.getOut().println(objBuf.toString());
        } catch (IOException IoEx) {
            throw new JspException("Error: While drawing grid pager!", IoEx);
        } finally {
            if (objBuf != null) objBuf = null;
        }
    }
    
/*------------------------------------------------------------------------------
 * Helpers
 *----------------------------------------------------------------------------*/
    
    private String resolveFields(String pstrUrl) throws ClassCastException {
        int    intPos = 0;
        int    intEnd = 0;
        String strCol = null;
        String strRet = null;
        DBGrid objTmp = null;
        
        strRet = pstrUrl;
        objTmp = (DBGrid) getParent();
        intPos = strRet.indexOf("{");
        while (intPos >= 0) {
            intEnd = strRet.indexOf("}", intPos + 1);
            if (intEnd != -1) {
                strCol = strRet.substring(intPos + 1, intEnd);
                strRet = strRet.substring(0, intPos) +
                        objTmp.getColumnValue(strCol) +
                        strRet.substring(intEnd + 1);
            }
            intPos = strRet.indexOf("{", intPos +1);
        }
        return strRet;
    }
    
 
    private GridPager getCopy() {
        GridPager objRet = null;
        
        objRet = new GridPager();
        objRet.setId(this.getId());
        objRet.setPageContext(this.pageContext);
        objRet.setParent(this.getParent());
        objRet.setImgFirst(this.mstrImgFirst);
        objRet.setImgPrevious(this.mstrImgPrevious);
        objRet.setImgNext(this.mstrImgNext);
        objRet.setImgLast(this.mstrImgLast);
        objRet.setAddImage(this.mstrAddImage);
        objRet.setAddAction(this.mstrAddAction);
        objRet.setScriptFunction(this.mstrScriptFunction);
        objRet.setSearchBy(this.mstrSearchBy);
        objRet.setSearchFunction(this.mstrSearchFunction);
        return objRet;
    }
}

Thank in advance..

Replies will be appriciated

atummapala is offline Reply

Please login to post reply.