pagination.js 2.7 KB
/**
 * 生成换页组件
 *
 * @param page 总的页数
 * @param currentPage 当前页序号
 * @returns {string} 生成换页组件的innerHTML
 */
function genaratePagination(pageCount, currentPage, functionName) {
    var isNeedLeft3Point = false;
    var isNeedRightPoint = false;

    var left;
    var right;

    var innerHTML = "";
    innerHTML += "<div>";
    innerHTML += "<ul class='pagination pagination-split mt5 pull-right' style='margin:5px'>";

    if(pageCount <= 10) {
        left = 0;
        right = pageCount;
    } else {
        if (currentPage < 5) {
            left = 0;
            right =10;
            isNeedRightPoint = true;
        } else if (currentPage >= pageCount  - 5) {
            left = pageCount - 10;
            right = pageCount;
            isNeedLeft3Point = true;
        } else {
            left = currentPage - 4;
            right = Number(currentPage) + Number(6);
            isNeedRightPoint = true;
            isNeedLeft3Point = true;
        }
    }

    //生成向前翻页按钮
    if(currentPage == 0) {
        innerHTML += "<li class='disabled'><a><i class='fa fa-angle-double-left'></i></a>";
        innerHTML += "<li class='disabled'><a><i class='fa fa-angle-left'></i></a>";
    } else {
        innerHTML += "<li><a href=\"javascript:" + functionName + "(\'"+ "FIRST_PAGE" +"\')\"><i class='fa fa-angle-double-left'></i></a>";
        innerHTML += "<li><a href=\"javascript:" + functionName + "(\'"+ "LEFT_PAGE" +"\')\"><i class='fa fa-angle-left'></i></a></li>";
    }
    if(isNeedLeft3Point) {
        innerHTML += "<li class='disabled'>...<li>";
    }

    //生成中间页翻页按钮
    for(var i = left; i < right; i ++) {

        if(i == currentPage) {
            var page = Number(i) + 1;
            innerHTML += "<li class='active'><a href=\"javascript:" + functionName + "(\'"+ i +"\')\">" + page + "</a></li>";
        } else {
            var page = Number(i) + 1;
            innerHTML += "<li><a href=\"javascript:" + functionName + "(\'"+ i +"\')\">" + page + "</a></li>";
        }
    }

    //向后翻页按钮翻页按钮
    if (isNeedRightPoint) {
        innerHTML += "<li class='disabled'>...<li>";
    }
    if (currentPage == pageCount - 1) {
        innerHTML += "<li class='disabled'><a ><i class='fa fa-angle-right'></i></a></li>";
        innerHTML += "<li class='disabled'><a ><i class='fa fa-angle-double-right'></i></a></li>";
    } else {
        innerHTML += "<li><a href=\"javascript:" + functionName + "(\'"+ "RIGHT_PAGE" +"\')\"><i class='fa fa-angle-right'></i></a></li>";
        innerHTML += "<li><a href=\"javascript:" + functionName + "(\'"+ "LAST_PAGE" +"\')\"><i class='fa fa-angle-double-right'></i></a></li>";
    }

    return innerHTML;
}