自制DataGrid HTMLJ#SQLJSPServlet
程序员文章站
2022-07-06 22:47:55
...
自己写的一个用java在表单生成的GRID
比较乱
package com.znmt.j2ee.common.datagrid;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.http.*;
/**
* <warn></warn>
* <p>Title:datagrid </p>
* <p>Description: 按照指定SQL语句及相应的设置枸造表格,以html表示</p>
* <p>Copyright: Tidedge Computer. Co., Ltd.</p>
* <p>Company: 泰克艾奇 中南迈特 </p>
* @author xuejun
* @version 1.0
* @serialData 2004/12/7
* History:// 历史修改记录
* <author> <time> <version > <desc>
* xuejun 2004/12/7 1.01 增加静态列设置,包括文字静态列,图像态列设置
* xuejun 2004/12/8 1.02 将主键值从td里移到链接字符串中
* xuejun 2004/12/26 1.03 增加图片显示列
* xuejun 2004/12/27 1.04 增加图片变换列,在页面上增加排序功能
* xuejun 2004/12/28 1.05 增加简单分页功能
*/
public class grid
{
String strSQl; //SQL语句
String[] primaryKeys; //主键列数组
String[] columnsName; //中文列名数组
ArrayList dlinkeColums = new ArrayList(2); //动态键接列数组
ArrayList slinkeColums = new ArrayList(2); //静态键接列数组
ArrayList showImgColums = new ArrayList(2); //图片显示列数组
ArrayList imgTransitionColums = new ArrayList(2); //图片转换列
boolean isHiddenCheck;
/**
* 初始化设置表格查询语句
* @param str_sql SQL查询语句
*/
public grid(String str_sql)
{
this.strSQl = str_sql;
}
/**
* 设置是否隐藏checkbok
* @param isHidden
*/
public void setHiddenCheck(boolean isHidden)
{
this.isHiddenCheck = isHidden;
}
/**
* 增加图片转换列
* @param columnName 数据库列名
* @param showName 表头显示名
* @param Value 与Value相等时显示imgUrl1,否则显示imgUrl2
* @param linkUrl 链接地址
* @param imgUrl1 图片地址1
* @param imgUrl2 图片地址2
*/
public void addImgTransitionColum(String columnName,String showName,
String Value1,String Value2,
String linkUrl,
String imgUrl1,String imgUrl2)
{
String[] arrayImgTransitionColumn = new String[7];
arrayImgTransitionColumn[0] = columnName;
arrayImgTransitionColumn[1] = showName;
arrayImgTransitionColumn[2] = Value1;
arrayImgTransitionColumn[3] = Value2;
arrayImgTransitionColumn[4] = linkUrl;
arrayImgTransitionColumn[5] = imgUrl1;
arrayImgTransitionColumn[6] = imgUrl2;
//加入动态数组列表
this.imgTransitionColums.add(arrayImgTransitionColumn);
}
/**
* 获得是否隐藏checkbok
* @param isHidden
*/
public boolean isHiddenCheck()
{
return isHiddenCheck;
}
/**
*设置主键列数组
* @param priKeys 主键列数组
*/
public void setPriKey(String[] priKeys)
{
this.primaryKeys = priKeys;
}
/**
* 加入显示图片列
* @param columnName
* @param showName
* @param Value
* @param imgUrl
*/
public void addShowImgColumn(String columnName,String showName,
String Value,String imgUrl)
{
String[] arrayShowImgColumn = new String[4];
arrayShowImgColumn[0] = columnName;
arrayShowImgColumn[1] = showName;
arrayShowImgColumn[2] = Value;
arrayShowImgColumn[3] = imgUrl;
//加入动态数组列表
this.showImgColums.add(arrayShowImgColumn);
}
/**
*设置中文列名数组
* @param tableHeandName 中文列名数组
*/
public void setColumnName(String[] tableHeandName)
{
this.columnsName = tableHeandName;
}
/**
* 设置键接列 (动态链接列)
* @param columnName 链接列的列名(数据库字段名)
* @param linkUrl 链接列的URL地址
*/
public void addLinkColum(String columnName,String linkUrl)
{
String[] arrayLinkColum = new String[2];
arrayLinkColum[0] = columnName;
arrayLinkColum[1] = linkUrl;
//加入动态数组列表
this.dlinkeColums.add(arrayLinkColum);
}
/**
* 设置键接列 (静态链接列)
* @param linkName 链接列的列名(表头)
* @param linkUrl 链接列的URL地址
* @param JpgURL 图像文件的URL地址
*/
public void addLinkColum(String linkName,String linkUrl,
String JpgURL
)
{
String[] arrayLinkColum = new String[3];
arrayLinkColum[0] = linkName;
arrayLinkColum[1] = linkUrl;
arrayLinkColum[2] = JpgURL;
//加入静态数组列表
this.slinkeColums.add(arrayLinkColum);
}
/**
* 判断是否动态链接列
* @param columnN 列名(数据库字段名)
* @return boolean 布尔型
*/
public boolean isdLinkColum(String columnN)
{
String[] columnName;
// 判断是否动态链接列
for(int i = 0; i < this.dlinkeColums.size(); i++)
{
columnName = (String[])dlinkeColums.get(i);
if(columnName[0].equals(columnN))
{
return true;
}
}
return false;
}
/**
* 判断是否图片显示列链接列
* @param columnN
* @return
*/
public boolean isShowImgColum(String columnN)
{
String[] columnName;
// 判断是否图片显示列
for(int i = 0; i < this.showImgColums.size(); i++)
{
columnName = (String[])showImgColums.get(i);
if(columnName[0].equals(columnN))
{
return true;
}
}
return false;
}
/**
*
* @param columnN
* @return
*/
public boolean isImgTransitionColums(String columnN)
{
String[] columnName;
// 判断是否图片显示列
for(int i = 0; i < this.imgTransitionColums.size(); i++)
{
columnName = (String[])imgTransitionColums.get(i);
if(columnName[0].equals(columnN))
{
return true;
}
}
return false;
}
/**
* 根据设置返回grid的html字符串
* @return grid的html字符串
*/
public String getGridHtml(JspWriter out,HttpServletRequest req) throws
JspException
{
dbrs sqlgrid = new dbrs();
sqlgrid.connect();
//查询
sqlgrid.query(this.strSQl);
String jspName = req.getRequestURI(); //jsp文件名
jspName = jspName.substring(jspName.lastIndexOf("/") + 1);
boolean[] islink = new boolean[sqlgrid.columnCount]; //记录是否是链接列
boolean[] isShowImgColums = new boolean[sqlgrid.columnCount]; //记录是否是链接列
boolean[] isimgTransitionColum = new boolean[sqlgrid.columnCount]; //记录是否是图像变化列
boolean[] isprimary = new boolean[sqlgrid.columnCount]; //记录是否是主键列
String[] linkcolumnName; //链接列数组
String[] ShowImgColumName; //图片显示列数组
String[] ImgTransitionColumName; //图片转换列数组
int counter = 0; //计数器
int recordCount = 0; //记录总数
int pageSize = 10; //每页显示10条
int pageCount = 0; //总页数
int pageId = 1; //当前页
//获取记录总数
recordCount = sqlgrid.getrowcount();
System.out.println("recordCount:" + recordCount);
//计算总页数
pageCount = ((recordCount % pageSize) == 0 ? (recordCount / pageSize) :
(recordCount / pageSize) + 1);
int count = 1;
//获取要显示的页数
String strPage = req.getParameter("pageId");
//为null默认跳转到第一页
if(strPage == null)
{
pageId = 1;
}
//将显示的页数转为整型
else
{
pageId = java.lang.Integer.parseInt(strPage);
}
if(pageId > recordCount)
{
pageId = recordCount;
}
if(recordCount > 0)
{
sqlgrid.absolute((pageId - 1) * pageSize + 1);
}
//设置是否是链接列数据组
for(int i = 0; i < islink.length; i++)
{
islink[i] = this.isdLinkColum(sqlgrid.getColunmName(i));
}
/**
* 此处修改 2004-12-27
*/
//设置是否是图片链接列数据列
for(int i = 0; i < isShowImgColums.length; i++)
{
isShowImgColums[i] = this.isShowImgColum(sqlgrid.getColunmName(i));
}
//设置是否是图片转换列数据列
for(int i = 0; i < isimgTransitionColum.length; i++)
{
isimgTransitionColum[i] = this.isImgTransitionColums(sqlgrid.
getColunmName(i));
}
/**
* 注意:此处修改 2004-12-9
* 如果没指定主键列,默认第一列是主键列
*/
if(this.primaryKeys == null)
{
primaryKeys = new String[1];
primaryKeys[0] = sqlgrid.getColunmName(0);
}
//设置是否是主键列数据组
for(int j = 0; j < sqlgrid.columnCount; j++)
{
for(int p = 0; p < this.primaryKeys.length; p++)
{
isprimary[j] = sqlgrid.getColunmName(j).equals(primaryKeys[p]);
if(isprimary[j])
{
break;
}
}
}
String Html = "<center><table cellSpacing=\"0\" "
+ "cellPadding=\"0\" align=\"center\" border=\"0\">"
+ " <thead>";
//写表头
if(this.isHiddenCheck)
{
Html += "<tr><th><a onclick=\"this.blur(); return sortTable('defTblBdy'," +
counter + " , false);\""
+ " href=\"薛钧\">选择</th>";
/**
* 注意:此处修改 2004-12-9
* 如果没指定列名,则列名默认为列1、列2、列3......
*/
counter++;
}
if(this.columnsName == null)
{
columnsName = new String[sqlgrid.getcolumncount() - 1];
for(int i = 1;
i <
sqlgrid.getcolumncount() - showImgColums.size() -
imgTransitionColums.size();
i++)
{
columnsName[i - 1] = "列" + i;
}
}
//图片显示列名
for(int i = 0; i < this.showImgColums.size(); i++)
{
String[] showImgColum = (String[])this.showImgColums.get(i);
Html += "<th><a onclick=\"this.blur(); return sortTable('defTblBdy'," +
counter + " , false);\""
+ " href=\"薛钧\">";
Html += showImgColum[1];
Html += "</th>";
counter++;
}
for(int i = 0; i < this.columnsName.length; i++)
{
if(columnsName[i] != null)
{
Html += "<th><a onclick=\"this.blur(); return sortTable('defTblBdy'," +
counter + " , false);\""
+ " href=\"薛钧\">";
Html += columnsName[i];
Html += "</th>";
counter++;
}
}
//图片转换列
for(int i = 0; i < this.imgTransitionColums.size(); i++)
{
String[] imgTransitionColum = (String[])this.imgTransitionColums.get(i);
Html += "<th><a onclick=\"this.blur(); return sortTable('defTblBdy'," +
counter + " , false);\""
+ " href=\"薛钧\">";
Html += imgTransitionColum[1];
Html += "</th>";
counter++;
}
//静态链接列名
for(int i = 0; i < this.slinkeColums.size(); i++)
{
String[] slinkeColum = (String[])this.slinkeColums.get(i);
Html += "<th><a onclick=\"this.blur(); return sortTable('defTblBdy'," +
counter + " , false);\""
+ " href=\"薛钧\">";
Html += slinkeColum[0];
Html += "</th>";
counter++;
}
Html += "</tr></thead> <tbody id=\"defTblBdy\">";
/* int recordCounter=0;
while(test.nextrecord()&&count<=pageSize){*/
/*表内容*/
int recordCounter = 0;
//if (pageId==1)
//sqlgrid.beforeFirst();
while(count <= pageSize)
{
if(sqlgrid.getrowcount() == 0)
{
break;
}
recordCounter++;
count++;
Html += "<tr> ";
for(int j = 0; j <= sqlgrid.columnCount; j++)
{
if(j == 0 && this.isHiddenCheck)
{
/**
* 第一列为复选框,name为CB,value为主键
* 设置复选框id,id值为设该行的主键列值
* id=sqlgrid.getstring(primaryKeys[k])
*/
Html += "<td><input type=\"checkbox\" name=\"CB\" value=\"";
for(int k = 0; k < this.primaryKeys.length; k++)
{
Html += sqlgrid.getstring(primaryKeys[k]);
/*System.out.println(primaryKeys[k] + ":" +
sqlgrid.getstring(primaryKeys[k]) +
sqlgrid.getstring(j + 1));*/
if(this.primaryKeys.length > 1)
{
Html += ",";
}
}
Html += "\"></td>";
}
else
{
if(j == 0)
{
j = 1;
}
//判断动态链接列
if(islink[j - 1])
{
//是,定位到链接详细内容数组
for(int k = 0; k < this.dlinkeColums.size(); k++)
{
linkcolumnName = (String[])dlinkeColums.get(k);
if(linkcolumnName[0].equals(sqlgrid.getColunmName(j - 1)))
{
//找到链接详细内容数组
// System.out.print(sqlgrid.getstring(j) + " ");
/*注意:此处修改
修改后将主键值放入链接字符串中*/
Html += "<td>" + "<a href=\"" + linkcolumnName[1] + "?";
for(int p = 0; p < this.primaryKeys.length; p++)
{
Html += primaryKeys[p] + "=" +
sqlgrid.getstring(primaryKeys[p]);
if(this.primaryKeys.length > 1)
{
Html += ",";
}
}
//linkcolumnName[1]为链接URL地址
Html += "\">" + "</a></td>";
break;
}
}
}
/**
* 此处为新增代码
*/
if(isShowImgColums[j - 1])
{
//是,定位到图片显示列详细内容数组
for(int k = 0; k < this.showImgColums.size(); k++)
{
ShowImgColumName = (String[])showImgColums.get(k);
// System.out.println("进来");
if(ShowImgColumName[0].equals(sqlgrid.getColunmName(j - 1)))
{
// System.out.println("ShowImgColumName:" + ShowImgColumName[2]);
// System.out.println("sqlgrid.getstring(ShowImgColumName[0])" +
// sqlgrid.getstring(ShowImgColumName[0]));
if(sqlgrid.getstring(ShowImgColumName[0]).trim().equals(
ShowImgColumName[2]))
{
Html += "<td>" + "<img src=\"" + ShowImgColumName[3] + "\">";
// System.out.println("进来");
}
else
{
Html += "<td>";
}
//linkcolumnName[1]为链接URL地址
Html += "</td>";
break;
}
}
}
if(isimgTransitionColum[j - 1])
{
//是,定位到图片显示列详细内容数组
for(int k = 0; k < this.imgTransitionColums.size(); k++)
{
ImgTransitionColumName = (String[])imgTransitionColums.get(k);
// System.out.println("进来");
if(ImgTransitionColumName[0].equals(sqlgrid.getColunmName(j - 1)))
{
if(sqlgrid.getstring(ImgTransitionColumName[0]).trim().equals(
ImgTransitionColumName[2]))
{
Html += "<td><a href=\"" + ImgTransitionColumName[4] + "?";
for(int p = 0; p < this.primaryKeys.length; p++)
{
Html += primaryKeys[p] + "=" +
sqlgrid.getstring(primaryKeys[p]);
if(this.primaryKeys.length > 1)
{
Html += ",";
}
}
Html += "&&UserStatus=0\"><img "
+ " border=\"0\" src=\"" + ImgTransitionColumName[5]
+ "\"></a>";
}
else if(sqlgrid.getstring(ImgTransitionColumName[0]).trim().
equals(
ImgTransitionColumName[3]))
{
Html += "<td><a href=\"" + ImgTransitionColumName[4] + "?";
for(int p = 0; p < this.primaryKeys.length; p++)
{
Html += primaryKeys[p] + "=" +
sqlgrid.getstring(primaryKeys[p]);
if(this.primaryKeys.length > 1)
{
Html += ",";
}
}
Html += "&&UserStatus=1\"><img "
+ " border=\"0\" src=\"" + ImgTransitionColumName[6]
+ "\"></a>";
}
else
{
Html += "<td>";
}
//linkcolumnName[1]为链接URL地址
Html += "</td>";
break;
}
}
}
//普通列内容
else
{
if(!isprimary[j - 1] && !(isShowImgColums[j - 1]) &&
!(isimgTransitionColum[j - 1]))
{
// System.out.print(sqlgrid.getstring(j) + " ");
Html += "<td>" + sqlgrid.getstring(j) + "</td>";
}
}
}
/* */
}
//静态列内容
for(int n = 0; n < this.slinkeColums.size(); n++)
{
String[] slinkeColum = (String[])this.slinkeColums.get(n);
if(slinkeColum[2].equals(""))
{
//文字静态列
/*注意:此处修改
修改后将主键值放入链接字符串中*/
Html += "<td>" + "<a href=\"" + slinkeColum[1] + "?";
for(int p = 0; p < this.primaryKeys.length; p++)
{
Html += primaryKeys[p] + "=" + sqlgrid.getstring(primaryKeys[p]);
if(this.primaryKeys.length > 1)
{
Html += ",";
}
}
//linkcolumnName[1]为链接URL地址
Html += "\">" + slinkeColum[0] + "</a></td>";
/** //设置静态列链接地址和显示内容
Html += "\"><a href=\"" + slinkeColum[1] + "\">" +
slinkeColum[0] + "</a></td>";**/
}
else
{
//设置图像静态列URL
Html += "<td><a href=\"" + slinkeColum[1] + "?";
/*注意:此处修改
修改后将主键值放入链接字符串中*/
for(int p = 0; p < this.primaryKeys.length; p++)
{
Html += primaryKeys[p] + "=" + sqlgrid.getstring(primaryKeys[p]);
if(this.primaryKeys.length > 1)
{
Html += ",";
}
}
Html +=
"\"><img border=\"0\" src=\"" +
slinkeColum[2] + "\"></a></td>";
}
}
Html += "</tr>";
// System.out.println();
if(sqlgrid.nextrecord() == false)
{
break;
}
}
//数据读入完毕
sqlgrid.close();
//收尾
Html += "</tbody></table></center>";
/*
if(pageCount>1){
if(pageId==1){ out.print("<font color=\"#999999\">首页 上一页</font>"); }else{ %>
<a href="pagetest.jsp?pageId=1">首页</a> <a href="pagetest.jsp?pageId=<%=pageId-1%>">上一页</a>
<%}%>
<%if(pageId>=pageCount){out.print("<font color=\"#999999\">下一页 尾页</font>");}else{ %>
<a href="pagetest.jsp?pageId=<%=pageId+1%>">下一页</a> <a
href="pagetest.jsp?pageId=<%=pageCount%>">尾页</a>
<%}}else{
out.print("<font color=\"#999999\">首页 上一页 下一页 尾页</font>");
}
%>
*/
try
{
out.print(Html);
out.print("<form><center>");
if(pageCount > 1)
{
if(pageId == 1)
{
out.print("<font color=\"#999999\">首页 上一页</font>");
}
else
{
out.print("<a href=\"" + jspName + "?pageId=1\">首页</a> <a href=\"" +
jspName + "?pageId=" + (pageId - 1) + "\">上一页</a>");
}
if(pageId >= pageCount)
{
out.print("<font color=\"#999999\">下一页 尾页</font>");
}
else
{
out.print("<a href=\"" + jspName + "?pageId=" + (pageId + 1) +
"\">下一页</a> <a "
+ "href=\"" + jspName + "?pageId=" + pageCount +
"\">尾页</a>");
}
}
else
{
out.print("<font color=\"#999999\">首页 上一页 下一页 尾页</font>");
}
out.print("</center></form>");
Html = null;
}
catch(java.io.IOException je)
{
throw new JspException(je);
}
return "";
}
public static void main(String[] args)
{
//实例化grid
grid test = new grid("select * from huida");
//加修改列
test.addLinkColum("修改","dd","");
//表头数组
String[] ColumnsName =
{
"外键","回答人","回答内容","日期"};
//主键数组
String[] PriKeys =
{
"h_id"};
//设置表头
test.setColumnName(ColumnsName);
//设置主键
test.setPriKey(PriKeys);
// System.out.println(test.getGridHtml());
}
}
比较乱
package com.znmt.j2ee.common.datagrid;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.http.*;
/**
* <warn></warn>
* <p>Title:datagrid </p>
* <p>Description: 按照指定SQL语句及相应的设置枸造表格,以html表示</p>
* <p>Copyright: Tidedge Computer. Co., Ltd.</p>
* <p>Company: 泰克艾奇 中南迈特 </p>
* @author xuejun
* @version 1.0
* @serialData 2004/12/7
* History:// 历史修改记录
* <author> <time> <version > <desc>
* xuejun 2004/12/7 1.01 增加静态列设置,包括文字静态列,图像态列设置
* xuejun 2004/12/8 1.02 将主键值从td里移到链接字符串中
* xuejun 2004/12/26 1.03 增加图片显示列
* xuejun 2004/12/27 1.04 增加图片变换列,在页面上增加排序功能
* xuejun 2004/12/28 1.05 增加简单分页功能
*/
public class grid
{
String strSQl; //SQL语句
String[] primaryKeys; //主键列数组
String[] columnsName; //中文列名数组
ArrayList dlinkeColums = new ArrayList(2); //动态键接列数组
ArrayList slinkeColums = new ArrayList(2); //静态键接列数组
ArrayList showImgColums = new ArrayList(2); //图片显示列数组
ArrayList imgTransitionColums = new ArrayList(2); //图片转换列
boolean isHiddenCheck;
/**
* 初始化设置表格查询语句
* @param str_sql SQL查询语句
*/
public grid(String str_sql)
{
this.strSQl = str_sql;
}
/**
* 设置是否隐藏checkbok
* @param isHidden
*/
public void setHiddenCheck(boolean isHidden)
{
this.isHiddenCheck = isHidden;
}
/**
* 增加图片转换列
* @param columnName 数据库列名
* @param showName 表头显示名
* @param Value 与Value相等时显示imgUrl1,否则显示imgUrl2
* @param linkUrl 链接地址
* @param imgUrl1 图片地址1
* @param imgUrl2 图片地址2
*/
public void addImgTransitionColum(String columnName,String showName,
String Value1,String Value2,
String linkUrl,
String imgUrl1,String imgUrl2)
{
String[] arrayImgTransitionColumn = new String[7];
arrayImgTransitionColumn[0] = columnName;
arrayImgTransitionColumn[1] = showName;
arrayImgTransitionColumn[2] = Value1;
arrayImgTransitionColumn[3] = Value2;
arrayImgTransitionColumn[4] = linkUrl;
arrayImgTransitionColumn[5] = imgUrl1;
arrayImgTransitionColumn[6] = imgUrl2;
//加入动态数组列表
this.imgTransitionColums.add(arrayImgTransitionColumn);
}
/**
* 获得是否隐藏checkbok
* @param isHidden
*/
public boolean isHiddenCheck()
{
return isHiddenCheck;
}
/**
*设置主键列数组
* @param priKeys 主键列数组
*/
public void setPriKey(String[] priKeys)
{
this.primaryKeys = priKeys;
}
/**
* 加入显示图片列
* @param columnName
* @param showName
* @param Value
* @param imgUrl
*/
public void addShowImgColumn(String columnName,String showName,
String Value,String imgUrl)
{
String[] arrayShowImgColumn = new String[4];
arrayShowImgColumn[0] = columnName;
arrayShowImgColumn[1] = showName;
arrayShowImgColumn[2] = Value;
arrayShowImgColumn[3] = imgUrl;
//加入动态数组列表
this.showImgColums.add(arrayShowImgColumn);
}
/**
*设置中文列名数组
* @param tableHeandName 中文列名数组
*/
public void setColumnName(String[] tableHeandName)
{
this.columnsName = tableHeandName;
}
/**
* 设置键接列 (动态链接列)
* @param columnName 链接列的列名(数据库字段名)
* @param linkUrl 链接列的URL地址
*/
public void addLinkColum(String columnName,String linkUrl)
{
String[] arrayLinkColum = new String[2];
arrayLinkColum[0] = columnName;
arrayLinkColum[1] = linkUrl;
//加入动态数组列表
this.dlinkeColums.add(arrayLinkColum);
}
/**
* 设置键接列 (静态链接列)
* @param linkName 链接列的列名(表头)
* @param linkUrl 链接列的URL地址
* @param JpgURL 图像文件的URL地址
*/
public void addLinkColum(String linkName,String linkUrl,
String JpgURL
)
{
String[] arrayLinkColum = new String[3];
arrayLinkColum[0] = linkName;
arrayLinkColum[1] = linkUrl;
arrayLinkColum[2] = JpgURL;
//加入静态数组列表
this.slinkeColums.add(arrayLinkColum);
}
/**
* 判断是否动态链接列
* @param columnN 列名(数据库字段名)
* @return boolean 布尔型
*/
public boolean isdLinkColum(String columnN)
{
String[] columnName;
// 判断是否动态链接列
for(int i = 0; i < this.dlinkeColums.size(); i++)
{
columnName = (String[])dlinkeColums.get(i);
if(columnName[0].equals(columnN))
{
return true;
}
}
return false;
}
/**
* 判断是否图片显示列链接列
* @param columnN
* @return
*/
public boolean isShowImgColum(String columnN)
{
String[] columnName;
// 判断是否图片显示列
for(int i = 0; i < this.showImgColums.size(); i++)
{
columnName = (String[])showImgColums.get(i);
if(columnName[0].equals(columnN))
{
return true;
}
}
return false;
}
/**
*
* @param columnN
* @return
*/
public boolean isImgTransitionColums(String columnN)
{
String[] columnName;
// 判断是否图片显示列
for(int i = 0; i < this.imgTransitionColums.size(); i++)
{
columnName = (String[])imgTransitionColums.get(i);
if(columnName[0].equals(columnN))
{
return true;
}
}
return false;
}
/**
* 根据设置返回grid的html字符串
* @return grid的html字符串
*/
public String getGridHtml(JspWriter out,HttpServletRequest req) throws
JspException
{
dbrs sqlgrid = new dbrs();
sqlgrid.connect();
//查询
sqlgrid.query(this.strSQl);
String jspName = req.getRequestURI(); //jsp文件名
jspName = jspName.substring(jspName.lastIndexOf("/") + 1);
boolean[] islink = new boolean[sqlgrid.columnCount]; //记录是否是链接列
boolean[] isShowImgColums = new boolean[sqlgrid.columnCount]; //记录是否是链接列
boolean[] isimgTransitionColum = new boolean[sqlgrid.columnCount]; //记录是否是图像变化列
boolean[] isprimary = new boolean[sqlgrid.columnCount]; //记录是否是主键列
String[] linkcolumnName; //链接列数组
String[] ShowImgColumName; //图片显示列数组
String[] ImgTransitionColumName; //图片转换列数组
int counter = 0; //计数器
int recordCount = 0; //记录总数
int pageSize = 10; //每页显示10条
int pageCount = 0; //总页数
int pageId = 1; //当前页
//获取记录总数
recordCount = sqlgrid.getrowcount();
System.out.println("recordCount:" + recordCount);
//计算总页数
pageCount = ((recordCount % pageSize) == 0 ? (recordCount / pageSize) :
(recordCount / pageSize) + 1);
int count = 1;
//获取要显示的页数
String strPage = req.getParameter("pageId");
//为null默认跳转到第一页
if(strPage == null)
{
pageId = 1;
}
//将显示的页数转为整型
else
{
pageId = java.lang.Integer.parseInt(strPage);
}
if(pageId > recordCount)
{
pageId = recordCount;
}
if(recordCount > 0)
{
sqlgrid.absolute((pageId - 1) * pageSize + 1);
}
//设置是否是链接列数据组
for(int i = 0; i < islink.length; i++)
{
islink[i] = this.isdLinkColum(sqlgrid.getColunmName(i));
}
/**
* 此处修改 2004-12-27
*/
//设置是否是图片链接列数据列
for(int i = 0; i < isShowImgColums.length; i++)
{
isShowImgColums[i] = this.isShowImgColum(sqlgrid.getColunmName(i));
}
//设置是否是图片转换列数据列
for(int i = 0; i < isimgTransitionColum.length; i++)
{
isimgTransitionColum[i] = this.isImgTransitionColums(sqlgrid.
getColunmName(i));
}
/**
* 注意:此处修改 2004-12-9
* 如果没指定主键列,默认第一列是主键列
*/
if(this.primaryKeys == null)
{
primaryKeys = new String[1];
primaryKeys[0] = sqlgrid.getColunmName(0);
}
//设置是否是主键列数据组
for(int j = 0; j < sqlgrid.columnCount; j++)
{
for(int p = 0; p < this.primaryKeys.length; p++)
{
isprimary[j] = sqlgrid.getColunmName(j).equals(primaryKeys[p]);
if(isprimary[j])
{
break;
}
}
}
String Html = "<center><table cellSpacing=\"0\" "
+ "cellPadding=\"0\" align=\"center\" border=\"0\">"
+ " <thead>";
//写表头
if(this.isHiddenCheck)
{
Html += "<tr><th><a onclick=\"this.blur(); return sortTable('defTblBdy'," +
counter + " , false);\""
+ " href=\"薛钧\">选择</th>";
/**
* 注意:此处修改 2004-12-9
* 如果没指定列名,则列名默认为列1、列2、列3......
*/
counter++;
}
if(this.columnsName == null)
{
columnsName = new String[sqlgrid.getcolumncount() - 1];
for(int i = 1;
i <
sqlgrid.getcolumncount() - showImgColums.size() -
imgTransitionColums.size();
i++)
{
columnsName[i - 1] = "列" + i;
}
}
//图片显示列名
for(int i = 0; i < this.showImgColums.size(); i++)
{
String[] showImgColum = (String[])this.showImgColums.get(i);
Html += "<th><a onclick=\"this.blur(); return sortTable('defTblBdy'," +
counter + " , false);\""
+ " href=\"薛钧\">";
Html += showImgColum[1];
Html += "</th>";
counter++;
}
for(int i = 0; i < this.columnsName.length; i++)
{
if(columnsName[i] != null)
{
Html += "<th><a onclick=\"this.blur(); return sortTable('defTblBdy'," +
counter + " , false);\""
+ " href=\"薛钧\">";
Html += columnsName[i];
Html += "</th>";
counter++;
}
}
//图片转换列
for(int i = 0; i < this.imgTransitionColums.size(); i++)
{
String[] imgTransitionColum = (String[])this.imgTransitionColums.get(i);
Html += "<th><a onclick=\"this.blur(); return sortTable('defTblBdy'," +
counter + " , false);\""
+ " href=\"薛钧\">";
Html += imgTransitionColum[1];
Html += "</th>";
counter++;
}
//静态链接列名
for(int i = 0; i < this.slinkeColums.size(); i++)
{
String[] slinkeColum = (String[])this.slinkeColums.get(i);
Html += "<th><a onclick=\"this.blur(); return sortTable('defTblBdy'," +
counter + " , false);\""
+ " href=\"薛钧\">";
Html += slinkeColum[0];
Html += "</th>";
counter++;
}
Html += "</tr></thead> <tbody id=\"defTblBdy\">";
/* int recordCounter=0;
while(test.nextrecord()&&count<=pageSize){*/
/*表内容*/
int recordCounter = 0;
//if (pageId==1)
//sqlgrid.beforeFirst();
while(count <= pageSize)
{
if(sqlgrid.getrowcount() == 0)
{
break;
}
recordCounter++;
count++;
Html += "<tr> ";
for(int j = 0; j <= sqlgrid.columnCount; j++)
{
if(j == 0 && this.isHiddenCheck)
{
/**
* 第一列为复选框,name为CB,value为主键
* 设置复选框id,id值为设该行的主键列值
* id=sqlgrid.getstring(primaryKeys[k])
*/
Html += "<td><input type=\"checkbox\" name=\"CB\" value=\"";
for(int k = 0; k < this.primaryKeys.length; k++)
{
Html += sqlgrid.getstring(primaryKeys[k]);
/*System.out.println(primaryKeys[k] + ":" +
sqlgrid.getstring(primaryKeys[k]) +
sqlgrid.getstring(j + 1));*/
if(this.primaryKeys.length > 1)
{
Html += ",";
}
}
Html += "\"></td>";
}
else
{
if(j == 0)
{
j = 1;
}
//判断动态链接列
if(islink[j - 1])
{
//是,定位到链接详细内容数组
for(int k = 0; k < this.dlinkeColums.size(); k++)
{
linkcolumnName = (String[])dlinkeColums.get(k);
if(linkcolumnName[0].equals(sqlgrid.getColunmName(j - 1)))
{
//找到链接详细内容数组
// System.out.print(sqlgrid.getstring(j) + " ");
/*注意:此处修改
修改后将主键值放入链接字符串中*/
Html += "<td>" + "<a href=\"" + linkcolumnName[1] + "?";
for(int p = 0; p < this.primaryKeys.length; p++)
{
Html += primaryKeys[p] + "=" +
sqlgrid.getstring(primaryKeys[p]);
if(this.primaryKeys.length > 1)
{
Html += ",";
}
}
//linkcolumnName[1]为链接URL地址
Html += "\">" + "</a></td>";
break;
}
}
}
/**
* 此处为新增代码
*/
if(isShowImgColums[j - 1])
{
//是,定位到图片显示列详细内容数组
for(int k = 0; k < this.showImgColums.size(); k++)
{
ShowImgColumName = (String[])showImgColums.get(k);
// System.out.println("进来");
if(ShowImgColumName[0].equals(sqlgrid.getColunmName(j - 1)))
{
// System.out.println("ShowImgColumName:" + ShowImgColumName[2]);
// System.out.println("sqlgrid.getstring(ShowImgColumName[0])" +
// sqlgrid.getstring(ShowImgColumName[0]));
if(sqlgrid.getstring(ShowImgColumName[0]).trim().equals(
ShowImgColumName[2]))
{
Html += "<td>" + "<img src=\"" + ShowImgColumName[3] + "\">";
// System.out.println("进来");
}
else
{
Html += "<td>";
}
//linkcolumnName[1]为链接URL地址
Html += "</td>";
break;
}
}
}
if(isimgTransitionColum[j - 1])
{
//是,定位到图片显示列详细内容数组
for(int k = 0; k < this.imgTransitionColums.size(); k++)
{
ImgTransitionColumName = (String[])imgTransitionColums.get(k);
// System.out.println("进来");
if(ImgTransitionColumName[0].equals(sqlgrid.getColunmName(j - 1)))
{
if(sqlgrid.getstring(ImgTransitionColumName[0]).trim().equals(
ImgTransitionColumName[2]))
{
Html += "<td><a href=\"" + ImgTransitionColumName[4] + "?";
for(int p = 0; p < this.primaryKeys.length; p++)
{
Html += primaryKeys[p] + "=" +
sqlgrid.getstring(primaryKeys[p]);
if(this.primaryKeys.length > 1)
{
Html += ",";
}
}
Html += "&&UserStatus=0\"><img "
+ " border=\"0\" src=\"" + ImgTransitionColumName[5]
+ "\"></a>";
}
else if(sqlgrid.getstring(ImgTransitionColumName[0]).trim().
equals(
ImgTransitionColumName[3]))
{
Html += "<td><a href=\"" + ImgTransitionColumName[4] + "?";
for(int p = 0; p < this.primaryKeys.length; p++)
{
Html += primaryKeys[p] + "=" +
sqlgrid.getstring(primaryKeys[p]);
if(this.primaryKeys.length > 1)
{
Html += ",";
}
}
Html += "&&UserStatus=1\"><img "
+ " border=\"0\" src=\"" + ImgTransitionColumName[6]
+ "\"></a>";
}
else
{
Html += "<td>";
}
//linkcolumnName[1]为链接URL地址
Html += "</td>";
break;
}
}
}
//普通列内容
else
{
if(!isprimary[j - 1] && !(isShowImgColums[j - 1]) &&
!(isimgTransitionColum[j - 1]))
{
// System.out.print(sqlgrid.getstring(j) + " ");
Html += "<td>" + sqlgrid.getstring(j) + "</td>";
}
}
}
/* */
}
//静态列内容
for(int n = 0; n < this.slinkeColums.size(); n++)
{
String[] slinkeColum = (String[])this.slinkeColums.get(n);
if(slinkeColum[2].equals(""))
{
//文字静态列
/*注意:此处修改
修改后将主键值放入链接字符串中*/
Html += "<td>" + "<a href=\"" + slinkeColum[1] + "?";
for(int p = 0; p < this.primaryKeys.length; p++)
{
Html += primaryKeys[p] + "=" + sqlgrid.getstring(primaryKeys[p]);
if(this.primaryKeys.length > 1)
{
Html += ",";
}
}
//linkcolumnName[1]为链接URL地址
Html += "\">" + slinkeColum[0] + "</a></td>";
/** //设置静态列链接地址和显示内容
Html += "\"><a href=\"" + slinkeColum[1] + "\">" +
slinkeColum[0] + "</a></td>";**/
}
else
{
//设置图像静态列URL
Html += "<td><a href=\"" + slinkeColum[1] + "?";
/*注意:此处修改
修改后将主键值放入链接字符串中*/
for(int p = 0; p < this.primaryKeys.length; p++)
{
Html += primaryKeys[p] + "=" + sqlgrid.getstring(primaryKeys[p]);
if(this.primaryKeys.length > 1)
{
Html += ",";
}
}
Html +=
"\"><img border=\"0\" src=\"" +
slinkeColum[2] + "\"></a></td>";
}
}
Html += "</tr>";
// System.out.println();
if(sqlgrid.nextrecord() == false)
{
break;
}
}
//数据读入完毕
sqlgrid.close();
//收尾
Html += "</tbody></table></center>";
/*
if(pageCount>1){
if(pageId==1){ out.print("<font color=\"#999999\">首页 上一页</font>"); }else{ %>
<a href="pagetest.jsp?pageId=1">首页</a> <a href="pagetest.jsp?pageId=<%=pageId-1%>">上一页</a>
<%}%>
<%if(pageId>=pageCount){out.print("<font color=\"#999999\">下一页 尾页</font>");}else{ %>
<a href="pagetest.jsp?pageId=<%=pageId+1%>">下一页</a> <a
href="pagetest.jsp?pageId=<%=pageCount%>">尾页</a>
<%}}else{
out.print("<font color=\"#999999\">首页 上一页 下一页 尾页</font>");
}
%>
*/
try
{
out.print(Html);
out.print("<form><center>");
if(pageCount > 1)
{
if(pageId == 1)
{
out.print("<font color=\"#999999\">首页 上一页</font>");
}
else
{
out.print("<a href=\"" + jspName + "?pageId=1\">首页</a> <a href=\"" +
jspName + "?pageId=" + (pageId - 1) + "\">上一页</a>");
}
if(pageId >= pageCount)
{
out.print("<font color=\"#999999\">下一页 尾页</font>");
}
else
{
out.print("<a href=\"" + jspName + "?pageId=" + (pageId + 1) +
"\">下一页</a> <a "
+ "href=\"" + jspName + "?pageId=" + pageCount +
"\">尾页</a>");
}
}
else
{
out.print("<font color=\"#999999\">首页 上一页 下一页 尾页</font>");
}
out.print("</center></form>");
Html = null;
}
catch(java.io.IOException je)
{
throw new JspException(je);
}
return "";
}
public static void main(String[] args)
{
//实例化grid
grid test = new grid("select * from huida");
//加修改列
test.addLinkColum("修改","dd","");
//表头数组
String[] ColumnsName =
{
"外键","回答人","回答内容","日期"};
//主键数组
String[] PriKeys =
{
"h_id"};
//设置表头
test.setColumnName(ColumnsName);
//设置主键
test.setPriKey(PriKeys);
// System.out.println(test.getGridHtml());
}
}
下一篇: 很多人能看到大数据未来,但还没找到路