123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- var Paging = function(nodeid,url,datas,pageSize,callBack,firstPageDatas){
- this.currentPage = 1;
- this.totalPages = 0;
- this.count = 0;
- this.firstPageDatas = firstPageDatas;
- //初始化
- this.init = function(){
- this.totalPages = parseInt((this.count + pageSize - 1) / pageSize);
- if(this.totalPages > 1){
- var html = '<nav>'
- +'<ul class="pagination"><li';
- if(!this.hasPrevPage()){
- html += ' class="disabled"';
- }
- html += ' id="firstPage"><a>«</a></li><li';
- if(!this.hasPrevPage()){
- html += ' class="disabled"';
- }
- html += ' id="prevPage">'
- +'<a>‹</a>'
- +'</li>';
- for(var i=0;i<this.totalPages;i++){
- html += '<li id="pageNum-'+(i+1)+'"';
- if(i == 0){
- html += ' class="disabled"';
- }else if(i >= 10){
- html += ' class="hide"';
- }
- html += '><a>'+(i+1)+'</a></li>';
- }
- html += '<li';
- if(!this.hasNextPage()){
- html += ' class="disabled"';
- }
- html += ' id="nextPage"><a>›</a></li><li';
- if(!this.hasNextPage()){
- html += ' class="disabled"';
- }
- html += ' id="lastPage"><a>»</a></li></ul></nav>';
- $("#"+nodeid).html(html);
- var thisClass = this;
- var getThisClass = function(){
- return thisClass;
- }
- $("#"+nodeid+" #firstPage").click(function(){
- if($(this).hasClass("disabled")){
- return;
- }
- thisClass.currentPage = 1;
- $(this).addClass("disabled");
- $("#"+nodeid+" #prevPage").addClass("disabled");
- $("#"+nodeid+" #nextPage,#"+nodeid+" #lastPage").removeClass("disabled");
- thisClass.commonMethod();
- });
- $("#"+nodeid+" #prevPage").click(function(){
- if($(this).hasClass("disabled")){
- return;
- }
- $("#"+nodeid+" #pageNum-"+(--thisClass.currentPage)).addClass("disabled");
- if(thisClass.hasPrevPage()){
- $(this).removeClass("disabled");
- $("#"+nodeid+" #firstPage").removeClass("disabled");
- }else{
- $(this).addClass("disabled");
- $("#"+nodeid+" #firstPage").addClass("disabled");
- }
- $("#"+nodeid+" #nextPage,#"+nodeid+" #lastPage").removeClass("disabled");
- thisClass.commonMethod();
- });
- $("#"+nodeid+" #nextPage").click(function(){
- if($(this).hasClass("disabled")){
- return;
- }
- $("#"+nodeid+" #pageNum-"+(++thisClass.currentPage)).addClass("disabled");
- if(thisClass.hasNextPage()){
- $(this).removeClass("disabled");
- $("#"+nodeid+" #lastPage").removeClass("disabled");
- }else{
- $(this).addClass("disabled");
- $("#"+nodeid+" #lastPage").addClass("disabled");
- }
- $("#"+nodeid+" #firstPage,#prevPage").removeClass("disabled");
- thisClass.commonMethod();
- });
- $("#"+nodeid+" #lastPage").click(function(){
- if($(this).hasClass("disabled")){
- return;
- }
- thisClass.currentPage = thisClass.totalPages;
- $("#"+nodeid+" #firstPage,#"+nodeid+" #prevPage").removeClass("disabled");
- $("#"+nodeid+" #nextPage,#"+nodeid+" #lastPage").addClass("disabled");
- thisClass.commonMethod();
- });
- $("#"+nodeid+" [id^='pageNum-']").click(function(){
- if($(this).hasClass("disabled")){
- return;
- }
- thisClass.currentPage = parseInt(this.id.split("-")[1]);
- if(thisClass.hasPrevPage()){
- $("#"+nodeid+" #firstPage,#"+nodeid+" #prevPage").removeClass("disabled");
- }else{
- $("#"+nodeid+" #firstPage,#"+nodeid+" #prevPage").addClass("disabled");
- }
- if(thisClass.hasNextPage()){
- $("#"+nodeid+" #nextPage,#"+nodeid+" #lastPage").removeClass("disabled");
- }else{
- $("#"+nodeid+" #nextPage,#"+nodeid+" #lastPage").addClass("disabled");
- }
- thisClass.commonMethod();
- });
- }
- }
- this.commonMethod = function(){
- $("#"+nodeid+" [id^='pageNum-']").removeClass("disabled");
- $("#"+nodeid+" #pageNum-"+this.currentPage).addClass("disabled");
- if(this.currentPage == 1){
- $("#"+nodeid+" [id^='pageNum-']").addClass("hide");
- for(var i=1;i<=10;i++){
- if(i > this.totalPages){
- break;
- }
- $("#"+nodeid+" #pageNum-"+i).removeClass("hide");
- }
- }else if(this.currentPage >= 5){
- var start = this.currentPage-5;
- var end = this.currentPage+5;
- if(this.currentPage > this.totalPages - 5){
- start = this.totalPages - 10;
- end = this.totalPages;
- }
- for(var i=1;i<=start;i++){
- $("#"+nodeid+" #pageNum-"+i).addClass("hide");
- }
- for(var i=start+1;i<=end;i++){
- $("#"+nodeid+" #pageNum-"+i).removeClass("hide");
- }
- for(var i=end+1;i<=this.totalPages;i++){
- $("#"+nodeid+" #pageNum-"+i).addClass("hide");
- }
- }
- this.getDatas();
- }
- //从服务端获取数据
- this.getDatas = function(){
- if(datas == null){
- datas = {};
- }
- datas["pageSize"] = pageSize;
- datas["currentPage"] = this.currentPage;
- var thisClass = this;
- if(typeof(this.firstPageDatas) != "undefined" && this.firstPageDatas != null){
- if(this.totalPages == 0){
- this.count = this.firstPageDatas.count;
- this.init();
- }
- callBack(this.firstPageDatas.list);
- }else{
- $.post(url,datas,function(r){
- if(thisClass.totalPages == 0){
- thisClass.count = r.count;
- thisClass.init();
- }
- callBack(r.list);
- });
- }
- }
- //是否有上一页
- this.hasPrevPage = function(){
- if(this.currentPage > 1){
- return true;
- }
- return false;
- }
- //是否有下一页
- this.hasNextPage = function(){
- if(this.currentPage < this.totalPages){
- return true;
- }
- return false;
- }
- //第一次获取数据和总数
- this.getDatas();
- }
|