123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- function page_ctrl(data_obj) {
- //分页元素
- var obj_box = (data_obj.obj_box !== undefined) ? data_obj.obj_box : function() { return; };
- //总共数据条数
- var total_item = (data_obj.total_item !== undefined) ? parseInt(data_obj.total_item) : 0;
- //每页显示数据条数
- var per_num = (data_obj.per_num !== undefined) ? parseInt(data_obj.per_num) : 5;
- //当前页数
- var current_page = (data_obj.current_page !== undefined) ? parseInt(data_obj.current_page) : 1;
- //总页数
- var total_page = Math.ceil(total_item / per_num);
- // if (total_page < 2) { return; }
- //插入放数据的div
- $(obj_box).html('<div class="page_content"></div>');
- //插入放页码的div
- $(obj_box).html('<div class="page_ctrl"></div>');
- $('.page_activitiesPage').css({display:'none'}); //首次进入隐藏翻页组件
- function page_even() {
- //只能传达数字
- var regPos = /^\d+$/;
- //如果当前页数不是数字
- if (regPos.test(current_page)) {
- current_page = current_page < 1 ? 1 : (current_page > total_page ? total_page : current_page);
- } else {
- current_page = 1;
- }
- //调用 回调函数
- data_obj.change_content(per_num, current_page);
- var inp_val = (current_page == total_page) ? 1 : current_page;
- //分页div前半部分
- var append_html = '<span class="page_total">共 ' + total_page + ' 页</span><button class="iconfont icon-xiangzuo prev_page"></button>';
- //页码数量
- for (var i = 0; i < total_page - 1; i++) {
- if (total_page > 8 && current_page > 6 && i < current_page - 3) {
- if (i < 2) {
- append_html += '<button class="page_num">' + (i + 1) + '</button>';
- } else if (i == 2) {
- append_html += '<span class="page_dot">...</span>';
- }
- } else if (total_page > 8 && current_page < total_page - 3 && i > current_page + 1) {
- if (current_page > 6 && i == current_page + 2) {
- append_html += '<span class="page_dot">...</span>';
- } else if (current_page < 7) {
- if (i < 8) {
- append_html += '<button class="page_num">' + (i + 1) + '</button>';
- } else if (i == 8) {
- append_html += '<span class="page_dot">...</span>';
- }
- }
- } else {
- if (i == current_page - 1) {
- append_html += '<button class="page_num current_page">' + (i + 1) + '</button>';
- } else {
- append_html += '<button class="page_num">' + (i + 1) + '</button>';
- }
- }
- }
- //生成最后一页
- if (current_page == total_page) {
- append_html += '<button class="page_num current_page">' + (i + 1) + '</button>';
- } else {
- append_html += '<button class="page_num">' + (i + 1) + '</button>';
- }
- //分页div后半部分
- append_html += '<button class="iconfont icon-xiangyou next_page"></button><span class="page_total">到第</span><input class="input_page_num" type="text" ><span class="page_text">页</span><button class="to_page_num">确定</button>';
- $(obj_box).children('.page_ctrl').append(append_html);
- //左右分页按钮监控
- if (current_page == 1) {
- $(obj_box + ' .page_ctrl .prev_page').attr('disabled', 'disabled').addClass('btn_dis');
- } else {
- $(obj_box + ' .page_ctrl .prev_page').removeAttr('disabled').removeClass('btn_dis');
- }
- if (current_page == total_page) {
- $(obj_box + ' .page_ctrl .next_page').attr('disabled', 'disabled').addClass('btn_dis');
- } else {
- $(obj_box + ' .page_ctrl .next_page').removeAttr('disabled').removeClass('btn_dis');
- }
- };
- page_even();
- //给按钮绑定点击事件
- $(obj_box + ' .page_ctrl').on('click', 'button', function() {
- var that = $(this);
- if (that.hasClass('prev_page')) {
- if (current_page != 1) {
- current_page--;
- that.parent('.page_ctrl').html('');
- page_even();
- }
- } else if (that.hasClass('next_page')) {
- if (current_page != total_page) {
- current_page++;
- that.parent('.page_ctrl').html('');
- page_even();
- }
- } else if (that.hasClass('page_num') && !that.hasClass('current_page')) {
- current_page = parseInt(that.html());
- that.parent('.page_ctrl').html('');
- page_even();
- } else if (that.hasClass('to_page_num')) {
- current_page = parseInt(that.siblings('.input_page_num').val());
- that.parent('.page_ctrl').html('');
- page_even();
- }
- });
- }
|