jsp假分页,每一假页数据统计。
现在我直接用网上的代码进行假分页。
代码如下:
<script>
document.write("Hello World!")
var theTable = document.getElementById("table2");
var totalPage = document.getElementById("spanTotalPage");
var pageNum = document.getElementById("spanPageNum");
var spanPre = document.getElementById("spanPre");
var spanNext = document.getElementById("spanNext");
var spanFirst = document.getElementById("spanFirst");
var spanLast = document.getElementById("spanLast");
var numberRowsInTable = theTable.rows.length;
var pageSize = 21;
var page = 1;
var zhongrenshu=0;
var row;
//下一页
function next() {
hideTable();
zhongrenshu=0;
currentRow = pageSize * page;
maxRow = currentRow + pageSize;
if (maxRow > numberRowsInTable) maxRow = numberRowsInTable;
for (var i = currentRow; i < maxRow; i++) {
theTable.rows[i].style.display = '';
}
page++;
if (maxRow == numberRowsInTable) { nextText(); lastText(); }
showPage();
preLink();
firstLink();
}
document.write(zhongrenshu);
//上一页
function pre() {
hideTable();
page--;
currentRow = pageSize * page;
maxRow = currentRow - pageSize;
if (currentRow > numberRowsInTable) currentRow = numberRowsInTable;
for (var i = maxRow; i < currentRow; i++) {
theTable.rows[i].style.display = '';
}
if (maxRow == 0) { preText(); firstText(); }
showPage();
nextLink();
lastLink();
}
//第一页
function first() {
hideTable();
page = 1;
for (var i = 0; i < pageSize; i++) {
theTable.rows[i].style.display = '';
}
showPage();
preText();
nextLink();
lastLink();
}
//最后一页
function last() {
hideTable();
page = pageCount();
currentRow = pageSize * (page - 1);
for (var i = currentRow; i < numberRowsInTable; i++) {
theTable.rows[i].style.display = '';
}
showPage();
preLink();
nextText();
firstLink();
}
function hideTable() {
for (var i = 0; i < numberRowsInTable; i++) {
theTable.rows[i].style.display = 'none';
}
}
function showPage() {
pageNum.innerHTML = page;
}
//总共页数
function pageCount() {
var count = 0;
if (numberRowsInTable % pageSize != 0) count = 1;
return parseInt(numberRowsInTable / pageSize) + count;
}
//显示链接
function preLink() { spanPre.innerHTML = "<a href='javascript:pre();'>上一页</a>"; }
function preText() { spanPre.innerHTML = "上一页"; }
function nextLink() { spanNext.innerHTML = "<a href='javascript:next();'>下一页</a>"; }
function nextText() { spanNext.innerHTML = "下一页"; }
function firstLink() { spanFirst.innerHTML = "<a href='javascript:first();'>第一页</a>"; }
function firstText() { spanFirst.innerHTML = "第一页"; }
function lastLink() { spanLast.innerHTML = "<a href='javascript:last();'>最后一页</a>"; }
function lastText() { spanLast.innerHTML = "最后一页"; }
//隐藏表格
function hide() {
for (var i = pageSize; i < numberRowsInTable; i++) {
theTable.rows[i].style.display = 'none';
}
totalPage.innerHTML = pageCount();
pageNum.innerHTML = '1';
nextLink();
lastLink();
}
hide();
</script>
想在函数(下一页)中添加添加
zhongrenshu+=Integer.(((theTable.rows[i].)cell[2].)innerHtml);
如下:
function next() {
hideTable();
zhongrenshu=0;
currentRow = pageSize * page;
maxRow = currentRow + pageSize;
if (maxRow > numberRowsInTable) maxRow = numberRowsInTable;
for (var i = currentRow; i < maxRow; i++) {
theTable.rows[i].style.display = '';
zhongrenshu+=Integer.(((theTable.rows[i].)cell[2].)innerHtml);《《《《《《加入了此句
}
page++;
if (maxRow == numberRowsInTable) { nextText(); lastText(); }
showPage();
preLink();
firstLink();
}
document.write(zhongrenshu);
进行每一行的某一列数据进行统计,但加入此句后。页面就是把所有数据都显示出来。不进行假分页。并且数据也没有统计,有谁知道什么原因。或者怎么实现统计呢。
[解决办法]
js分页的代码网上很多,何必自己写呢。有很多的js的table控件,直接用很方便。
[解决办法]
感觉你这个好麻烦,我刚做了个jsp项目,是从数据库拿出数据,查询数据条数,在分页;可设置一页显示几天数据,简单明了,希望对你有帮助。我用的是SQL Server2005.
<%
//定义四个分页会用到的变量
int pageSize=15;//一页显示数据条数
int pageNow=1;//默认显示第一页
int rowCount=0;//该值从数据库中查询
int pageCount=0;//该值是通过pageSize和rowCount
//接受用户希望显示的页数(pageNow)
String s_pageNow=request.getParameter("pageNow");
if(s_pageNow!=null){
//接收到了pageNow
pageNow=Integer.parseInt(s_pageNow);
}
//查询得到rowCount
link.setSql("select count(*) from grass");
ResultSet rs=link.getRs();
if(rs.next()){
rowCount=rs.getInt(1);
}
//计算pageCount
if(rowCount%pageSize==0){
pageCount=rowCount/pageSize;
}else{
pageCount=rowCount/pageSize+1;
}
//查询出需要显示的记录
link.setSql("select top "+pageSize
+" * from grass where Gname="+gname+" and Gid not in(select top "
+pageSize*(pageNow-1)+" Gid from grass)");
rs=link.getRs();
%>
<table border="1" width="1022px">
<tr><td>编号</td><td>牧草名</td><td>拉丁名</td><td>英文名</td><td>别名</td><td>点击查看</td></tr>
<%
while(rs.next()){
%>
<tr><td><%=rs.getInt(1)%></td><td><%=rs.getString(6)%></td><td><%=rs.getString(7)%></td><td><%=rs.getString(8)%></td>
<td><%=rs.getString(9)%></td><td><%out.print("<a href=grasspage.jsp?g="+rs.getString(1)+">详细信息</a>"); %></td></tr>
<%}%>
</table>
<%
//上一页
if(pageNow!=1){
out.println("<a href=grass.jsp?pageNow="+(pageNow-1)+">上一页</a>");
}
//显示超链接
for(int i=1;i<=pageCount;i++){
out.println("<a href=grass.jsp?pageNow="+i+">["+i+"]</a>");
}
//下一页
if(pageNow!=pageCount){
out.println("<a href=grass.jsp?pageNow="+(pageNow+1)+">下一页</a>");
}
%>