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 $(obj_box).html(''); 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 = '共 ' + total_page + ' 页'; //页码数量 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 += ''; } else if (i == 2) { append_html += '...'; } } else if (total_page > 8 && current_page < total_page - 3 && i > current_page + 1) { if (current_page > 6 && i == current_page + 2) { append_html += '...'; } else if (current_page < 7) { if (i < 8) { append_html += ''; } else if (i == 8) { append_html += '...'; } } } else { if (i == current_page - 1) { append_html += ''; } else { append_html += ''; } } } //生成最后一页 if (current_page == total_page) { append_html += ''; } else { append_html += ''; } //分页div后半部分 append_html += '到第页'; $(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(); } }); }