JQuery實現仿Bootstrap-Table的分頁效果

概述

最近項目中使用到分頁組件拓诸,出于練習的目的,使用jquery模仿了BootstrapTable的分頁效果每辟;

實現

  1. 計算總頁數

總頁數 = 數據總條數 / 每頁顯示的數據條數汉规, 結果向上取整

  1. 計算數據開始位置、結束位置

開始位置 = (當前頁碼 - 1)* 當前展示的數據條數 + 1
結束位置 = 當前頁碼 * 當前展示的數據條數

  1. 計算當前頁碼

當點擊上一頁屏镊,當前頁碼減一,當頁碼值等于第一頁的頁碼值時痰腮,將當前頁碼置為最后一頁的頁碼值而芥;
當點擊下一頁,當前頁碼加一膀值,當頁碼值等于最后一頁的頁碼值棍丐,則將當前頁碼值置為首頁的頁碼值弟翘;

  1. 根據數據開始位置和結束為止,加載表格當前頁的數據

使用Array對象的slice(start骄酗,end),方法提取當前需要的表格數據;
即是:
arr.slice(start-1, end)
減1的原因是源數據的索引是從0開始的悦冀,而本例中的數據開始位置是從1開始的趋翻;

  1. 每次改變數據條數的時候,將頁碼置為1

  2. 頁碼列表展示

根據當前數據條數是否大于等于總數據條數盒蟆,若大于等于則隱藏踏烙,反之顯示或者是判斷表格數據的長度是否小于等于1,若是則隱藏历等,反之顯示讨惩;

代碼

HTML

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>分頁組件</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="../css/index.css" />

</head>

<body>
    <table border="1" id="table">
        <thead>
            <tr>
                <th>item1</th>
                <th>item2</th>
                <th>item3</th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
    <div class="c-pagination">
        <div class="pagination-text">
            <div class="page-size">顯示第<span class="start-num" id="startNum">0</span>到第<span class="end-num" id="endNum">0</span>條記錄,總共<span class="total" id="total">0</span>條記錄</div>
            <div class="page-list">
                每頁顯示<select name="" id="changeNumber">
                  <option value="0">10</option>
                  <option value="1">25</option>
                  <option value="2">50</option>
                  <option value="3">100</option>
                  
                </select>條記錄
            </div>
        </div>
        <ul class="pagination-number" id="paginationList">
            <li class="prev" id="prev">
                <span>?</span>
            </li>
            <li class="next" id="next">
                <span>?</span>
            </li>
        </ul>
    </div>
    <script src="../js/jquery/jquery-3.3.1.min.js"></script>
    <script src="../js/vender/index.js"></script>
</body>

</html>

CSS

下列樣式為index.css文件中的樣式:

html,
body {
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
    font-family: 'Microsoft YaHei';
}

* {
    padding: 0;
    margin: 0;
}

ul li {
    list-style: none;
}

table {
    margin: 10px 0;
    width: 100%;
    min-height: 500px;
    border-collapse: collapse;
    border-color: #ccc;
    color: #808696;
}

table th,
table tr {
    width: calc(100% / 3);
}

table tbody th:nth-child(2n) {
    background: #eee;
}

.c-pagination {
    display: flex;
    width: 100%;
    height: 100%;
    margin: 10px 0;
    height: 54px;
}

.pagination-text {
    flex: 1;
    display: -webkit-flex;
    height: 54px;
    line-height: 54px;
    padding-left: 5px;
    box-sizing: border-box;
}

.pagination-text .page-size {
    display: inline-block;
    margin-right: 5px;
    height: 54px;
    line-height: 54px;
    font-size: 12px;
}

.pagination-text .page-size span {
    display: inline-block;
    padding: 0 3px;
    font-size: 12px;
    font-family: "Microsoft YaHei";
    font-family: 'Microsoft YaHei';
    color: #333;
}

.pagination-text .page-list {
    display: inline-block;
    height: 54px;
    line-height: 54px;
    font-family: 'Microsoft YaHei';
    font-size: 12px;
}

