haiconc

用jquery编写的分页插件

用jquery编写的分页插件

源码

function _pager_go(total_page) {
    var page_str = $("#_pager_textbox").val();
    var int_expr = /^(\+|-)?\d+$/;
    if (!int_expr.test(page_str)) {
        alert("请输入整数");
        return;
    }

    var go_page = parseInt(page_str);
    if (go_page < 1 || go_page > total_page) {
        alert("跳转页数超出范围");
        return;
    }
    var link_fn_name = $("#_pager_link_fn_name").val();
    eval(link_fn_name + "(" + go_page + ")");
}


$.fn.ss_pager = function (options) {
    var box = $(this);
    var current_page = options.current_page;
    var total_page = options.total_page;
    var link_class_name = options.link_class || "pageitem";
    var text_class_name = options.text_class || "";
    var link_fn_name = options.link_fn || "void";
    var show_pages = options.show_pages || 10;
    var total_size = options.total_size;
    var btn_class_name = options.btn_class || "btn";

    if (current_page <= 1) {
        current_page = 1;
    }

    if (total_size <= 0) {
        $(box).html("<span style='color:red;'>没有记录</span>");
        return;
    }

    function __wprint(iscurrent, index, text) {
        if (!iscurrent) {
            return "<a class='" + link_class_name + "' href='javascript:" + link_fn_name + "(" + index + ")" + "'>" + text + "</a>";
        }
        else {
            return "<a class='" + text_class_name + "'>" + text + "</a>";
        }
    }

    var html_buf = '';
    if (total_size) {
        html_buf += __wprint(true, "", "共有" + total_size + "条记录");
    }

    html_buf += __wprint(current_page - 1 <= 0, current_page - 1, "上一页");
    if (total_page > 1) {
        //开始页码
        var start = 2;
        var end = total_page - 1;
        var _t = parseInt(show_pages / 2) - 1;

        html_buf += __wprint(current_page == 1, 1, 1);
        if (current_page - _t > 1) {
            html_buf += __wprint(true, "", "...");
            start = current_page - _t;
        }

        for (var i = start; i < current_page; i++) {
            html_buf += __wprint(current_page == i, i, i);
            //console.log("前 "+i);
        };

        if (current_page > 1 && current_page < total_page) {
            html_buf += __wprint(true, current_page, current_page);
        }

        if (current_page + _t < total_page - 1) {
            end = current_page + _t;
            //console.log("后 e "+ (current_page+_t));
            //console.log("后 e "+ (total_page-1));
        }

        for (var i = current_page + 1; i <= end; i++) {
            html_buf += __wprint(current_page == i, i, i);
            //console.log("后 "+i);
        };

        if (current_page + _t < total_page - 1) {
            html_buf += __wprint(true, "", "...");
        };
        html_buf += __wprint(current_page == total_page, total_page, total_page);
    }
    else {
        html_buf += __wprint(current_page == 1, 1, 1);
    }


    html_buf += __wprint(current_page + 1 > total_page, current_page + 1, "下一页");

    html_buf += ("<a class='" + text_class_name + "'> <input type='text' id='_pager_textbox' value='" + current_page + "' />");
    html_buf += ("<input type='hidden' value='" + link_fn_name + "' id='_pager_link_fn_name' />");
    html_buf += ("<input type='button' class='btn' value='转到' id='_pager_button' onclick='_pager_go(" + total_page + ")' /> </a>");

    $(box).html(html_buf);
};

 

posted on 2014-12-26 14:29  haiconc  阅读(190)  评论(0编辑  收藏  举报

导航