玩轉Web之easyui(一)-----easy ui datagird 分頁

easy ui 中數據表格的分頁實際上是很簡單的,分頁是在數據表格能夠正常顯示數據的基礎上進行的,在這裏給出servlet的代碼,其中selectAll()方法是從數據庫中提取全部數據,

分頁的一種思路是:從數據表中取出全部數據,而後根據分頁的要求取出對應的數據。html

package thejavabean;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

public class Table_info extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		try {
			response.setContentType("text/html");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			String page = request.getParameter("page"); // 當前頁數
			String rows = request.getParameter("rows"); // 每頁顯示行數  
			
			List<Student> allList=selectAll();   //獲取全部數據
			int p=Integer.parseInt(page);    
			int r=Integer.parseInt(rows);
			int begin=(p-1)*r;  //當前頁開始項
			int num=begin;
			int count=r;
			List<Student> list=new ArrayList();
			System.out.println();
			while(count>0&&num<allList.size()){
				list.add(allList.get(num)); 
				num++;
				count--;
			}
			int total=selectAll().size();
			String json = "{\"total\":"+total+" , \"rows\":"+JSONArray.fromObject(list).toString()+"}";
		    
			response.getWriter().write(json.toString());
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
    public List<Student> selectAll() throws SQLException{
    	StudentManage sm=new StudentManage();
		return sm.selectAll("");
    	
    }
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
		
	}

}
相關文章
相關標籤/搜索