[jQuery]EasyUI中DataGrid获取数据并分页

首先是页面代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>表格信息</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="js/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="js/themes/icon.css">

    <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
    <script type="text/javascript" src="js/jquery.easyui.min.js"></script>
    
    <script type="text/javascript">
        $(function(){
            $("#myTable").datagrid({
                title: "表单数据",
                width: 550,
                height: 260,
                collapsible: true,
                url: "GetDataGridServlet",
                method: 'POST',
                sortName: 'title',
                loadMsg: "数据加载中...",
                pageSize: 5,
                pageList : [5, 10],
                pagination:true,
                striped: true,
                
                columns:[[
                    {title: '姓名', field: 'name', width: 100, align: 'center'},
                    {title: '性别', field: 'sex', width: 50, align: 'center'},
                    {title: '年龄', field: 'age', width: 50, align: 'center'},
                    {title: '出生日期', field: 'birthday', width: 200, align: 'center'}
                ]]
            });
            
            var p = $('#myTable').datagrid('getPager');  
                $(p).pagination({  
                beforePageText: '',//页数文本框前显示的汉字  
                afterPageText: '页    共 {pages} 页',  
                displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
            });  
        });
    </script>

  </head>
  
  <body>
        <div style="padding: 30px">
            <table id="myTable"></table>
        </div>
        
  </body>
</html>

Servlet 

public class GetDataGridServlet extends HttpServlet {
    
    private EasyUIDao dao = new EasyUIDaoImpl();

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int pageSize = Integer.parseInt(request.getParameter("page"));
        int rows = Integer.parseInt(request.getParameter("rows"));
        System.out.println("pageSize: " + pageSize);
        System.out.println("rows: " + rows);
        int total = dao.getAllStudentsCounts();
        ArrayList<Student> data = dao.getAllStudents4DataGrid((pageSize - 1) * rows + 1 , pageSize * rows);
        String json = EasyUIUtil.stringToJSON(total, data);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }

}

jdbc实现数据获取

package com.wisher.easyui.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.wisher.db.DBConnection;
import com.wisher.easyui.bean.Student;
import com.wisher.easyui.dao.EasyUIDao;

public class EasyUIDaoImpl implements EasyUIDao {
    
    private Connection conn;
    private PreparedStatement pstmt;
    private ResultSet rs;

    public ArrayList<Student> getAllStudents4DataGrid(int start, int end) {
        ArrayList<Student> list = new ArrayList<Student>();
        
        StringBuffer sql = new StringBuffer("SELECT * FROM ");
        sql.append("(SELECT A.*, ROWNUM RN FROM (SELECT * FROM TB_EASYUI_STUDENT) A WHERE ROWNUM <= ?) ");
        sql.append("WHERE RN >= ?");
        
        try {
            conn = DBConnection.getConnectionInstance();
            pstmt = conn.prepareStatement(sql.toString());
            pstmt.setInt(1, end);
            pstmt.setInt(2, start);
            
            rs = pstmt.executeQuery();
            
            while(rs.next()) {
                Student student = new Student();
                student.setName(rs.getString("NAME"));
                student.setSex(rs.getString("SEX"));
                student.setAge(rs.getInt("AGE"));
                student.setBirthday(rs.getDate("BIRTHDAY"));
                list.add(student);
            }
        } catch (SQLException e) {
            list = null;
            e.printStackTrace();
        } finally {
            if(pstmt != null) {
                try {
                    pstmt.close();
                    pstmt = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if(rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        return list;
    }
    
    public int getAllStudentsCounts() {
        int total = 0;
        
        StringBuffer sql = new StringBuffer("SELECT COUNT(*) FROM TB_EASYUI_STUDENT");
        
        try {
            conn = DBConnection.getConnectionInstance();
            pstmt = conn.prepareStatement(sql.toString());
            rs = pstmt.executeQuery();
            
            if(rs.next()) {
                total = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(pstmt != null) {
                try {
                    pstmt.close();
                    pstmt = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if(rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        return total;
    }
    
}

String2JSON转换

public class EasyUIUtil {
    
    public static String stringToJSON(int total, ArrayList<Student> list) {
        StringBuffer sb = new StringBuffer();
        sb.append("{\"total\":").append(total).append(",");
        sb.append("\"rows\":[");
        
        for(int i=0; i<list.size(); i++) {
            Student stu = list.get(i);
            sb.append("{");
            sb.append("\"name\":").append("\"").append(stu.getName()).append("\",");
            sb.append("\"sex\":").append("\"").append(stu.getSex()).append("\",");;
            sb.append("\"age\":").append("\"").append(stu.getAge()).append("\",");;
            sb.append("\"birthday\":").append("\"").append(stu.getBirthday()).append("\"},");;
        }
        
        sb.delete(sb.length() - 1, sb.length());
        sb.append("]}");
        
        return sb.toString();
    }

}

效果图

转载于:https://www.cnblogs.com/wisher/p/3648502.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值