求助:HTML页面中的table分页显示
RT
假如说一个表,从数据库中读出来,有100行,控制为每页25个,分页显示。
应该怎么实现呢?
[解决办法]
http://rq2-79.iteye.com/blog/142626
[解决办法]
楼主 搜下table分页就可以了,或者 直接用后台来分页吧
[解决办法]
贴一段较齐全的分页给你参考下
<%@ 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 %>页 第<%=pageNo %>页 <a href="ShowArticleFlat.jsp?pageNo=<%=pageNo == 1 ? 1 : pageNo - 1 %>">上一页</a> <a href="ShowArticleFlat.jsp?pageNo=<%=1 %>">首页</a> <a href="ShowArticleFlat.jsp?pageNo=<%=pageNo - 5 > 0 ? pageNo - 5 : pageNo %>">前五页</a> <% 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'" : "" %>><%=j %></a> <% } %> <a href="ShowArticleFlat.jsp?pageNo=<%=pageNo + 5 < totalPages ? pageNo + 5 : totalPages %>">后五页</a> <a href="ShowArticleFlat.jsp?pageNo=<%=totalPages %>">末页</a> <a href="ShowArticleFlat.jsp?pageNo=<%=pageNo == totalPages ? pageNo : pageNo + 1 %>">下一页</a> <form name="form1" action="ShowArticleFlat.jsp"> <select name="pageNo" onchange="document.form1.submit()"> <% for(int i=1; i<=totalPages; i++) { %> <option value="<%=i%>" <%=(pageNo == i) ? "selected" : ""%>>第<%=i %>页</option> <% } %> </select> </form> <form name="form2" action="ShowArticleFlat.jsp"> <input type="text" size="4" value="<%=pageNo %>" name="pageNo"/> <input type="submit" value="go"/> </form> </body> </html>
[解决办法]
分页的目的是什么,为了控制页面的长度,还是说由于数据量比较大导致页面操作变得缓慢?实现方法有很多,要根据目的来选择方法。