Paging functions implemented by Javascript

Source: Internet
Author: User

From: IECN. Net; Author: Clock

/**
* Paging Class Construction
* Parameter nTotalList: Total number of entries
* Parameter nPageSize: number of entries displayed per page
* Parameter nPageNum: Current page number
* Parameter sPageUrl: the URL of the paging link. The page number is replaced by [pn], and the actual page number is replaced when output.
* Parameter nPageListSize: Maximum number of pages displayed in the page number list (drop-down box. This parameter can be omitted. The default value is 100.
*/
Function Pagination (nTotalList, nPageSize, nPageNum, sPageUrl, nPageListSize ){
This. totalList = nTotalList;
This. pageSize = nPageSize;
This. pageNum = nPageNum;
If (nTotalList = 0)
This. totalPages = 1;
Else
This. totalPages = Math. floor (this. totalList-1)/this. pageSize + 1 );
This. pageUrl = sPageUrl;
If (arguments [4])
This. pageListSize = nPageListSize;
Else
This. pageListSize = 100;
}

/**
* Generate pages and output HTML directly.
* No Parameter
* No Return Value
*/
Pagination. prototype. generate = function (){
Var output = "";
Output ++ = "<table width = \" 98% \ "cellspacing = \" 1 \ "cellpadding = \" 3 \ "align = \" center \ "> <tr> <td align = \ "right \"> ";
Output + = "Total" + this. totalList + "entries per page" + this. pageSize + "entries current ";
Output + = "<select onchange = \" if (this. value) location. href = '"+ this. pageUrl + "'. replace (// \ [pn \]/, ";
Output + = "this. value); \" align = \ "absMiddle \" style = \ "font: normal 9px Verdana, Arial,; \"> ";
Var firstPage = this. pageNum-Math. floor (this. pageListSize/2 );
If (firstPage <1)
FirstPage = 1;
Var lastPage = firstPage + this. pageListSize-1;
If (lastPage> this. totalPages ){
LastPage = this. totalPages;
FirstPage = lastPage-this. pageListSize + 1;
If (firstPage <1)
FirstPage = 1;
}
If (firstPage> 1 ){
Output + = "<option value = \" 1 \ "> 1 </option> ";
If (firstPage> 2)
Output + = "<option value = \" \ ">... </Option> ";
}
For (var p = firstPage; p <= lastPage; p ++ ){
Output + = "<option value = \" "+ p + "\"";
If (p = this. pageNum)
Output + = "selected = \" yes \"";
Output + = ">" + p + "</option> ";
}
If (lastPage <this. totalPages ){
If (lastPage <this. totalPages-1)
Output + = "<option value = \" \ ">... </Option> ";
Output + = "<option value = \" "+ this. totalPages +" \ ">" + this. totalPages + "</option> ";
}
If (this. pageNum> this. totalPages)
Output + = "<option value = \" \ "selected = \" yes \ "> page number out of range </option> ";
Output + = "</select> ";
Output + = "/" + this. totalPages + "page ";
If (this. pageNum = 1 ){
Output + = "[homepage]";
Output + = "[Previous Page]";
}
Else {
Output + = "<a href = \" "+ this. pageUrl. replace (/\ [pn \]/, "1") + "\"> [homepage] </a> ";
Output + = "<a href = \" "+ this. pageUrl. replace (/\ [pn \]/, this. pageNum-1) + "\"> [Previous Page] </a> ";
}
If (this. pageNum = this. totalPages ){
Output + = "[Next Page]";
Output + = "[last page]";
}
Else {
Output + = "<a href = \" "+ this. pageUrl. replace (/\ [pn \]/, this. pageNum + 1) + "\"> [Next Page] </a> ";
Output + = "<a href = \" "+ this. pageUrl. replace (/\ [pn \]/, this. totalPages) + "\"> [last page] </a> ";
}
Output + = "</td> </tr> </table> ";
Document. writeln (output );
}

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.