Puzzled me a period of time page pagination, today specially sorted out my completed soon project. Below I want to share the page code of my project, the front and back end take all. Hope that the predecessors more advice.
One, the effect chart
Next I go to the front page and the management side of the partial pagination effect chart, they use a set of code.
Two, before the code some knowledge points
This jquery plug-in for Ajax paging Plug-ins, one-time load, so paging when there is no refresh and delay, if the large amount of data is not recommended this method, because the load will be slower.
Third, the Front code section
var pageSize = 6; How many records per page shows Var total;
Total number of records $ (function () {Init (0);//Note parameters, the initial page defaults to the background parameters, the first page is 0; $ ("#Pagination"). Pagination (total, {//total not less callback:pagecallback, Prev_text: ' Previous page ', Next_text: ' next page ', Items_
function Pagecallback (index, JQ) {//previous page index value that represents the paging you are currently clicking on, and the latter parameter represents the loading container.
Init (index);
}
});
function Init (pageIndex) {//This parameter is the number of pages indexed for the clicked page, the first page is 0, and the following is the Ajax pass value. $.ajax ({type: Post), URL: ".. /getcontentpaixuserv? cat= "+str+" &rows= "+pagesize+" &page= "+pageindex, Async:false, DataType:" JSON ", success:function (data) {$ ("
. Neirong "). empty (); /* total = data.total;
*/var array = data.rows;
for (Var i=0;i<array.length;i++) {var info=array[i]; if (info.refpic!= null) {$ (". Neirong"). Append (' <dl>
Four, the Backstage section (Java)
I'm using the MVC 3 layer model.
servlet section: (can be skipped)
public void DoPost (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
Gets the paging parameter
String p=request.getparameter ("page");//Current page (click Get)
int page=integer.parseint (p);
String row=request.getparameter ("Rows"); How many records are displayed on each page
int rows=integer.parseint (row);
String s=request.getparameter ("Cat"); Column ID
int indexid=integer.parseint (s);
Jsonobject object= (New Contentservice ()). Getcontentpaixubyid (IndexID, page, rows);
Out.print (object);
Out.flush ();
Out.close ();
}
Service section:(can skip)
Public jsonobject Getcontentpaixubyid (int indexid, int page, int rows) {Jsonarray array=new jsonarray ();
list<content>contentlist1= (New Contentdao ()). Selectindexbyid (INDEXID);
List<content>contentlist=paginationcontent (contentlist1,page,rows);
for (Content content:contentlist) {jsonobject object=new jsonobject ();
Object.put ("ContentID", Content.getcontentid ());
Object.put ("caption", Content.getcaption ());
Object.put ("CreateDate", Content.getcreatedate ());
Object.put ("Times", String.valueof (Content.gettimes ()));
Object.put ("Source", Content.getsource ());
Object.put ("Text", Content.gettext ());
Object.put ("Pic", Content.getpic ());
Object.put ("Refpic", Content.getrefpic ());
Object.put ("Hot", content.gethot ());
Object.put ("UserId", Content.getauthorid (). GetUserID ());
int id = Content.getauthorid (). GetUserID ();
String showname = (new UserService ()). Selectuserbyid (ID). getString ("ShowName");
Object.put ("ShowName", showname);
Array.add (object); } jsonobject OBj=new Jsonobject ();
Obj.put ("Total", contentlist1.size ());
Obj.put ("Rows", array);
return obj;
}
Get the starting and ending IDs for each page (this is the focus), also written in the service, for example, assuming a page has 6 content, then the first page ID is from 1 to 6, the second page ID is from 7 to 12, and so on
Gets how the content of each page starts from which ID to which ID ends.
private list<content> paginationcontent (list<content> list,int page,int rows) {
list<content >small=new arraylist<content> ();
int beginindex=rows*page; Rows is the number of items displayed per page, page is the index value of the pages of the page that I emphasized many times before, and the first page is 0, which is a good understanding below!
System.out.println (beginindex);
int endindex;
if (rows* (page+1) >list.size ()) {
endindex=list.size ();
}
else{
endindex=rows* (page+1);
}
for (int i=beginindex;i<endindex;i++) {
Small.add (List.get (i));
}
return small;
}
DAO Layer: (can be skipped)
Public List Selectindexbyid (int indexid) {list<content>list=new arraylist<content> ();
try{conn = Dbconn.getcon (); String sql = "SELECT * from t_content,t_user where T_content.authorid = T_user.userid and catlogid=?"
ORDER BY createdate Desc ";
pstm = conn.preparestatement (sql);
Pstm.setint (1, INDEXID);
rs = Pstm.executequery ();
SimpleDateFormat ff=new SimpleDateFormat ("yyyy mm monthly DD Day HH when mm points)";
while (Rs.next ()) {content content = new content ();
Content.setcontentid (Rs.getint ("ContentID"));
Content.setcaption (rs.getstring ("Caption"));
Content.setcreatedate (F.format) (Rs.gettimestamp ("CreateDate"));
Content.settimes (Rs.getint ("Times"));
Content.setsource (rs.getstring ("Source"));
Content.settext (rs.getstring ("Text"));
Content.setpic (rs.getstring ("Pic"));
Content.setrefpic (rs.getstring ("Refpic"));
Content.sethot (Rs.getint ("hot"));
User user = new user ();
User.setuserid (Rs.getint ("UserId"));
Content.setauthorid (user); Catlog catlog = new Catlog (); //cnturl to be developed Catlog.setcatlogid (Rs.getint ("Catlogid"));
Content.setcatlog (Catlog);
List.add (content);
}}catch (Exception e) {e.printstacktrace ();
}finally{Dbconn.closedb (conn, pstm, RS);
} return list;
}
Wonderful feature sharing:jquery Paging functionality operations JavaScript paging functionality
The above is the page of the implementation of the paging code, Easy-ui part of the page can also refer to the above code.