.pagination-text .page-list select {
    display: inline-block;
    background-color: #fff;
    border-color: #ccc;
    font-size: 12px;
    padding: 6px 12px;
    margin-top: -10px;
    margin-right: 3px;
    margin-left: 3px;
    color: #808696;
}

.pagination-text .page-list select:hover {
    color: #333;
    background-color: #e6e6e6;
    border-color: #adadad;
}

.pagination-number {
    flex: 1;
    height: 54px;
    line-height: 54px;
    vertical-align: middle;
    text-align: right;
    padding-right: 5px;
    box-sizing: border-box;
    align-self: flex-end;
}

.pagination-number li {
    display: inline-flex;
    align-content: center;
}

.pagination-number li span {
    display: block;
    flex: 1;
    font-size: 12px;
    position: relative;
    font-family: 'Microsoft YaHei';
    padding: 6px 12px;
    margin-left: -6px;
    line-height: 1.42857143;
    color: #337ab7;
    text-decoration: none;
    background-color: #fff;
    border: 1px solid #ddd;
    cursor: pointer;
}

.pagination-number li:hover span {
    background-color: #eee;
    border-color: #ddd;
    color: #1181D9 !important;
}

.pagination-number li.active span {
    border-color: #337ab7;
    color: #1181D9 !important;
    margin-right: 6px;
}

.pagination-number li.prev span {
    margin-left: 0;
    border-top-left-radius: 4px;
    border-bottom-left-radius: 4px;
}

.pagination-number li.next span {
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
}

注意: table的樣式可以調整

JS

下面的腳本代碼為index.js文件的內容:

var pagination = {
    // 表體數據
    data: data,
    // 總條數
    total: 0,
    // 總頁數
    totalPages: 0,
    // 當前頁碼
    pageNumber: 1,
    // 每頁數據條數
    pageSize: 10,
    // 開始數據的位置
    start: 1,
    // 結束數據的位置
    end: 10,
    // 可供選擇的每頁的行數
    pageList: [10, 25, 50, 100],
    init: function(opt) {
        var that = this;
        // 計算總條數
        that.total = that.data.length;
        opt.totalNum.html(that.total);
        that.loadPageData(opt, that);
        // 計算頁數
        that.calculateNumberList(opt);
        that.showTableData(opt);
        opt.startNum.html(that.start);
        opt.endNum.html(that.end);
        // 改變數據條數
        opt.changeList.on('change', function(e) {
            var _this = this;
            that.pageNumber = 1;
            that.changeNumber(that, _this, opt);

        });
        // 上一頁
        opt.paginationList.find("li.prev").on('click', function() {
            var _this = this;
            $(_this).siblings().removeClass('active');
            $(_this).removeClass("active");
            if (that.pageNumber == 1) {
                that.pageNumber = that.totalPages;
            } else {
                that.pageNumber--;
            }
            that.loadPageData(opt, that);
            var page = opt.paginationList.find("li.page");
            for (let i = 0; i < page.length; i++) {
                if (i == that.pageNumber - 1) {
                    $(page[i]).addClass("active");
                }
            }
            that.showTableData(opt);
        });
        // 下一頁
        opt.paginationList.find("li.next").on('click', function() {
            var _this = this;
            $(_this).siblings('li').removeClass("active");
            $(_this).removeClass("active");
            if (that.pageNumber == that.totalPages) {
                that.pageNumber = 1;
            } else {
                that.pageNumber++;
            }
            that.loadPageData(opt, that);
            var page = opt.paginationList.find("li.page");
            for (let i = 0; i < page.length; i++) {
                if (i == that.pageNumber - 1) {
                    $(page[i]).addClass("active");
                }
            }
            that.showTableData(opt);
        });

    },
    // 改變開始位置寒屯,結束位置
    loadPageData: function(opt, that) {
        that.start = (that.pageNumber - 1) * that.pageSize + 1;
        that.end = that.pageNumber * that.pageSize;
        if (that.end > that.total) {
            that.end = that.total;
        }
        opt.startNum.html(that.start);
        opt.endNum.html(that.end);
    },
    // 計算頁碼數量
    calculateNumberList: function(opt) {
        var that = this;
        // 計算總的頁數 = 數據總條數 / 每頁顯示的條數
        that.totalPages = Math.ceil(that.total / that.pageSize);
        // 創(chuàng)建頁數標簽
        opt.paginationList.find("li.page").remove();
        if (that.pageSize >= that.total || that.data.length <= 1) {
            opt.paginationList.hide();
        } else {
            opt.paginationList.show();
            for (let i = 0; i < that.totalPages; i++) {
                opt.next.before("<li class='page'><span>" + parseInt(i + 1) + "</span></li>");
            }
            opt.paginationList
                .find("li:nth-child(2)")
                .addClass("active");
        }
        // 點擊頁碼改變數據
        opt.paginationList.find("li.page").on("click", function() {
            var _this = this;
            var _this = this;
            $(_this)
                .siblings()
                .removeClass("active");
            $(_this).addClass("active");
            that.pageNumber = parseInt(
                $(_this)
                .find("span")
                .text()
            );
            that.loadPageData(opt, that);
            that.showTableData(opt);

        });
    },
    // 展示表格數據
    showTableData: function(opt) {
        var arr = [],
            that = this,
            len;
        arr = that.data.slice(that.start - 1, that.end);
        that.initTable(opt, arr);

    },
    initTable(opt, arr) {
        opt.table
            .find("tbody")
            .find("tr")
            .remove();
        arr.forEach(function(item, index) {
            var oTr = $("<tr></tr>");
            for (let key in item) {
                oTr.append("<td>" + item[key] + "</td>");
            }
            opt.table.find("tbody").append(oTr);
        });
    },
    changeNumber: function(that, target, opt) {
        that.pageSize = parseInt($(target)
            .find("option:selected")
            .text());
        if (that.pageSize >= that.total) {
            that.pageSize = that.total
            that.start = 1;
            that.pageNumber = 1
        }
        that.loadPageData(opt, that);
        that.calculateNumberList(opt);
        that.showTableData(opt);
    }
};

