Home  >  Article  >  Web Front-end  >  Javascript paging code example sharing (js paging)_javascript skills

Javascript paging code example sharing (js paging)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:09:151250browse

Call:

Copy code The code is as follows:

var pageChange = function (index) {
         html = pager("divid", index, 5, 1000, pageChange, { showGoTo: false, showFirst: false });
    }

Implementation:

Copy code The code is as follows:

pager = function (divPager, pageIndex, pageSize, totalCount, pageChange, opt) {

var theOpt = {
barSize: 5, //The number of pages displayed in the paging bar
barTemplate: "{bar} total {totalPage} pages {totalCount} items {goto}", //Show template
autoHide: true, //Whether to automatically hide
showFirst: true, //In totalPage>barSize Whether to automatically display the first page link
showLast: true, // Whether to automatically display the last page link when totalPage>barSize
showGoTo: true, // Whether to display GoTo
autoHideGoTo: true / /If there are too few, whether to automatically hide GoTo
};

if (opt) {
if (opt.barSize)
theOpt.barSize = opt.barSize; .barTemplate)
theOpt.barTemplate = opt.barTemplate;
if (opt.autoHide == false)
theOpt.autoHide = false;
if (opt.showFirst == false)
theOpt.showFirst = false;
if (opt.showLast = false)
theOpt.showLast = false;
if (opt.showGoTo == false)
theOpt.showGoTo = false;
if (opt.autoHideGoTo == false)
                  theOpt.autoHideGoTo = false; ));

if (!myPagerChanges[divPager]) myPagerChanges[divPager] = pageChange;

var startPage = 0; //Paging bar start page
var endPage = 0; //Paging bar termination page
var showFirst = true;
var showLast = true;


if (isNaN(pageIndex)) {
pageIndex = 1;
}
pageIndex = parseInt(pageIndex);
if (pageIndex <= 0)
pageIndex = 1;
if (pageIndex * pageSize > totalCount) {
pageIndex = Math.ceil( totalCount / pageSize);
}

if (totalCount == 0) { //If there is no data
document.getElementById(divPager).innerHTML = "";
return "";
}

var totalPage = Math.ceil(totalCount / pageSize);
if (theOpt.autoHide && totalCount <= pageSize) { //Auto-hide
document.getElementById(divPager ).innerHTML = "";
                                                                                             Page ;
          theOpt.showLast = theOpt.showFirst = false;
           
else {
if (pageIndex <= Math.ceil(theOpt.barSize / 2)) { //The first few pages
startPage = 1;
endPage = theOpt.barSize;
theOpt.showFirst = false;
}
else if (pageIndex > (totalPage - theOpt.barSize / 2)) { //The last few pages
startPage = totalPage - theOpt.barSize 1;
endPage = totalPage;
theOpt.showLast = false;
}
else {                                                                                                                                  🎜>         endPage = pageIndex Math.floor(theOpt.barSize / 2);
                                                                                                                                          Last = theOpt.showFirst = false ;
}
}

function _getLink(index, txt) {
                                             style='margin: 2px 5px;border: 1px solid #6d8cad;color: #0269BA;padding: 2px 5px;text-decoration: none;' onclick='myPagerChanges["" divPager ""](" index ")'> ;" txt "";
}

var barHtml = ""; //Page bar
barHtml = pageIndex == 1 ? "" : _getLink(pageIndex - 1, "Previous page");
if (theOpt.showFirst) {
barHtml = _getLink(1) "...";
}
for ( var index = startPage; index <= endPage; index ) {

if (index == pageIndex) {
barHtml = "" index "";
                                                        if (theOpt.showLast) {
barHtml = "..." _getLink(totalPage);
}
barHtml = pageIndex == totalPage ? "" : _getLink(pageIndex 1, "Next page ");

var gotoHtml = ""; //goto box and button
if (theOpt.showGoTo && theOpt.barTemplate.indexOf("{goto}") > 0) {
if ((theOpt.autoHideGoTo && totalPage > 15) || theOpt.autoHideGoTo == false) {
var txtid = divPager "_goIndex";
var indexVal = "document.getElementById("" txtid " ") .value";
gotoHtml = "";
             gotoHtml = " ";
         }
     }

     //替换模板
     var pagerHtml = theOpt.barTemplate.replace("{bar}", barHtml)
                               .replace("{totalCount}", totalCount)
                               .replace("{pageIndex}", pageIndex)
                               .replace("{totalPage}", totalPage)
                               .replace("{goto}", gotoHtml);

     document.getElementById(divPager).innerHTML = pagerHtml;
     return pagerHtml;
 };
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn