文件有:pagination.js 分頁邏輯 paginationModel 分頁數據模型 pagination.html html模版javascript
pagination.js 相關代碼html
define(function (require, exports, modules) { 'use strict'; var tpl = require('text!./pagination.html'); var view = Backbone.View.extend({ template: _.template(tpl), events:{ 'click li,.page-btn':'changePageTrigger', 'blur .unify-page-size input':'changePageSize', 'click .unify-page-goto button':'gotoPageNum', 'keypress .unify-page-size input':'keySizeChange', 'keypress .unify-page-goto input':'keyNumChange' }, initialize: function () { _.bindAll(this,'changePageTrigger','changePageSize','gotoPageNum','keySizeChange','keyNumChange'); this.listenTo(this.model, 'change', this.render); }, render: function () { // 初始化viewer 並渲染 this.$el.html(this.template(this.model.toJSON())); return this; }, changePageTrigger:function(e){ e.preventDefault(); var self = $(e.currentTarget); var pageNum = ''; if(self.hasClass('disabled')) return false; if(self.hasClass('page-num')){ pageNum = self.text(); }else if(self.hasClass('previous')){ pageNum = this.model.get('pageNum')-1; }else if(self.hasClass('next')){ pageNum = this.model.get('pageNum') + 1; } this.gotoPageNum(Number(pageNum)); }, keySizeChange:function(event){ if(event.keyCode == "13") { this.changePageSize(event); } }, keyNumChange:function(event){ if(event.keyCode == "13") { this.gotoPageNum(); } }, changePageSize:function(event){ //更改頁面頁數大小 var self = $(event.currentTarget); if(self.val() > 1000000000){ unifyComponent.unifyWarn("每頁條數不能超過10億"); return false; } if(self.val()!="" && (!/^[0-9]*$/.test(self.val()))){ // 驗證數字 unifyComponent.unifyWarn("每頁條數必須是整數"); return false; } if(self.val() == 0){ unifyComponent.unifyWarn("每頁條數不能爲0"); return false; } if(this.model.get('pageSize') != self.val()){ var pageCount = Math.ceil(this.model.get('totalCount') / self.val()); var json = _.extend({},this.model.toJSON(),{pageCount:pageCount,pageSize:self.val()}); this.model.setBeforeHandle(json,true); this.trigger('page', this.model.toJSON()); // 觸發函數 } }, gotoPageNum:function(pageNum){ //更改頁面當前頁 var page = _.isNumber(pageNum) ? pageNum : parseInt(this.$el.find('.unify-page-goto input').val()); if(page > 1000000000){ unifyComponent.unifyWarn("輸入頁數不能超過10億"); return false; } if(page!="" && (!/^[0-9]*$/.test(page))){ // 驗證數字 unifyComponent.unifyWarn("頁數必須是整數"); return false; } if(!page || page > this.model.get('pageCount')){ unifyComponent.unifyWarn('輸入錯誤超過總頁數或者沒輸入'); return false; } var json = _.extend({},this.model.toJSON(),{pageNum:page}); this.model.setBeforeHandle(json); this.trigger('page', this.model.toJSON()); // 觸發函數 } }); return view; });
paginationModel 對應代碼java
/** * Created by carey on 16/6/28. */ define(function (require, exports, modules) { 'use strict'; var Model = Backbone.Model.extend({ defaults:{ first:false,//第一頁 last:true, //最後一頁 prev:false,//上一頁 next:true,//下一頁 hasPrevMore:false, //是否有上一頁 hasNextMore:false, //是否有下一頁 visiblePages:5,// 展現頁數寬帶 startPage:1, pageNum:1, pageSize:10, totalCount:0, pageCount:-1 }, getRequestPage:function(flag){ var page = {}; if(flag){ this.set({pageNum:1,startPage:1},{silent:false}); } page.pageNum = this.get('pageNum'); page.pageSize = this.get('pageSize'); // page.pageCount = this.get('pageCount'); return {page:page}; }, setBeforeHandle:function(json){ if( (json.pageNum == this.get('pageNum') && json.pageSize == this.get('pageSize') && json.pageCount == this.get('pageCount') && json.totalCount == this.get('totalCount'))){ return false; } json.prev = !(json.pageNum == 1); json.next = !(json.pageNum == json.pageCount); if(json.visiblePages >= json.pageCount || json.pageCount < 5){ json.visiblePages = json.pageCount; json.hasPrevMore = false; json.hasNextMore = false; this.set(json); return false; }else{ json.visiblePages = 5; } if((json.pageNum + json.visiblePages) >= json.pageCount) { json.startPage = json.pageCount - json.visiblePages+1; } var lastNum = json.visiblePages + json.startPage - 1; if(lastNum <= json.pageNum && lastNum != json.pageCount){ json.startPage = json.pageNum + 1; lastNum++; } json.hasNextMore = lastNum < json.pageCount; if(json.startPage >= json.pageNum && json.startPage != 1){ json.startPage = json.pageNum == 1 ? json.pageNum : json.pageNum - 1; } json.hasPrevMore = json.startPage > 1; this.set(json); } }); return Model; });
pagination.html 對應代碼json
<%if(pageCount > 1 || totalCount > 10){%> <div class="unify-page"> <div class="unify-page-inner"> <div class="unify-page-num"> <ul> <li class="previous <%=prev ? '' : 'disabled'%>"><a>上一頁</a></li> <%for(var i = startPage; i < visiblePages + startPage; i++){%> <%if(i == pageNum){%> <li class="page-num num active"><a href=""><%=i%></a></li> <%}else{%> <li class="page-num num"><a href=""><%=i%></a></li> <%}%> <%}%> <li class="next <%=next ? '' : 'disabled'%>" ><a class="">下一頁</a></li> </ul> </div> <div class="unify-page-goto"> <input type="text" value="<%=pageNum%>" /><button>GO</button> </div> <div class="unify-page-info"> 共<%=pageCount%>頁 <%if(totalCount==10001){%>共10000+條<%}else{%>共<%=totalCount%>條<%}%> </div> <div class="unify-page-size"> 每頁顯示 <input type="text" value="<%=pageSize%>"/> 條 </div> </div> </div> <%}%>