JS分頁控制項 可用於無重新整理分頁

來源:互聯網
上載者:User

JS分頁控制項,可用於無重新整理分頁

複製代碼 代碼如下:
function PagerBar(recordcount, pagesize, pageindex, showpagecount) {
    var NumberRegex = new RegExp(/^\d+$/);
    this.PageIndex = 1; //頁索引,當前頁
    if (pageindex != null && NumberRegex.test(pageindex)) this.PageIndex = parseInt(pageindex);
    this.PageSize = 10; //頁面大小
    if (pagesize != null && NumberRegex.test(pagesize)) this.PageSize = parseInt(pagesize);
    this.RecordCount = 0;
    if (recordcount != null && NumberRegex.test(recordcount)) this.RecordCount = parseInt(recordcount); //記錄總數
    this.PageCount = 0;  //頁總數
    var PagerBar = this;
    function CalculatePageCount(_pagesize, _recordcount) {//計算總頁數
        if (_pagesize != null && NumberRegex.test(_pagesize)) PagerBar.PageSize = parseInt(_pagesize);
        if (_recordcount != null && NumberRegex.test(_recordcount)) PagerBar.RecordCount = parseInt(_recordcount);
        else PagerBar.RecordCount = 0;
        if (PagerBar.RecordCount % PagerBar.PageSize == 0) {//計算總也頁數
            PagerBar.PageCount = parseInt(PagerBar.RecordCount / PagerBar.PageSize);
        }
        else {
            PagerBar.PageCount = parseInt(PagerBar.RecordCount / PagerBar.PageSize) + 1;
        }
    }
    if (this.RecordCount != 0) {//如果傳入了記錄總數則計算總頁數
        CalculatePageCount(this.PageSize, this.RecordCount);
    }
    this.ReplaceString = "《#PageLink》"; //替換頁數的文本,註:不可以有Regex中的符號
    this.ShowPagesCount = 5; //顯示頁數量
    if (showpagecount != null && NumberRegex.test(showpagecount.toString())) this.ShowPagesCount = parseInt(showpagecount);
    this.PreviouBarFormat = ""; //上一頁顯示文字格式設定
    this.IsShowPreviouString = true; //是否顯示上一頁
    this.NextBarFormat = ""; //下一頁顯示文字格式設定
    this.IsShowNextString = true; //是否顯示下一頁
    this.PageBarFormat = ""; //頁面串連顯示文字格式設定
    this.CurrentBarFormat = ""; //當前頁顯示文字格式設定
    this.IsShowPageString = true; //是否顯示頁索引
    this.FristBarFormat = ""; //首頁連結顯示文字格式設定
    this.IsShowFristString = true; //是否顯示首頁
    this.LastBarFormat = ""; //尾頁顯示文字格式設定
    this.IsShowLastString = true; //是否顯示尾頁
    this.CurrentRecordBarFormat = "目前記錄{0}-{1}"; //目前記錄顯示文字格式設定
    this.IsShowCurrentRecordString = true; //是否顯示目前記錄
    this.CurrentPageBarFormat = "當前第" + this.ReplaceString + "頁,共" + (this.PageCount == 0 ? 1 : this.PageCount) + "頁"; //當前頁文字說明文字格式設定
    this.IsShowCurrentPageString = true; //是否顯示當前頁文字說明文本
    this.OtherBarFormat = ""; //其他也顯示文本
    this.IsShowOtherString = true; //是否顯示其它頁文本
    var regexp = new RegExp(this.ReplaceString, "g"); //替換頁數文本Regex
    function GetFristPageString() {//擷取首頁文本
        if (PagerBar.FristBarFormat != "" && PagerBar.PageIndex != 1) {
            return PagerBar.FristBarFormat.replace(regexp, 1);
        }
        else {
            return "";
        }
    }
    function GetPreviouPageString() { //擷取上一頁文本
        if (PagerBar.PreviouBarFormat != "") {
            if (PagerBar.RecordCount > PagerBar.PageSize && PagerBar.PageIndex != 1) {//上一頁HTML輸出
                return PagerBar.PreviouBarFormat.replace(regexp, PagerBar.PageIndex - 1);
            }
            else {
                return "";
            }
        }
        else {
            return "";
        }
    }
    function GetPageString() {//擷取中間頁數連結
        var pagestr = "";
        if (PagerBar.CurrentBarFormat != "" && PagerBar.PageBarFormat != "") {
            var ShowPageFirest = PagerBar.PageIndex - parseInt(PagerBar.ShowPagesCount / 2 + 1) < 0 ? 0 : PagerBar.PageIndex - parseInt(PagerBar.ShowPagesCount / 2 + 1); //計算顯示頁數的其實頁數
            if (PagerBar.PageCount < PagerBar.ShowPagesCount) {//當也總數小於顯示頁數量
                ShowPageFirest = 0;
            }
            else {
                if (PagerBar.PageIndex > (PagerBar.PageCount - parseInt(PagerBar.ShowPagesCount / 2 + 1))) {//當頁總數在後幾頁顯示
                    ShowPageFirest = PagerBar.PageCount - PagerBar.ShowPagesCount;
                }
            }
            for (var i = ShowPageFirest; i < ShowPageFirest + PagerBar.ShowPagesCount; i++) {//迴圈出書頁數文本
                if (PagerBar.PageIndex == i + 1) {
                    pagestr += PagerBar.CurrentBarFormat.replace(regexp, i + 1);
                }
                else {
                    pagestr += PagerBar.PageBarFormat.replace(regexp, i + 1);
                }
                if (i >= PagerBar.PageCount - 1) {//當到達頁總數的時候挑出迴圈
                    break;
                }
            }
        }
        return pagestr;
    }
    function GetNextPageString() {//擷取下一頁連結
        if (PagerBar.NextBarFormat != "") {
            if (PagerBar.RecordCount > PagerBar.PageSize && PagerBar.PageIndex != PagerBar.PageCount) {//輸出下一頁HTMl
                return PagerBar.NextBarFormat.replace(regexp, PagerBar.PageIndex + 1);
            }
            else {
                return "";
            }
        }
        else {
            return "";
        }
    }
    function GetLastPageString() {//擷取尾頁連結
        if (PagerBar.LastBarFormat != "" && PagerBar.PageIndex != PagerBar.PageCount && PagerBar.RecordCount != 0) {
            return PagerBar.LastBarFormat.replace(regexp, PagerBar.PageCount);
        }
        else {
            return "";
        }
    }

    function GetFrontOtherPageString() {//擷取前其它頁連結
        if (PagerBar.OtherBarFormat != "") {
            if (PagerBar.PageIndex > PagerBar.ShowPagesCount / 2 + 1) {
                return PagerBar.OtherBarFormat.replace(regexp, PagerBar.PageIndex - PagerBar.ShowPagesCount <= 0 ? 1 : PagerBar.PageIndex - PagerBar.ShowPagesCount)
            }
            else {
                return "";
            }
        }
        else {
            return "";
        }
    }
    function GetAfterOtherPageString() {//擷取後其它頁連結
        if (PagerBar.OtherBarFormat != "") {
            if (PagerBar.PageIndex <= PagerBar.PageCount - PagerBar.ShowPagesCount / 2) {
                return PagerBar.OtherBarFormat.replace(regexp,
                PagerBar.PageIndex + PagerBar.ShowPagesCount >= PagerBar.PageCount ? PagerBar.PageCount : PagerBar.PageIndex + PagerBar.ShowPagesCount);
            }
            else {
                return "";
            }
        }
        else {
            return "";
        }
    }
    function GetCurrentRecordPageString() {//擷取目前記錄文本
        if (PagerBar.CurrentRecordBarFormat != "") {
            if (PagerBar.RecordCount == 0) {
                return "";
            }
            else {
                return PagerBar.CurrentRecordBarFormat.replace("{0}", (PagerBar.PageIndex - 1) * PagerBar.PageSize + 1).replace("{1}", PagerBar.PageIndex * PagerBar.PageSize > PagerBar.RecordCount ? PagerBar.RecordCount : PagerBar.PageIndex * PagerBar.PageSize);
            }
        }
        else return "";
    }
    function GetCurrentPageBarString() {//擷取當前頁記錄文本
        if (PagerBar.CurrentPageBarFormat != "") {
            return PagerBar.CurrentPageBarFormat.replace(regexp, PagerBar.PageIndex);
        }
        else return "";
    }
    this.GetString = function (pageindex) {//輸出HTML代碼(全部模式)
        if (pageindex != null && NumberRegex.test(pageindex)) {//如果傳入了頁索引則賦值
            this.PageIndex = parseInt(pageindex);
        }
        if (this.PageCount == 0) {//如果沒有計算過頁總數,則計算頁總數
            CalculatePageCount(this.PageSize, this.RecordCount);
        }
        var pagestr = "";
        if (this.IsShowCurrentPageString) {
            pagestr = GetCurrentPageBarString();
        }
        if (this.IsShowCurrentRecordString) {
            pagestr += GetCurrentRecordPageString();
        }
        if (this.IsShowFristString) {
            pagestr += GetFristPageString();
        }
        if (this.IsShowPreviouString) {
            pagestr += GetPreviouPageString();
        }
        if (this.IsShowOtherString) {
            pagestr += GetFrontOtherPageString();
        }
        if (this.IsShowPageString) {
            pagestr += GetPageString();
        }
        if (this.IsShowOtherString) {
            pagestr += GetAfterOtherPageString();
        }
        if (this.IsShowNextString) {
            pagestr += GetNextPageString();
        }
        if (this.IsShowLastString) {
            pagestr += GetLastPageString();
        }
        return pagestr;
    }
    this.GetNormalString = function (pageindex) {
        if (pageindex != null && NumberRegex.test(pageindex)) {//如果傳入了頁索引則賦值
            this.PageIndex = parseInt(pageindex);
        }
        if (this.PageCount == 0) {//如果沒有計算過頁總數,則計算頁總數
            CalculatePageCount(this.PageSize, this.RecordCount);
        }
        var pagestr = "";
        pagestr += GetFristPageString();
        pagestr += GetPreviouPageString();
        pagestr += GetPageString();
        pagestr += GetNextPageString();
        pagestr += GetLastPageString();
        return pagestr;
    }
    this.GetSimpleString = function (pageindex) {
        if (pageindex != null && NumberRegex.test(pageindex)) {//如果傳入了頁索引則賦值
            this.PageIndex = parseInt(pageindex);
        }
        if (this.PageCount == 0) {//如果沒有計算過頁總數,則計算頁總數
            CalculatePageCount(this.PageSize, this.RecordCount);
        }
        var pagestr = "";
        pagestr += GetPreviouPageString();
        pagestr += GetCurrentPageBarString();
        pagestr += GetNextPageString();
        return pagestr;
    }
}

使用樣本:

暫無

內容中需要的知識點
分頁符《#PageLink》

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.