window.onload = function() {
    var option = {
        startNum: $(".start-num"),
        endNum: $(".end-num"),
        totalNum: $(".total"),
        changeList: $("#changeNumber"),
        paginationList: $("#paginationList"),
        prev: $(".prev"),
        next: $(".next"),
        table: $('#table')
    };
    pagination.init(option);
};

數據

案例數據如下:

var data = [{
        item1: 1,
        item2: 3,
        item3: 4
    },
    {
        item1: 4,
        item2: 5,
        item3: 6
    },
    {
        item1: 7,
        item2: 8,
        item3: 9
    },
    {
        item1: 10,
        item2: 11,
        item3: 12
    },
    {
        item1: 13,
        item2: 14,
        item3: 15
    },
    {
        item1: 16,
        item2: 17,
        item3: 18
    },
    {
        item1: 19,
        item2: 20,
        item3: 21
    },
    {
        item1: 22,
        item2: 23,
        item3: 24
    },
    {
        item1: 25,
        item2: 26,
        item3: 27
    },
    {
        item1: 27,
        item2: 29,
        item3: 30
    },
    {
        item1: 31,
        item2: 32,
        item3: 33
    },
    {
        item1: 34,
        item2: 35,
        item3: 36
    },
    {
        item1: 37,
        item2: 38,
        item3: 39
    },
    {
        item1: 40,
        item2: 41,
        item3: 42
    },
    {
        item1: 43,
        item2: 44,
        item3: 45
    },
    {
        item1: 46,
        item2: 47,
        item3: 48
    },
    {
        item1: 49,
        item2: 50,
        item3: 51
    },
    {
        item1: 52,
        item2: 53,
        item3: 54
    },
    {
        item1: 55,
        item2: 56,
        item3: 57
    },
    {
        item1: 58,
        item2: 59,
        item3: 60
    },
    {
        item1: 61,
        item2: 62,
        item3: 63
    },
    {
        item1: 64,
        item2: 65,
        item3: 66
    },
    {
        item1: 67,
        item2: 68,
        item3: 69
    },
    {
        item1: 7,
        item2: 8,
        item3: 9
    },
    {
        item1: 1,
        item2: 3,
        item3: 4
    },
    {
        item1: 4,
        item2: 5,
        item3: 6
    },
    {
        item1: 7,
        item2: 8,
        item3: 9
    },
    {
        item1: 1,
        item2: 3,
        item3: 4
    },
    {
        item1: 4,
        item2: 5,
        item3: 6
    },
    {
        item1: 7,
        item2: 8,
        item3: 9
    },
    {
        item1: 1,
        item2: 3,
        item3: 4
    },
    {
        item1: 4,
        item2: 5,
        item3: 6
    },
    {
        item1: 7,
        item2: 8,
        item3: 9
    },
    {
        item1: 1,
        item2: 3,
        item3: 4
    },
    {
        item1: 4,
        item2: 5,
        item3: 6
    },
    {
        item1: 7,
        item2: 8,
        item3: 9
    }
];

