Last active
December 19, 2015 19:19
-
-
Save dreamsky/6005531 to your computer and use it in GitHub Desktop.
jQuery 分页插件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.extend({ | |
paging : function(options) { | |
var default_configs = { | |
container: null, | |
total_count: 0, | |
page_size: 0, | |
cur_page: 0, | |
list_count: 5, | |
goPage: function(page_num) {} | |
}; | |
var os = $.extend({}, default_configs, options); | |
if(!os.container || !os.total_count || !os.page_size || !os.cur_page || !os.goPage) | |
return; | |
var m = os.total_count/os.page_size; | |
var total_page_count = Math.ceil(m); | |
var count_info = '<span>共有<b style="color:red">' + total_page_count + '</b>页,<b style="color:red">' + os.total_count + '</b>条数据</span>'; | |
if(total_page_count < 2){ | |
os.container.html(count_info); | |
return; | |
} | |
var firstPage = os.cur_page - Math.floor(os.list_count/2); | |
if (firstPage < 1) | |
firstPage = 1; | |
var lastPage = firstPage + os.list_count - 1; | |
if (lastPage > total_page_count) { | |
lastPage = total_page_count; | |
firstPage = lastPage - os.list_count + 1; | |
if (firstPage < 1) | |
firstPage = 1; | |
} | |
var prefix = '<a href="#1" pg="1" class="' + (os.cur_page == 1 ? 'page-now':'') + '">首页</a>'; | |
var paging_html = ''; | |
for(var p=firstPage;p<=lastPage;p++) { | |
var cn = ''; | |
if(p == os.cur_page) | |
cn = 'page-now'; | |
paging_html += '<a href="#'+ p +'" pg="'+ p +'" class="' + cn + '">' + p + '</a>'; | |
} | |
var posfix = '<a href="#' + total_page_count + '" pg="' + total_page_count + '" class="' + (os.cur_page == total_page_count ? 'page-now':'') + '">末页</a>'; | |
var ctn = os.container; | |
ctn.html(prefix + paging_html + posfix + count_info); | |
ctn.find('a').die('click'); | |
ctn.find('a').live('click', function(){ | |
var cur_page = $(this).attr('pg'); | |
os.goPage(parseInt(cur_page)); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment