日期:2014-05-17  浏览次数:20706 次

求助:HTML页面中的table分页显示
RT
假如说一个表,从数据库中读出来,有100行,控制为每页25个,分页显示。
应该怎么实现呢?




------解决方案--------------------
http://rq2-79.iteye.com/blog/142626
------解决方案--------------------
楼主 搜下table分页就可以了,或者 直接用后台来分页吧
------解决方案--------------------
贴一段较齐全的分页给你参考下

Java code


<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="gbk"%>






<%

int pageSize = 3;

String strPageNo = request.getParameter("pageNo");
int pageNo;
if(strPageNo ==null || strPageNo.equals("")) {
    pageNo = 1;
} else {
    try {
        pageNo = Integer.parseInt(strPageNo.trim());
    } catch (NumberFormatException e) {
        pageNo = 1;
    }
    if(pageNo <= 0) {
        pageNo = 1;
    }
}






Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/bbsa", "root", "root");

Statement stmtCount = conn.createStatement();
ResultSet rsCount = stmtCount.executeQuery("select count(*) from article where pid = 0");
rsCount.next();
int totalRecords = rsCount.getInt(1);


int totalPages = totalRecords % pageSize == 0 ? totalRecords / pageSize : totalRecords / pageSize + 1;


if(pageNo > totalPages) {
    pageNo = totalPages;
}

int startPos = (pageNo - 1) * pageSize;

Statement stmt = conn.createStatement();
ResultSet rs =  stmt.executeQuery("select * from article where pid = 0 order by pdate desc limit " + startPos + "," + pageSize);


%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'ShowArticleTree.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    
    <link rel="stylesheet" type="text/css" href="main.css">

  </head>
  
  <body>
  
  <a href="Post.jsp">发表新帖</a><br/>
   
    <table border="1">
    
    <%
    while(rs.next()) {
    
    %>
    
    <tr>
        <td>
            <%=rs.getString("title") %>
        </td>
    </tr>
    
<%
}
rs.close();
stmt.close();
conn.close();
%>
    </table>
    
    <br/>共<%=totalPages %>页&nbsp;&nbsp;&nbsp;&nbsp;第<%=pageNo %>页
   
   &nbsp;&nbsp;&nbsp;&nbsp;<a href="ShowArticleFlat.jsp?pageNo=<%=pageNo == 1 ? 1 : pageNo - 1 %>">上一页</a>
   &nbsp;&nbsp;&nbsp;&nbsp;<a href="ShowArticleFlat.jsp?pageNo=<%=1 %>">首页</a>
   &nbsp;&nbsp;&nbsp;&nbsp;<a href="ShowArticleFlat.jsp?pageNo=<%=pageNo - 5 > 0 ? pageNo - 5 : pageNo %>">前五页</a>&nbsp;&nbsp;&nbsp;&nbsp;
    <%
    int pageArticles = 5;
    int pageRows = totalPages % pageArticles == 0 ? totalPages / pageArticles : totalPages / pageArticles + 1;
    //System.out.println(pageRows);
    %>
    <%
        int k = pageNo % pageArticles == 0 ? (pageNo / pageArticles) * pageArticles - 4 : (pageNo / pageArticles) * pageArticles + 1;
        int m = totalPages - pageNo < 1 ? k + (totalPages - pageNo) : k + 4;
        for(int j = k; j <= m; j++) {
    %>
   
   <a href="?pageNo=<%=j %>" <%=(pageNo == j) ? "class='bgOn'" : ""