效果

pagination.GIF
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末荐捻,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子寡夹,更是在濱河造成了極大的恐慌处面,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件菩掏,死亡現場離奇詭異魂角,居然都是意外死亡,警方通過查閱死者的電腦和手機智绸,發(fā)現死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門野揪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人瞧栗,你說我怎么就攤上這事斯稳。” “怎么了沼溜?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵平挑,是天一觀的道長。 經常有香客問我系草,道長通熄,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任找都,我火速辦了婚禮唇辨,結果婚禮上,老公的妹妹穿的比我還像新娘能耻。我一直安慰自己赏枚,他們只是感情好亡驰,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著饿幅,像睡著了一般凡辱。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上栗恩,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天透乾,我揣著相機與錄音,去河邊找鬼磕秤。 笑死乳乌,一個胖子當著我的面吹牛,可吹牛的內容都是我干的市咆。 我是一名探鬼主播汉操,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼蒙兰!你這毒婦竟也來了磷瘤?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤癞己,失蹤者是張志新(化名)和其女友劉穎膀斋,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體痹雅,經...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡仰担,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了绩社。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片摔蓝。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖愉耙,靈堂內的尸體忽然破棺而出贮尉,到底是詐尸還是另有隱情,我是刑警寧澤朴沿,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布猜谚,位于F島的核電站,受9級特大地震影響赌渣,放射性物質發(fā)生泄漏魏铅。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一坚芜、第九天 我趴在偏房一處隱蔽的房頂上張望览芳。 院中可真熱鬧,春花似錦鸿竖、人聲如沸沧竟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽悟泵。三九已至杈笔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間糕非,已是汗流浹背桩撮。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留峰弹,地道東北人。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓芜果,卻偏偏與公主長得像鞠呈,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子右钾,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內容

  • 一蚁吝、文本框為字符型 必填項非空校驗: 1、必填項未輸入--程序應提示錯誤舀射; 2窘茁、必填項只輸入若干個空格,未輸入其它...
    許小小晴閱讀 4,631評論 0 2
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案脆烟? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標簽默認的外補...
    _Yfling閱讀 13,754評論 1 92
  • 測試最重要的是測試思路山林、測試策略和測試計劃,只有這些前期工作做足邢羔,后面的測試執(zhí)行才能針對整體的產品測試起到事半功倍...
    雷哥說閱讀 1,460評論 0 8
  • Web網站測試流程和方法(轉載) 1測試流程與方法 1.1測試流程 進行正式測試之前驼抹,應先確定如何開展測試,不可盲...
    夏了夏夏夏天閱讀 1,297評論 0 0
  • JavaEE三層架構實現ajax分頁查詢 開發(fā)環(huán)境: jdk版本:1.7.0 32位 系統(tǒng) window10 ID...
    359afe052eac閱讀 4,678評論 8 22