JavaScript ajax imitation Baidu paging function _javascript skills

Source: Internet
Author: User
Tags prev visibility
Copy Code code as follows:

/**
* Ajax Paging function
* Add <ul class= "pagination" where paging is required ></ol>
* As a paging component container element.
* PageCount Total Pages
* CurrentPage Current Page
* Container an OL container element with pagination class
* LoadData functions for loading data
* Version 1.0
*/
Pagination:function (PageCount, CurrentPage, container, LoadData) {
This.startpage = 1;
This.endpage = PageCount;
This.mindisplaypagecount = 5;
var c = $ (container);
var paginationlinks = "";
if (PageCount = = 1) {
C.css ({' Visibility ': ' Hidden '});
Return
}
if (PageCount > this.mindisplaypagecount + 1) {
if (Currentpage-this.mindisplaypagecount > 0) {
This.startpage = Currentpage-this.mindisplaypagecount;
}
if ((CurrentPage + this.mindisplaypagecount-1) < PageCount) {
This.endpage = CurrentPage + this.mindisplaypagecount-1;
} else {
This.endpage = PageCount;
}
}
Paginationlinks = "<ul>";
if (currentpage!= 1) {
Paginationlinks + + <li><a id= ' prevpage ' href= ' javascript:; ' rel= ' prev ' > Prev </a></li> ';
}
for (var i = this.startpage i <= this.endpage; i++) {
if (currentpage = = i) {
Paginationlinks + = "<li id= ' Page_" + currentpage + "_container ' ><a id= ' Page_" + i + "' class= ' current ' href= ' Java Script:; ' > "+ currentpage + </a></li>";
} else {
Paginationlinks + + "<li id= ' Page_" + i + "_container ' ><a id= ' Page_" + i + "' href= ' javascript:; ' > "+ i +" </a></li> ";
}
}
if (CurrentPage < PageCount) {
Paginationlinks + + <li><a id= ' nextpage ' href= ' javascript:; ' rel= ' Next ' > Next ' </a></li> ';
}
Paginationlinks = "</ul>";
C.html (paginationlinks);
var links = $ ("#page_number ul li a");
Links.each (function (index) {
if (!) ( this.innerhtml = "Prev" | | this.innerhtml = "Next page") {
$ (this). Click (Function (event) {
alert (links[index].innerhtml);
LoadData (Curtaskid, "", "", parseint (links[index].innerhtml));
Pagination (PageCount, parseint (links[index].innerhtml), container, loaddata);
});
}
});
var prevpage = $ ("#prevpage");
var nextPage = $ ("#nextpage");
C.css ({' Visibility ': ' Visible '});
if (prevpage) {
Prevpage.click (function (event) {
LoadData (Curtaskid, "", "", currentPage-1);
Pagination (PageCount, currentPage-1, container, loaddata);
});
}
if (nextPage) {
Nextpage.click (function (event) {
LoadData (Curtaskid, "", "", CurrentPage + 1);
Pagination (PageCount, currentpage + 1, container, loaddata);
});
}
}

LoadData is the function that loads the data, this function needs to define a parameter for the current page number, such as:
Copy Code code as follows:

var currentpage = 1;
Loadexamlist (currentpage) {
Todo
Pagination (5,currentpage,$ (UL), loadexamlist);
};

5 is the total number of pages, 1 is the current page, $ (UL) is to place the page numbers button, Loadexamlist is clicked on the previous page, next page or page number when the function called.
Related Article

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.