/* create by yangjinhua data:2016-11-04 */ define(function(require, exports, module) { require('./pagination.css') var tpl = require('./pagination.tpl') function Pagination(args) { this.$container = args.container; this.$container.append(tpl); this.actions = args.actions; this.pageSize = args.pageSize; this.totalRows = args.totalRows; this.pageIndex = 1; this.pageCount = Math.ceil(args.totalRows / args.pageSize); this.init(); this.addEvents(); } Pagination.prototype.init = function() { var self = this; var container = this.$container.find('.fdpr-pagination-container'); //初始化页码 self.setRefreshPages(0, self.pageSize, self.totalRows) //首页 var $btnFirstPage = container.find('.edp-first'); $btnFirstPage.on('click', function() { if ($(this).hasClass('pagi-button-disabled')) return; if (self.actions.click && typeof self.actions.click == 'function') { self.pageIndex = 1; self.actions.click('firstPage', self.pageIndex - 1); } }); //上一页 var $btnPrevPage = container.find('.edp-previous'); $btnPrevPage.on('click', function() { if ($(this).hasClass('pagi-button-disabled')) return; if (self.actions.click && typeof self.actions.click == 'function') { self.pageIndex = parseInt(self.pageIndex - 1); self.actions.click('prevPage', self.pageIndex - 1); } }); //下一页 var $btnNextPage = container.find('.edp-next'); $btnNextPage.on('click', function() { if ($(this).hasClass('pagi-button-disabled')) return; if (self.actions.click && typeof self.actions.click == 'function') { self.pageIndex = parseInt(self.pageIndex + 1); self.actions.click('nextPage', self.pageIndex - 1); } }); //末页 var $btnLastPage = container.find('.edp-last'); $btnLastPage.on('click', function() { if ($(this).hasClass('pagi-button-disabled')) return; if (self.actions.click && typeof self.actions.click == 'function') { self.pageIndex = parseInt(self.pageCount); self.actions.click('lastPage', self.pageCount - 1); } }); //跳转 var $btnLinkPage = container.find('.edp-link'); $btnLinkPage.on('click', function() { if ($(this).hasClass('pagi-button-disabled')) return; var linkPage = parseInt(container.find('.edp-pagination').val()); if ( isNaN(linkPage) ) { ER.messager.err('必须输入数字!'); container.find('.edp-pagination').val(''); return; } if (linkPage === self.pageIndex) return; if (linkPage === NaN || container.find('.edp-pagination').val() === '') { ER.messager.tip('跳转页码不能为空 ,请输入。'); return; } if (linkPage < 1) { ER.messager.tip('跳转页码不能小于1 ,请重新输入。'); return; } if (linkPage > self.pageCount) { ER.messager.tip('跳转页码不能大于最大页数 ,请重新输入。'); return; } if (self.actions.click && typeof self.actions.click == 'function') { self.pageIndex = linkPage; self.actions.click('linkPage', self.pageIndex - 1); } }); }; Pagination.prototype.addEvents = function() { var container = this.$container.find('.fdpr-pagination-container'); ER.bindVKEvents(container.find('.edp-pagination')[0]); }; /** * 初始化按钮可用性 * @return {[type]} [description] */ Pagination.prototype.initBtnDisable = function() { var self = this; var pageContainer = this.$container.find('.fdpr-pagination-container'); if (self.pageIndex === 1 && (self.pageCount === 0 || self.pageCount === 1)) { pageContainer.find('.edp-first').addClass('pagi-button-disabled'); pageContainer.find('.edp-previous').addClass('pagi-button-disabled'); pageContainer.find('.edp-next').addClass('pagi-button-disabled'); pageContainer.find('.edp-last').addClass('pagi-button-disabled'); pageContainer.find('.edp-link').addClass('pagi-button-disabled'); } else if (self.pageIndex === 1) { pageContainer.find('.edp-first').addClass('pagi-button-disabled'); pageContainer.find('.edp-previous').addClass('pagi-button-disabled'); pageContainer.find('.edp-next').removeClass('pagi-button-disabled'); pageContainer.find('.edp-last').removeClass('pagi-button-disabled'); pageContainer.find('.edp-link').removeClass('pagi-button-disabled'); } else if (self.pageIndex === self.pageCount) { pageContainer.find('.edp-first').removeClass('pagi-button-disabled'); pageContainer.find('.edp-previous').removeClass('pagi-button-disabled'); pageContainer.find('.edp-next').addClass('pagi-button-disabled'); pageContainer.find('.edp-last').addClass('pagi-button-disabled'); pageContainer.find('.edp-link').removeClass('pagi-button-disabled'); } else { pageContainer.find('.edp-first').removeClass('pagi-button-disabled'); pageContainer.find('.edp-previous').removeClass('pagi-button-disabled'); pageContainer.find('.edp-next').removeClass('pagi-button-disabled'); pageContainer.find('.edp-last').removeClass('pagi-button-disabled'); pageContainer.find('.edp-link').removeClass('pagi-button-disabled'); } } /** * [初始化页码及按钮可用性] * @param {[type]} 页码(从0开始) [description] * @param {[type]} 每页显示数 [description] * @param {[type]} 总数据条数 [description] */ Pagination.prototype.setRefreshPages = function(pageIndex, pageSize, totalRows) { var self = this; var pageContainer = this.$container.find('.fdpr-pagination-container'); var pageCount = Math.ceil(totalRows / pageSize) var $pagesize = pageContainer.find('.edp-pageNumber'); $pagesize.text(pageSize); var $totalRows = pageContainer.find('.edp-allNum'); $totalRows.text(totalRows); var $pageIndex = pageContainer.find('.edp-currentPage'); $pageIndex.text(pageIndex + 1); self.pageIndex = pageIndex + 1; var $pageCount = pageContainer.find('.edp-allPage'); $pageCount.text(pageCount); self.pageCount = pageCount; this.initBtnDisable(); } /** * [获取分页信息] * @return {[type]} [description] */ Pagination.prototype.getPageInfo = function() { return { pageIndex: this.pageIndex, pageCount: this.pageCount, pagesize: this.pagesize, totalRows: this.totalRows }; }; return Pagination; });