A simple pagination control based on jquery _jquery

Source: Internet
Author: User
Tags extend prev

1: Effect Chart

2: Material

3: Coding
3.1 Thinking
What do you need to do?
1: Paging control needs to send the request to the background, send parameters including the current page, the number of each page display, query conditions; and get the data loaded to the current page;
2: To modify the deletion of the time to remember the current page;
3: The query after the page can remember the current query conditions

3.2 implementation
Html

Copy Code code as follows:

<!--containers for storing data-->
<div class= "Tabledata" >
</div>
<!--paging control display-->
<div class= "Pagebar" ></div>

Jquery
For our control to be free to use, we will write it as a plug-in form, first of all, we have a framework, we named the plug-in SimplePage
Copy Code code as follows:

(function ($) {
$.fn.simplepage=function (o) {
var options={
Configuration parameters
};
Return//sth
}
}) (JQuery)

What are the default parameters?
Because of the need to send the current page, the number of each page display, so need to currentpage,pagesize two basic parameters;
Because you need to query the table content, you need a form to place the query criteria;
Because you need to modify the deletion and remember the current page, you need a flag to indicate what type of action is currently in progress;
To make our program more flexible, plus the container to be loaded after getting the data, there is the pager that the paging control loads,
Specifically as follows
Copy Code code as follows:

var options={
Pager: '. Pager ',//container for table controls
Container: '. Tabledata ',//container for placing tabular data
Form: ' #form ',//forms for placing query criteria
Pageform: ' #pageForm ',//placing the hidden div
URL: ',//send the requested address
Currentpage:1,
Pagesize:2
type:null,//Optional: Action,
Pageshow:7
}

For ease of maintenance, we declare a separate object to get the data, bind the event, and we'll name the function $.page
Copy Code code as follows:

$.page = {
//
Setpage:function (o) {
},
Get current Page
Getcurrentpage:function (o) {
},
Get the number of display per page
Getpagesize:function (o) {
},
Generate the JSON data needed to send
Gendata:function (o) {
},
Send data
Loaddata:function (o) {
}
}

To implement the functions declared above, when the page is first loaded, we need to get the total number of pages from the server to the next page control, so first implement the LoadData function
Copy Code code as follows:

Loaddata:function (o) {
var that = this;
var data = That.gendata (o);
$.ajax ({
Url:o.url,
Data:data,
Type: ' Post ',
DataType: ' HTML ',
Cache:false,
Success:function (Result) {
var res = $ (Result). Find (' tbody '). html ();
var totalpage = $ (Result). Find (' #totalPage '). Val ();
var currentpage = $ (Result). Find (' #currentPage '). Val ();
O.currentpage=currentpage;
O.pager.empty ();
$.line.setline (Totalpage,o); function to invoke a raw page control
},
Error:function () {
Alert ("Error");
}
})
}

Below we implement the function of the raw page control above $.line.setline
Copy Code code as follows:

