分頁類改進

// JavaScript Document
/**
*分頁類
*
**/
/***************************************************/
	  var pageFunc=function(){
		//alert('here');
		var _this=this;
		this.datas=100;//所有數據條數
		this.render=function(arr){//模擬類的抽象函數
			throw new Error('該函數不能爲空');
			}
		this.renderButton=function(){//模擬類的抽象函數
			throw new Error('該函數不能爲空');
			}
		this.bindEvent=function(){//模擬類的抽象函數
			throw new Error('該函數不能爲空');
			}
		this.init=function(){
			throw new Error('該函數不能爲空');
			}
		this.getChangePage=function(){
			throw new Error('該函數不能爲空');
			}
		this.getPages=function(){
			return 0;
			}
		this.pages=_this.getPages();
		this.currentPage=1;
		this.pageSize=3;
		this.getNextPage=function(){
			var _nextPage=_this.currentPage+1;
			if(_nextPage>=_this.pages){
					_nextPage=_this.pages;
				}
			return _nextPage;
			}
		this.getPreviousPage=function(){
			var _previousPage=1;
			_previousPage=_this.currentPage-1;
			if(_previousPage<=0){
					_previousPage=1;
				}
			return _previousPage;
			}
		this.gotoPage=function(pageNum){
				if(pageNum!=_this.currentPage){
					var _arr='';
					_arr=[];
					_arr['start']=pageNum-1;
					_arr['len']=_this.pageSize;
					//alert(_arr['start']);
					//alert(_arr['len']);
					_this.render(_arr);
					_this.currentPage=pageNum;
					_this.getChangePage();
					//alert(pageNum);
				}
			}
		this.gotoFirstPage=function(){
				//alert('gotoFirstPage');
				//alert(_arr['start']);
				_this.gotoPage(1);
			}
		this.gotoLastPage=function(){
				_this.gotoPage(_this.pages);	
			}
		this.gotoNextPage=function(){
			var _nextPage=_this.currentPage+1;
			if(_nextPage>_this.pages){
				_nextPage=_this.pages;
				}
			_this.gotoPage(_nextPage);
			}
		this.gotoPreviousPage=function(){
			var _previousPage=_this.currentPage-1;
			if(_previousPage<1){
				_previousPage=1
				}
			_this.gotoPage(_previousPage);
			}
		this.run=function(){
			_this.pages=_this.getPages();
			//alert(_this.pages);
			_this.currentPage=1;
			_this.renderButton();
			_this.render({start:0,len:_this.pageSize});
			}
	}
	
/***************************************************/	
         //alert($('div[calss=pages]').html());
	var page=new pageFunc();
	page.pageSize=20;
	page.init=function(){
		$('#page ul').children('li').css('cursor','pointer');
		$('#page ul').children('li[rel=1]').addClass('activePage');
		this.datas=$('#aListUl').children('li').length;
		}
	page.render=function(arr){//arr['start'],arr['len']
			$('#aListUl').children().slideUp(100);
			//console.log(arr['len']);
			//alert('start is '+arr['start']+' and end is '+(arr['start']+arr['len']));
			var _start=arr['start']*arr['len'];
			//alert(_start);
			//alert(arr['len']);
			//alert(this.datas);
			var _end=(_start+1)*arr['len'];
			//alert(this.datas);
			if(_end>this.datas){
					_end=this.datas;
				}
			//alert(_start*arr['len']);
			//alert('start is '+_start+' end is '+_end);
			//alert(_start+' and '+_end);
			$('#aListUl').children('li').slice(_start,_end).slideDown(1000);
			//console.log($('.itemDl').siblings().slice(arr['start'],arr['start']+arr['len']).length);
			//alert($('.itemDl').find('dd').length);
		}
	page.bindEvent=function(){
			var _this=this;
			_this.init();
			$('.pageUl li[rel=firstPage]').live('click',function(e){
				//alert('firstPage');
				_this.gotoFirstPage();
				});
			$('.pageUl li[rel=lastPage]').live('click',function(e){
				_this.gotoLastPage();
				});
			$('.pageUl li[rel=previousPage]').live('click',function(e){
				_this.gotoPreviousPage();
				});
			$('.pageUl li[rel=nextPage]').live('click',function(e){
				_this.gotoNextPage();
				});
			$('.pageUl').find('li').each(function(index){
				var _that=$(this);
				var _rel=_that.attr('rel');
				if(_rel!='firstPage'&&_rel!='previousPage'&&_rel!='nextPage'&&_rel!='lastPage'&&_rel!='total'&&_rel!='gotoPage'){
					_that.live('click',function(e){
						_this.gotoPage(_rel);
						});
					}
				});
			/*$('.pages select').live('change',function(e){
				//alert(e.type);
				var _pageNum=$(this).val();
				_this.gotoPage(_pageNum);
				});*/
		}
	page.renderButton=function(){
			$('.pageUl').html('');
			var _totalPages=this.pages;
			var _nowPage=this.currentPage;
			var _listButton='';
			//var _listSelect='';
			for(var i=0;i<_totalPages;i++){
				_listButton+='<li rel="'+(i+1)+'">'+(i+1)+'</li>';
				//_listSelect+='<option>'+(i+1)+'</option>';
				}
			var _content='<li rel="firstPage" class="firstpage">首頁</li><li rel="previousPage">上一頁</li>'+_listButton+'<li rel="nextPage">下一頁></li><li rel="lastPage">末頁</li><li rel="total">共'+_totalPages+'頁/第'+_nowPage+'頁</li>';
			$('.pageUl').html(_content);
                         //alert(_content);
			//document.write(_content);
			this.bindEvent();
		}
	page.getPages=function(){
		//alert($('.innerList').html());
		var _len=parseInt($('#aListUl').children('li').length);
		//alert(_len);
		//alert(_len);
		var _pages='';
		//alert(this.pageSize);
		if(_len%this.pageSize==0){
			
			_pages=_len/this.pageSize
			}else{
				//alert(_len/this.pageSize);
				_pages=parseInt(_len/this.pageSize)+1;
				//alert(parseInt(_len/this.pageSize)+1);
				}
			return _pages;
		}
	page.getChangePage=function(){
		$('.pageUl li[rel=total]').html('共'+this.pages+'頁/第'+this.currentPage+'頁');
		$('.pageUl li[rel='+this.currentPage+']').addClass('activePage').siblings().removeClass('activePage');
		}
	
	//alert(run.pages);
	//run.gotoNextPage();
/**********************************************************/

發佈了115 篇原創文章 · 獲贊 42 · 訪問量 68萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章