Full Jquery-easyui display of pagination data examples

Source: Internet
Author: User
Tags code tag

Although said to be the introductory example, but I try to do the detailed point, will continue to update, part of the function

In addition, in order to follow the easyui of learning, made a simple framework: backstage using HIBERNATE2.5+STRUTS2 development: Database with MySQL. Red Code tag Note.

Data for the model layer

Student class

Package Org.easyui.model;

Import Java.util.Date;

public class Student {
private int id;
private int age;
private String name;
Private Date birthday;
Private String ClassName;
private char sex;

Public Char getsex () {
return sex;
}
public void Setsex (char sex) {
This.sex = sex;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public Date Getbirthday () {
return birthday;
}
public void Setbirthday (Date birthday) {
This.birthday = Birthday;
}
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
Public String GetName () {
return name;
}
Public String GetClassName () {
return className;
}
public void SetName (String name) {
THIS.name = name;
}
public void Setclassname (String className) {
This.classname = ClassName;
}

}

DAO database Access Layer

Interface Easydao:

Package Org.easyui.dao;

Import java.util.List;

Import org.easyui.model.Student;

Public interface Easydao {
Public list<student> getstudent (int page,int rows);
public int gettotalpages (int rows);
}

Interface Implementation Class Easydaoimpl:

Package Org.easyui.dao;

Import java.util.List;

Import org.easyui.model.Student;
Import Org.easyui.util.UtilHibernate;
Import org.hibernate.HibernateException;
Import org.hibernate.Session;

public class Easydaoimpl implements Easydao {