$.line={
Setline:function (totalpage,o) {
for (Var i=0;i<totalpage;i++) {
var a=$ (' <a/> '). html (' <span> ' + (i+1) + ' </span> '). addclass (' PageA '). Bind (' click ', function () {
var s=$ (this);
S.siblings (). Removeclass (' pageactive ');
S.addclass (' pageactive ');
O.currentpage=s.text ();
$.page.loaddata (o);
});
if (o.currentpage==i+1) {
A.addclass (' pageactive ');
}
O.pager.append (a);
}
var limit=this.getlimit (o,totalpage);
var apage=o.pager.find (' A.pagea '). Not (' A.previous,a.nextall,a.record ');
Apage.hide ();
Apage.slice (Limit.start,limit.end). Show ();
var prev=$ (' <a/> '). html (' <span> prev </span> '). AddClass (' PageA previous '). Unbind (' click '). Bind (' Click ', function () {
var pageactive=o.pager.find (' a.pageactive ');
var s=$ (this);
if (Pageactive.prev (). Text () = ' prev ') {
Alert (' Already the first page! ');
return false;
}
Pageactive.removeclass (' pageactive ');
Pageactive.prev (). addclass (' pageactive ');
O.currentpage=pageactive.prev (). text ();
$.page.loaddata (o);
});
var next=$ (' <a/> '). html (' <span> next page </span> '). addclass (' PageA nextall '). Unbind (' click '). Bind (' Click ', function () {
var pageactive=o.pager.find (' a.pageactive ');
var s=$ (this);
if (Pageactive.next (). Text () = ' next page ') {
Alert (' Already the last page! ');
return false;
}
Pageactive.removeclass (' pageactive ');
Pageactive.next (). addclass (' pageactive ');
O.currentpage=pageactive.next (). text ();
$.page.loaddata (o);
});
var pageactivetext=o.pager.find (' a.pageactive '). Text ();
var record=$ (' <a/> '). html (' <span> ' +pageactivetext+ '/' +totalpage+ ' </span> '). addclass (' PageA Record ");
O.pager.prepend (prev). prepend (Record). append (next);
}
}

In the above code we add the Pageactive class to the current page, so now we can implement the $.page getcurrentpage function, very simple
Copy Code code as follows:

Getcurrentpage:function (o) {
var p = o.pager.find ("a.pageactive"). Text ();
return p;
}

Then we implement the Gendata function that generates the JSON data, and the JSON format is {Key:value,key:value}
Copy Code code as follows:

Gendata:function (o) {
var sdata = $.extend ({}, {"CurrentPage": O.currentpage,
"PageSize": O.pagesize}, $.jsonobj (O.pageform));
return sdata;
},

The above $.jsonobj is a custom function, in order to generate the JSON format we need to send the query data, only support Input,select
Copy Code code as follows:

$.jsonobj = function (form) {
To determine whether there is a serialized Dongdong
if (!$ (form). HTML () | | $ (form). HTML () = NULL | | $.trim ($ (form). HTML ()) = = "") {
return null;
}
var Formel = $ (form). Find (' input[type= "text"]);
var formselect = $ (form). Find (' select ');
var json = "{";
for (var i = 0; i < formel.length-1; i++) {
var name = Formel.eq (i). attr (' name ');
var val = "'" + formel.eq (i). Val () + "'";
JSON + name;
JSON + = ":";
JSON = val;
JSON + + ",";
}
var lname = Formel.eq (formel.length-1). attr (' name ');
var lval = "'" + formel.eq (formel.length-1). Val () + "'";
JSON + lname;
JSON + = ":";
JSON + lval;
if (formselect) {
JSON + + ",";
for (var i = 0; i < formselect.length-1; i++) {
var name = Formselect.eq (i). attr (' name ');
var val = "'" + formselect.eq (i). Val () + "'";
JSON + name;
JSON + = ":";
JSON = val;
JSON + + ",";
}
var lname = Formselect.eq (formselect.length-1). attr (' name ');
var lval = "'" + formselect.eq (formselect.length-1). Val () + "'";
JSON + lname;
JSON + = ":";
JSON + lval;
}
JSON + = "}";
var jsonobj = eval ("+ JSON +")
return jsonobj;
}

Then we bind the event to the button of the query form, and we extend our $.page function
Copy Code code as follows:

Handlequeryline:function (o) {
$ (o.form). Find (". Query"). Click (function () {
$ (o.pageform). Append ($ (o.form). Clone (True);
$ (o.pageform). empty ();
$ (o.form). Find (' input[type= "text"]). each (function () {
var Vals = $ (this). Val ();
var s = $ (this). Clone (). Val (Vals);
$ (o.pageform). append (s);
});
$ (o.form). Find (' select '). each (function () {
var Vals = $ (this). Val ();
var s = $ (this). Clone (). Val (Vals);
$ (o.pageform). append (s);
});
$.page.query (o);
});
}

OK, the basic function has been completed, the following completes the main function
Copy Code code as follows:

$.fn.simplepage = function (OS) {
var options = {
Pager: '. Pager ',//container for table controls
Container: '. Tabledata ',//container for placing tabular data
Form: ' #form ',//forms for placing query criteria
Pageform: ' #pageForm ',//placing the hidden div
URL: ',//send the requested address
Currentpage:1,
Pagesize:2,
type:null,//Optional: Action,
pageshow:7//,
};
var o = $.extend (options, OS);
Return This.each (function () {
O.pager = $ (this). Find (O.pager);
O.container = $ (this). Find (O.container);
First clear the Click event
O.pager.unbind (' click ');
if (O.type = = ' action ') {
The specified action, such as an event when deleted, requires refreshing the data on the current page
O.currentpage = $.page.getpagesize (o);
O.pagesize = $.page.getcurrentpage (o);
$.page.loaddata (o);
Return
}
$.page.loaddata (o);
$.line.handlequeryline (o);
})
}

Now our paging is not very good-looking, we use Firebug to view the resulting paging structure, wrote the following style
Copy Code code as follows:

. Pager a {
Display:block;
Float:left;
width:16px;
height:16px;
margin:5px;
}
. Pager a.pagea{
Background:url (".. /images/grid/page.png ") no-repeat left 0px transparent;
Display:inline-block;
font-size:14px;
margin:0 3px;
padding-left:6px;
Text-align:center;
Vertical-align:bottom;
Height:auto;
Width:auto;
Cursor:pointer;
}
. Pager A.pagea span{
Background:url (".. /images/grid/page.png ") No-repeat right 0px transparent;
Display:inline-block;
height:24px;
line-height:22px;
padding-right:6px;
}
. Pager a.pageactive{
Background:url (".. /images/grid/page.png ") no-repeat left-24px Transparent;
}

Done!!

DEMO Download

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.