layui.use(['layer'], function () { let $ = layui.$; $.fn.pager = function (option, cb = null) { option = $.extend({ total: 100, currentPage: 1, pageListLength: 9, pageLimit: 20, }, option) option.currentPage = Number(option.currentPage) option.total = Number(option.total) option.pageListLength = Number(option.pageListLength) option.pageLimit = Number(option.pageLimit) let maxPage = 0; if (option.currentPage === 1) { maxPage = Math.ceil(option.total / option.pageLimit); } else { maxPage = Math.ceil((option.total + 4) / option.pageLimit); } let pageList = getPageList() let _this = $(this) let domId = _this.attr("id") if (!domId) { domId = Math.random().toString(36).slice(-8) _this.attr("id", domId) } _this.html(renderHtml(pageList)) _this.find('.page-num').one('click', function () { let page = $(this).attr('data-num') * 1 typeof cb === "function" && cb(page) }); _this.find('.prev-turn-page').one('click', function () { let page = option.currentPage - option.pageListLength if (page < 1) { page = 1; } typeof cb === "function" && cb(page) }) _this.find('.next-turn-page').one('click', function () { let page = option.currentPage + option.pageListLength if (page > maxPage) { page = maxPage } typeof cb === "function" && cb(page) }) _this.find('.prev-page').one('click', function () { typeof cb === "function" && cb(option.currentPage - 1) }) _this.find('.next-page').one('click', function () { typeof cb === "function" && cb(option.currentPage + 1) }) function getPageList() { let min, max; let middle = Math.floor(option.pageListLength / 2); min = option.currentPage - middle; max = option.currentPage + middle; if (min < 1) { max = max + Math.abs(min) + 1 min = 1; } if (max > maxPage) { min = maxPage - middle * 2 max = maxPage } if (maxPage < option.pageListLength) { min = 1 max = maxPage } let pageList = []; for (let i = min; i <= max; i++) { pageList.push(i) } return pageList } function renderHtml(pageList) { if (pageList.length === 0) { return ""; } let pageListHtml = `" return ` ` } } })