  @SuppressWarnings ("unchecked")
 public list<student> getstudent (int page, int rows) {
  List<Student> List = null;
  session Session = Utilhibernate.getsession ();
  try {
   session.begintransaction ();
   string sql = "from Student";
   list = session.createquery (sql)
        . Setfirstresult ((page-1) *rows)
        .setmaxresults (rows)
        .list ();
   session.gettransaction (). commit ();
  } catch (Hibernateexception e) {
   session.gettransaction (). rollback ();
   e.printstacktrace ();
  }finally{
   utilhibernate.closesession (session);
  }
  
  return list;
 }

public int gettotalpages () {
Session session = Utilhibernate.getsession ();
int total = 0;
try {
Session.begintransaction ();
String sql = "SELECT COUNT (*) from Student";
int count = (Integer) session.createquery (SQL). Uniqueresult ();
Total =count;
Session.gettransaction (). commit ();
} catch (Hibernateexception e) {
Session.gettransaction (). rollback ();
E.printstacktrace ();
}finally{
Utilhibernate.closesession (session);
}
return total;
}

}

Action Layer:

The EasyuiAction1 code is as follows

Package Org.easyui;

Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;

Import Javax.servlet.http.HttpServletRequest;

Import Org.apache.struts2.interceptor.ServletRequestAware;
Import Org.easyui.dao.EasyDao;
Import Org.easyui.dao.EasyDaoImpl;
Import org.easyui.model.Student;

Import Com.opensymphony.xwork2.ActionSupport;

Public class EasyuiAction1 extends Actionsupport implements servletrequestaware{
 private static final long Serialversionuid = 1L;
 private  httpservletrequest request;
 private int total;
 private list<object> rows;
 
 public int gettotal () {
  return total;
 }
 public list<object> getRows () {
  return rows;
 }
 public void settotal (int total) {
  this.total = total;
 }
 public void Setrows (list<object> rows) {
  this.rows = rows;
 }
 
  @Override
 public String Execute () throws Exception {
  int page = Integer.parseint (Request.getparameter ("page"));
  int row = Integer.parseint (Request.getparameter ("Rows"));//Accept Parameters page and rows
  easydao DAO = new Easydaoimpl ();    //instantiation dao
  this.total = Dao.gettotalpages ();

This.rows = new arraylist<object> ();
list<student> list = dao.getstudent (page, row);//paging, saving data to list

for (int i=0;i<list.size (); i++) {
Student Student = List.get (i); The most primitive loop method to the Student object
map<string,object> map = new hashmap<string,object> ();
Map.put ("id", Student.getid ()); Save as key-value pairs in map
Map.put ("Sex", student.getsex ());
Map.put ("Name", Student.getname ());
Map.put ("Age", Student.getage ());
Map.put ("Birthday", Student.getbirthday ());
Map.put ("ClassName", Student.getclassname ());
This.rows.add (map); Loop save Map to list Object This.rows
}
System.out.println (Request.getparameter ("page"));
System.out.println (Request.getparameter ("Rows"));
return SUCCESS;
}
public void Setservletrequest (HttpServletRequest request) {
This.request = Request;
}
}

The Struts.xml configuration is as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd ">
<struts>
<package name= "Jsondemo" extends= "Json-default" >
<action name= "easyaction" class= "Org.easyui.EasyuiAction1" >
<result type= "json"/>
</action>
</package>
</struts>

The easydemo1.jsp page code is as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base href= "<%=basePath%>" >

<title>easyDemo1.jsp</title>
<link rel= "stylesheet" type= "Text/css" href= "Jquery-easyui/themes/default/easyui.css" >
<link rel= "stylesheet" type= "Text/css" href= "Jquery-easyui/themes/icon.css" >
<script type= "Text/javascript" src= "Jquery-easyui/jquery-1.4.2.min.js" ></script>
<script type= "Text/javascript" src= "Jquery-easyui/jquery.easyui.min.js" ></script>

<script type= "Text/javascript" src= "Jslib/easydemo.js" ></script>
<script type= "Text/javascript" >
$ (function () {
$ (' #tt '). DataGrid ({
Title: ' Small example of the DataGrid ',
Iconcls: ' Icon-ok ',
WIDTH:500,
height:320,
Pagesize:5,
PAGELIST:[5,10,15,20],
Nowrap:false,
Striped:true,
Collapsible:true,
URL: ' Easyaction.action ',
LOADMSG: ' Data loading ... ',
Sortname: ' Code ',
SortOrder: ' Desc ',
Remotesort:false,
frozencolumns:[[
{field: ' CK ', checkbox:true}
]],
columns:[[
{title: ' Study Number ', field: ' ID ', Width: ' + ', rowspan:2,align: ' Center '},
{title: ' Name ', field: ' Name ', Width: ' $ ', rowspan:2,align: ' Center '},
{title: ' Sex ', field: ' Sex ', Width: ' + ', rowspan:2,align: ' Center '},
{title: ' Ages ', field: ' Age ', Width: ' + ', rowspan:2,align: ' Center '},
{title: ' Date of birth ', field: ' Birthday ', Width: ' + ', rowspan:2,align: ' Center '},
{title: ' Class ', field: ' ClassName ', Width: ' + ', rowspan:2,align: ' Center '}
]],
Pagination:true,
Rownumbers:true

});
$ (' #tt '). DataGrid (' Getpager '). Pagination ({
Displaymsg: ' current display from {-} to {-} total} Records ',
Onbeforerefresh:function (pagenumber, pageSize) {
$ (this). Pagination (' loading ');
Alert (' pagenumber: ' +pagenumber+ ', pageSize: ' +pagesize ');
$ (this). Pagination (' loaded ');
},
});

$ (' #tt '). DataGrid ({url: ' easyaction.action '});
});
</script>

<body>
<a href= "javascript:void (0)" onclick= "Verify ()" Class= "Easyui-linkbutton" > Test josn Data </a>
<table id= "tt" >
</table>
</body>

The Easydemo.js code is as follows:

var xmlhttp;
function Verify () {
Create a XMLHttpRequest Object
if (window. XMLHttpRequest) {
XMLHTTP = new XMLHttpRequest ();
if (Xmlhttp.overridemimetype) {
Xmlhttp.overridemimetype ("Text/xml");
}
}else if (window. ActiveXObject) {
var activerxname = ["MSXML2. XMLHTTP "," Microsoft.XMLHTTP "];
for (Var i=0;i<activerxname.length;i++) {
try{
XMLHTTP = new ActiveXObject (Activerxname[i]);
Break
}catch (e) {
}
}
}
Confirm that the XMLHttpRequest object is created successfully
if (!xmlhttp) {
Alert ("XMLHttpRequest object creation failed");
Return
}else{
}
Xmlhttp.onreadystatechange = callback;
Xmlhttp.open ("POST", "Easyaction.action?page=1&rows=5", "true");
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.send ();
}
function callback () {
if (xmlhttp.readystate = = 4) {
if (Xmlhttp.status = = 200) {
var obook = Eval_r (' (' + xmlhttp.responsetext + ') ');
$.messager.alert (' Test Jsondata ', xmlhttp.responsetext);

Full Jquery-easyui display of pagination data examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.