佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > 前后端結(jié)合實(shí)現(xiàn)amazeUI分頁效果

前后端結(jié)合實(shí)現(xiàn)amazeUI分頁效果

熱門標(biāo)簽:阿里云 美圖手機(jī) 使用U盤裝系統(tǒng) 百度競價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 網(wǎng)站建設(shè) 智能手機(jī) 硅谷的囚徒呼叫中心 檢查注冊表項(xiàng)

前后端結(jié)合實(shí)現(xiàn)amazeUI分頁,代碼如下所示;

借鑒

本文在博客https://blog.csdn.net/brave_coder/article/details/52367124的基礎(chǔ)上實(shí)現(xiàn)的,非常感謝大佬的分享。

前端實(shí)現(xiàn)

1、引入paginator.js

(function ($) {
    $.fn.paginator = function (options) {
        //this指向當(dāng)前的選擇器
        var config = {
            url: "",
            pageParent: "",
            totalBars: -1,
            limit: -1,
            offset: 1,
            callback: null
        }
        //合并參數(shù)
        var opts = $.extend(config, options);
 
        opts.totalBars = Math.ceil(opts.totalBars / opts.limit);
        //計(jì)算按鈕的總個(gè)數(shù)
 
        //獲取offset參數(shù)
        var queryString = function (url) {
            var offset = (url.split("?")[1]).split("=")[1];
            return parseInt(offset);
        }
 
        //ajax核心方法,用于分頁的數(shù)據(jù)操作
        var ajaxCore = function (offset, fn) {
            $.ajax({
                "url": opts.url,
                "data": {
                    "offset": offset,
                    "limit": opts.limit
                },
                "dataType": "JSON",
                "method": "POST",
                "success": fn
            });
        }
 
        //重新裝配分頁按鈕
        var pageCore = function (offset) {
            if (opts.offset == offset) {
                return;
            } //如果是當(dāng)前頁面,那么就什么事都不用干了!
            else {
                ajaxCore(offset, opts.callback);
                $(opts.pageParent).empty();
                //否則,清空所有的節(jié)點(diǎn),重新向DOM插入新的分頁按鈕
                var output = "";
                var nextBar = offset == opts.totalBars ? "<li class=\&;am-disabled\&;><a yxhref=\&;javascript:;\&;>»</a></li>" : "<li><a yxhref=\&;" + opts.url + (offset + 1) + "\&;>»</a></li>";
                var preBar = offset == 1 ? "<li class=\&;am-disabled\&;><a yxhref=\&;javascript:;\&;>«</a></li>" : "<li><a yxhref=\&;" + opts.url + (offset - 1) + "\&;>«</a></li>";
                //組裝向上一個(gè)節(jié)點(diǎn)和下一頁節(jié)點(diǎn)
                if (opts.totalBars > 7) {
                    if (offset < 5) {
                        output += preBar;
                        for (var i = 1; i <= 5; i++) {
                            if (i == offset) {
                                output += "<li class=\&;am-active\&;><a yxhref=\&;" + opts.url + offset + "\&;>" + offset + "</a></li>";
                            } else {
                                output += "<li><a yxhref=\&;" + opts.url + i + "\&;>" + i + "</a></li>";
                            }
                        }
                        output += "<li><span>...</span></li>";
                        output += "<li><a yxhref=\&;" + opts.url + (opts.totalBars) + "\&;>" + (opts.totalBars) + "</a></li>" + nextBar;
                    } else if (offset >= 5 && offset <= opts.totalBars - 4) {
                        //當(dāng)頁面大于7個(gè)的時(shí)候,那么在第五個(gè)和倒數(shù)第五個(gè)時(shí),執(zhí)行
                        output += preBar;
                        output += "<li><a yxhref=\&;" + opts.url + 1 + "\&;>" + 1 + "</a></li>";
                        //第一個(gè)
                        output += "<li><span>...</span></li>"; //省略號(hào)
 
                        output += "<li><a yxhref=\&;" + opts.url + (offset - 1) + "\&;>" + (offset - 1) + "</a></li>";
 
                        output += "<li class=\&;am-active\&;><a  yxhref=\&;" + opts.url + offset + "\&;>" + offset + "</a></li>";
 
                        output += "<li><a yxhref=\&;" + opts.url + (offset + 1) + "\&;>" + (offset + 1) + "</a></li>";
 
                        output += "<li><span>...</span></li>"; //省略號(hào);
 
                        output += "<li><a yxhref=\&;" + opts.url + (opts.totalBars) + "\&;>" + (opts.totalBars) + "</a></li>"; //尾頁
 
                        output += nextBar;
 
                    } else if (offset > opts.totalBars - 4 && offset <= opts.totalBars) {
                        //當(dāng)頁面位于倒數(shù)第四個(gè)時(shí)候
                        output += preBar;
                        output += "<li><a yxhref=\&;" + opts.url + 1 + "\&;>" + 1 + "</a></li>" + "<li><span>...</span></li>";
 
                        for (var j = 4; j >= 0; j--) {
                            if (opts.totalBars - j == offset) {
                                output += "<li class=\&;am-active\&;><a yxhref=\&;" + opts.url + (opts.totalBars - j) + "\&;>" + (opts.totalBars - j) + "</a></li>";
                            } else {
                                output += "<li><a yxhref=\&;" + opts.url + (opts.totalBars - j) + "\&;>" + (opts.totalBars - j) + "</a></li>";
                            }
                        }
                        output += nextBar;
                    } else {
                        console.log("分頁數(shù)據(jù)出錯(cuò)!");
                        return;
                    }
                } else {
                    output += preBar;
                    for (var i = 1; i <= opts.totalBars; i++) {
                        if (i == offset) {
                            output += "<li class=\&;am-active\&;><a yxhref=\&;" + opts.url + offset + "\&;>" + offset+ "</a></li>";
                        } else {
                            output += "<li><a yxhref=\&;" + opts.url + i + "\&;>" + i+ "</a></li>";
                        }
                    }
                    output += nextBar;
                }
                $(opts.pageParent).append(output);
                opts.offset = offset; //將偏移量賦值給config里面的offset
            }
        }
 
        //清理函數(shù),防止多綁定事件和重新計(jì)算分頁
        var clear = function () {
            $(opts.pageParent).empty().undelegate();
        }
 
 
        //初始化裝配分頁按鈕
        var init = function (fn) {
            if (typeof (fn) != "function") {
                console.log("將不能正確的執(zhí)行回調(diào)函數(shù)");
            } else {
                opts.callback = fn;
            }
            clear();
            ajaxCore(1, opts.callback);//執(zhí)行初始化ajax方法
            var preBar = "<li class=\&;am-disabled\&;><a yxhref=\&;javascript:;\&;>«</a></li>";
            //上一頁,(禁用的效果)
            //如果只有一頁,那么禁用下一頁
            var nextBar = opts.totalBars > 1 ? "<li><a yxhref=\&;" + opts.url + 2 + "\&;>»</a></li>" : "<li class=\&;am-disabled\&;><a yxhref=\&;javascript:;\&;>»</a></li>";
            //最后一頁
            var output = "<li class=\&;am-active\&;><a yxhref=\&;" + opts.url + 1 + "\&;>1</a></li>";
 
            if (opts.totalBars <= 7) {
                for (var i = 1; i < opts.totalBars; i++) {
                    output += "<li><a yxhref=\&;" + opts.url + (i + 1) + "\&;>" + (i + 1) + "</a></li>";
                }
            } else {
                for (var j = 1; j < 5; j++) {
                    output += "<li><a yxhref=\&;" + opts.url + (j + 1) + "\&;>" + (j + 1) + "</a></li>";
                }
                output += "<li><span>...</span></li>";
                output += "<li><a yxhref=\&;" + opts.url + (opts.totalBars) + "\&;>" + (opts.totalBars) + "</a></li>";
            }
            $(opts.pageParent).delegate("a","click", function () {
                var offset = queryString($(this).attr("yxhref"));
                console.log("ok");
                pageCore(offset);
            });
            $(opts.pageParent).append(preBar + output + nextBar);
        };
        init(opts.callback);//初始化分頁引擎
    }
}(window.jQuery))

