js分页

现在js很强大,用js分页很方便。

一个同事写的分页,混杂着各种东西,比较难懂:

function generate_pagenation1(){
    $("#div_pager").empty();

    page_count = parseInt((records_count - 1 + page_size) / page_size);
    current_page = parseInt(page_start / page_size)
    var str = "";
    if(page_count > 1 && current_page == 0){
        str +=    '<li class="disabled"><a href="#">«</a></li>';
    }
    if(page_count > 1 && current_page != 0){
        str +=    '<li><a href="#" onclick="refresh(\'' + 0 + '\')">«</a></li>';
    }
    for(var i = 1; i <= page_count; i++){
        if(current_page == i - 1){
            str +=    '<li class="active"><a href="#">' + i + '</a></li>';
            continue;
        }
        str +=    '<li><a href="#" onclick="refresh(\'' + (i - 1) + '\')">' + i + '</a></li>';
    }
    if(page_count > 1 && current_page == page_count - 1){
        str +=    '<li class="disabled"><a href="#">»</a></li>';
    }
    if(page_count > 1 && current_page != page_count - 1){
        str +=    '<li><a href="#" onclick="refresh(\'' + (page_count - 1) + '\')">»</a></li>';
    }
    page_no = records_count > 0 ? current_page + 1 : current_page;
    str += '<li><span>第 ' + page_no + ' 页 / 共 ' + page_count + ' 页</span></li>'
    $(".pagination ul").html(str);
}

这里面的变量居多。没有办法维护的。


还有更加恐怖的分页:

function caculate_pageNmb_by_mode(mode) {
    if(isNaN(mode)) {
        switch(mode){
            case "+":
                if(_currentPage < _allPageCount) {
                    _currentPage += 1;
                }
                return _currentPage;
                break;
            case "-":
                if(_currentPage > 1) {
                    _currentPage -= 1;
                }
                return _currentPage;
                break;
        }
    } else {
        if(mode > 0 && mode <= _allPageCount) {
            _currentPage = mode;
        }
        return _currentPage;
    }
}

function goToPage(pageNo) {
    if(undefined == pageNo) {
        var gopagenumber = $("#gopagenumber");
        var value = $.trim(gopagenumber.val());
        if(value == "" || isNaN(value)) {
            gopagenumber.val("");
            return;
        } else {
            value = parseInt(value);
            value = caculate_pageNmb_by_mode(value);
            get_videoList_of_uploaded_by_pageAndStatus(value, get_checkedType_of_edit_select(), get_checkedType_of_transcode_select());
        }
        gopagenumber.val('');
    } else {
        var value = caculate_pageNmb_by_mode(pageNo);
        get_videoList_of_uploaded_by_pageAndStatus(value, get_checkedType_of_edit_select(), get_checkedType_of_transcode_select());
    }
}

function create_html_of_pageNmbList(fromPage, toPage, currentPageNo, allPage) {
    var list = [];
    if(currentPageNo != 1) {
        list.push("<li ><a href='javascript:goToPage(1)'><<</a></li>");
        list.push("<li ><a href='javascript:goToPage(\"-\")'><</a></li>");
    }
    for (var i = fromPage; i <= toPage; i++) {
        if (currentPageNo == i) {
            list.push("<li class='active'><a href='javascript:goToPage("+i+")'>"+i+"</a></li>");
        } else {
            list.push("<li ><a href='javascript:goToPage("+i+")'>"+i+"</a></li>");
        }
    }
    if(currentPageNo != allPage) {
        list.push("<li ><a href='javascript:goToPage(\"+\")'>></a></li>");
        list.push("<li ><a href='javascript:goToPage("+allPage+")'>>></a></li>");
    }
    return list.join("");
}

// TODO: FIXME: use pager.
function changePage(toPageNmb, allPage, pageShow) {
    var fromPage = 0, toPage = 0;
    if (toPageNmb <= 1) {
        toPageNmb = 1;
        fromPage = 1;
        if (allPage < pageShow) {
            toPage = allPage;
        } else {
            toPage = pageShow;
        }
    } else if (toPageNmb >= allPage) {
        toPageNmb = allPage;
        toPage = toPageNmb;
        if (allPage < pageShow) {
            fromPage = 1;
        } else {
            fromPage = allPage - pageShow + 1;
        }
    } else {
        if (allPage - toPageNmb >= pageShow) {
            toPage = toPageNmb + pageShow - 1;
            fromPage = toPageNmb;
        } else {
            toPage = allPage;
            fromPage = allPage - pageShow >= 0 ? allPage - pageShow + 1 : 1;
        }
        var flag = 1,
            middle = Math.round(pageShow / 2);
        if (toPageNmb < (allPage - pageShow + middle)) {
            while (true) {
                if (fromPage - 1 >= 1) {
                    fromPage -= 1;
                    toPage -= 1;
                    flag++;
                } else {
                    break;
                }
                if (flag >= middle) {
                    break;
                }
            }
        }
    }
    $("#pages").html(create_html_of_pageNmbList(fromPage, toPage, toPageNmb, allPage))
}

这个代码是没有办法看懂的。


可以改一下:


你可能感兴趣的:(js分页)