2、獲取總頁數(shù),再獲取分頁

$.ajax({
        type: "GET",
        url: selectSendNumberNumsByContURL,//獲取總數(shù)
        data: {},
        dataType: "json",
        success: function(data){

            if (data[0].code == 200) {

                $("#paginator").paginator({
                    url: selectSendNumberByContURL + "?offsets=",
                    pageParent: "#paginator",
                    totalBars: data[0].allNums,
                    limit: 10,
                    offset: 1,
                    callback: function (data1) {

                        //清空DOM節(jié)點(diǎn)
                        
                        //動(dòng)態(tài)加dom節(jié)點(diǎn)
                    }
                });
            }else{

            }
        },
        error: function (err) {

        }
    });

后端實(shí)現(xiàn)(分頁)

這里是controller,拿到offset(第幾頁)參數(shù)、limit(每頁多少數(shù)量),再寫SQL實(shí)現(xiàn)分頁就好了。

@RequestMapping(value = "/selectNumberCheckByCont", method = RequestMethod.POST)
    @ResponseBody
    public List<ReturnUtils> selectNumberCheckByCont(HttpServletRequest request,
                                                     HttpServletResponse response) throws Exception {

        //統(tǒng)一設(shè)置返回?cái)?shù)據(jù)格式
        response.setContentType("application/json");
        response.setHeader("Pragma", "no-cache");
        response.setCharacterEncoding("UTF-8");

        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");

        List<ReturnUtils> list = iNumberCheckService.selectNumberCheckByCont(offset, limit);

        return list;
    }

總結(jié)

到此這篇關(guān)于前后端結(jié)合實(shí)現(xiàn)amazeUI分頁的文章就介紹到這了,更多相關(guān)amazeUI分頁內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

標(biāo)簽:通遼 煙臺(tái) 賀州 湖北 湘潭 懷化 黃山 山南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《前后端結(jié)合實(shí)現(xiàn)amazeUI分頁效果》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    长泰县| 文昌市| 鄂温| 宁南县| 慈利县| 南郑县| 伊通| 林周县| 祁东县| 高陵县| 无锡市| 东莞市| 崇左市| 思南县| 尼木县| 内黄县| 灵寿县| 大丰市| 兴化市| 桑日县| 靖宇县| 桃江县| 鲁甸县| 枣强县| 河西区| 霞浦县| 双柏县| 屯留县| 苍山县| 永兴县| 台南市| 文登市| 沂水县| 进贤县| 临沭县| 新竹县| 新邵县| 根河市| 开原市| 宝丰县| 长治县|