• 前端bootstrap分页+mybatis+PageHelper分页


    实现的效果样式

    一、bootstrap分页插件,bootstrap-paginator.js里面的内容是我自己修改之后的,可以再网上下载一个版本修改成自己的样式

    /**
     * bootstrap-paginator.js v1.0
     * --
     * Copyright 2013 Yun Lai <lyonlai1984@gmail.com>
     * --
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     * 2020.3.17  页面只显示上页和下页,没有首页和末页,样式以及方法有修改
     */
    
    (function ($) {
    
        "use strict"; // jshint ;_;
    
    
        /* Paginator PUBLIC CLASS DEFINITION
         * ================================= */
    
        /**
         * Boostrap Paginator Constructor
         *
         * @param element element of the paginator
         * @param options the options to config the paginator
         *
         * */
        var BootstrapPaginator = function (element, options) {
            this.init(element, options);
        },
            old = null;
    
        BootstrapPaginator.prototype = {
    
            /**
             * Initialization function of the paginator, accepting an element and the options as parameters
             *
             * @param element element of the paginator
             * @param options the options to config the paginator
             *
             * */
            init: function (element, options) {
    
                this.$element = $(element);
    
                var version = (options && options.bootstrapMajorVersion) ? options.bootstrapMajorVersion : $.fn.bootstrapPaginator.defaults.bootstrapMajorVersion,
                    id = this.$element.attr("id");
    
                if (version === 2 && !this.$element.is("div")) {
    
                    throw "in Bootstrap version 2 the pagination must be a div element. Or if you are using Bootstrap pagination 3. Please specify it in bootstrapMajorVersion in the option";
                } else if (version > 2 && !this.$element.is("ul")) {
                    throw "in Bootstrap version 3 the pagination root item must be an ul element."
                }
    
    
    
                this.currentPage = 1;
    
                this.setOptions(options);
    
                this.initialized = true;
            },
    
            /**
             * Update the properties of the paginator element
             *
             * @param options options to config the paginator
             * */
            setOptions: function (options) {
    
                this.options = $.extend({}, (this.options || $.fn.bootstrapPaginator.defaults), options);
    
                this.totalPages = parseInt(this.options.totalPages, 10);  //setup the total pages property.
                this.numberOfPages = parseInt(this.options.numberOfPages, 10); //setup the numberOfPages to be shown
    
                //move the set current page after the setting of total pages. otherwise it will cause out of page exception.
                if (options && typeof (options.currentPage)  !== 'undefined') {
    
                    this.setCurrentPage(options.currentPage);
                }
    
                this.listen();
    
                //render the paginator
                this.render();
    
                if (!this.initialized && this.lastPage !== this.currentPage) {
                    this.$element.trigger("page-changed", [this.lastPage, this.currentPage]);
                }
    
            },
    
            /**
             * Sets up the events listeners. Currently the pageclicked and pagechanged events are linked if available.
             *
             * */
            listen: function () {
    
                this.$element.off("page-clicked");
    
                this.$element.off("page-changed");// unload the events for the element
    
                if (typeof (this.options.onPageClicked) === "function") {
                    this.$element.bind("page-clicked", this.options.onPageClicked);
                }
    
                if (typeof (this.options.onPageChanged) === "function") {
                    this.$element.on("page-changed", this.options.onPageChanged);
                }
    
                this.$element.bind("page-clicked", this.onPageClicked);
            },
    
    
            /**
             *
             *  Destroys the paginator element, it unload the event first, then empty the content inside.
             *
             * */
            destroy: function () {
    
                this.$element.off("page-clicked");
    
                this.$element.off("page-changed");
    
                this.$element.removeData('bootstrapPaginator');
    
                this.$element.empty();
    
            },
    
            /**
             * Shows the page
             *
             * */
            show: function (page) {
    
                this.setCurrentPage(page);
    
                this.render();
    
            },
    
            /**
             * Shows the next page
             *
             * */
            showNext: function () {
                var pages = this.getPages();
    
                if (pages.next) {
                    this.show(pages.next);
                }
    
            },
    
            /**
             * Shows the previous page
             *
             * */
            showPrevious: function () {
                var pages = this.getPages();
    
                if (pages.prev) {
                    this.show(pages.prev);
                }
    
            },
    
    
            /**
             * Internal on page item click handler, when the page item is clicked, change the current page to the corresponding page and
             * trigger the pageclick event for the listeners.
             *
             *
             * */
            onPageItemClicked: function (event) {
    
                var type = event.data.type,
                    page = event.data.page;
    
                this.$element.trigger("page-clicked", [event, type, page]);
    
            },
    
            onPageClicked: function (event, originalEvent, type, page) {
    
                //show the corresponding page and retrieve the newly built item related to the page clicked before for the event return
    
                var currentTarget = $(event.currentTarget);
    
                switch (type) {
                case "prev":
                    currentTarget.bootstrapPaginator("showPrevious");
                    break;
                case "next":
                    currentTarget.bootstrapPaginator("showNext");
                    break;
                case "page":
                    currentTarget.bootstrapPaginator("show", page);
                    break;
                }
    
            },
    
            /**
             * Renders the paginator according to the internal properties and the settings.
             *
             *
             * */
            render: function () {
    
                //fetch the container class and add them to the container
                var containerClass = this.getValueFromOption(this.options.containerClass, this.$element),
                    size = this.options.size || "normal",
                    alignment = this.options.alignment || "left",
                    pages = this.getPages(),
                    listContainer = this.options.bootstrapMajorVersion === 2 ? $("<ul></ul>") : this.$element,
                    listContainerClass = this.options.bootstrapMajorVersion === 2 ? this.getValueFromOption(this.options.listContainerClass, listContainer) : null,
                    first = null,
                    prev = null,
                    next = null,
                    last = null,
                    p = null,
                    i = 0;
    
    
                this.$element.prop("class", "");
    
                this.$element.addClass("pagination");
    
                switch (size.toLowerCase()) {
                case "large":
                case "small":
                case "mini":
                    this.$element.addClass($.fn.bootstrapPaginator.sizeArray[this.options.bootstrapMajorVersion][size.toLowerCase()]);
                    break;
                default:
                    break;
                }
    
                if (this.options.bootstrapMajorVersion === 2) {
                    switch (alignment.toLowerCase()) {
                    case "center":
                        this.$element.addClass("pagination-centered");
                        break;
                    case "right":
                        this.$element.addClass("pagination-right");
                        break;
                    default:
                        break;
                    }
                }
    
    
                this.$element.addClass(containerClass);
    
                //empty the outter most container then add the listContainer inside.
                this.$element.empty();
    
                if (this.options.bootstrapMajorVersion === 2) {
                    this.$element.append(listContainer);
    
                    listContainer.addClass(listContainerClass);
                }
    
                //update the page element reference
                this.pageRef = [];
    
                if (pages.prev) {//if the there is previous page element
    
                    prev = this.buildPageItem("prev", pages.prev);
    
                    if (prev) {
                        listContainer.append(prev);
                    }else{
                        listContainer.append(this.buildPageItem("up", "up"));
                    }
    
                }
    
                for (i = 0; i < pages.length; i = i + 1) {
                    p = this.buildPageItem("page", pages[i]);
    
                    if (p) {
                        listContainer.append(p);
                    }
                }
    
                if (pages.next) {//if there is next page
                    next = this.buildPageItem("next", pages.next);
                    if (next) {
                        listContainer.append(next);
                    }else{
                        listContainer.append(this.buildPageItem("down", "down"));
                    }
                }
            },
    
            /**
             *
             * Creates a page item base on the type and page number given.
             *
             * @param page page number
             * @param type type of the page, whether it is the first, prev, page, next, last
             *
             * @return Object the constructed page element
             * */
            buildPageItem: function (type, page) {
    
                var itemContainer = $("<li></li>"),//creates the item container
                    itemContent = $("<a></a>"),//creates the item content
                    text = "",
                    title = "",
                    itemContainerClass = this.options.itemContainerClass(type, page, this.currentPage),
                    itemContentClass = this.getValueFromOption(this.options.itemContentClass, type, page, this.currentPage),
                    tooltipOpts = null;
    
                switch (type) {
                case "prev":
                    if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
                    text = this.options.itemTexts(type, page, this.currentPage);
                    title = this.options.tooltipTitles(type, page, this.currentPage);
                    break;
                case "next":
                    if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
                    text = this.options.itemTexts(type, page, this.currentPage);
                    title = this.options.tooltipTitles(type, page, this.currentPage);
                    break;
                case "page":
                    if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
                    text = this.options.itemTexts(type, page, this.currentPage);
                    title = this.options.tooltipTitles(type, page, this.currentPage);
                    break;
                case "up":
                    text = "上页" ;
                    title = "上页";
                    break;
                case "down":
                    text = "下页" ;
                    title = "下页";
                    break;
                }
                /*如果是首页或者下页则不需要加高亮的类*/
                if(title == "上页" ||  title == "下页"){
                    itemContainerClass="";
                }
                itemContainer.addClass(itemContainerClass).append(itemContent);
                itemContent.addClass(itemContentClass).html(text).on("click", null, {type: type, page: page}, $.proxy(this.onPageItemClicked, this));
    
                if (this.options.pageUrl) {
                    itemContent.attr("href", this.getValueFromOption(this.options.pageUrl, type, page, this.currentPage));
                }
    
                if (this.options.useBootstrapTooltip) {
                    tooltipOpts = $.extend({}, this.options.bootstrapTooltipOptions, {title: title});
    
                    itemContent.tooltip(tooltipOpts);
                } else {
                    itemContent.attr("title", title);
                }
    
                return itemContainer;
    
            },
    
            setCurrentPage: function (page) {
                if (page > this.totalPages || page < 1) {// if the current page is out of range, throw exception.
    
                    throw "Page out of range";
    
                }
    
                this.currentPage = parseInt(page, 10);
    
            },
    
            /**
             * Gets an array that represents the current status of the page object. Numeric pages can be access via array mode. length attributes describes how many numeric pages are there. First, previous, next and last page can be accessed via attributes first, prev, next and last. Current attribute marks the current page within the pages.
             *
             * @return object output objects that has first, prev, next, last and also the number of pages in between.
             * */
            getPages: function () {
    
                var totalPages = this.totalPages,// get or calculate the total pages via the total records
                    pageStart = (this.currentPage % this.numberOfPages === 0) ? (parseInt(this.currentPage / this.numberOfPages, 10) - 1) * this.numberOfPages + 1 : parseInt(this.currentPage / this.numberOfPages, 10) * this.numberOfPages + 1,//calculates the start page.
                    output = [],
                    i = 0,
                    counter = 0;
    
                pageStart = pageStart < 1 ? 1 : pageStart;//check the range of the page start to see if its less than 1.
    
                for (i = pageStart, counter = 0; counter < this.numberOfPages && i <= totalPages; i = i + 1, counter = counter + 1) {//fill the pages
                    output.push(i);
                }
    
    
                if (this.currentPage > 1) {// add the previous when the current page leaves the 1st page
                    output.prev = this.currentPage - 1;
                } else {
                    output.prev = 1;
                }
    
                if (this.currentPage < totalPages) {// add the next page when the current page doesn't reach the last page
                    output.next = this.currentPage + 1;
                } else {
                    output.next = totalPages;
                }
    
                output.current = this.currentPage;//mark the current page.
    
                output.total = totalPages;
    
                output.numberOfPages = this.options.numberOfPages;
    
                return output;
    
            },
    
            /**
             * Gets the value from the options, this is made to handle the situation where value is the return value of a function.
             *
             * @return mixed value that depends on the type of parameters, if the given parameter is a function, then the evaluated result is returned. Otherwise the parameter itself will get returned.
             * */
            getValueFromOption: function (value) {
    
                var output = null,
                    args = Array.prototype.slice.call(arguments, 1);
    
                if (typeof value === 'function') {
                    output = value.apply(this, args);
                } else {
                    output = value;
                }
    
                return output;
    
            }
    
        };
    
    
        /* TYPEAHEAD PLUGIN DEFINITION
         * =========================== */
    
        old = $.fn.bootstrapPaginator;
    
        $.fn.bootstrapPaginator = function (option) {
    
            var args = arguments,
                result = null;
    
            $(this).each(function (index, item) {
                var $this = $(item),
                    data = $this.data('bootstrapPaginator'),
                    options = (typeof option !== 'object') ? null : option;
    
                if (!data) {
                    data = new BootstrapPaginator(this, options);
    
                    $this = $(data.$element);
    
                    $this.data('bootstrapPaginator', data);
    
                    return;
                }
    
                if (typeof option === 'string') {
    
                    if (data[option]) {
                        result = data[option].apply(data, Array.prototype.slice.call(args, 1));
                    } else {
                        throw "Method " + option + " does not exist";
                    }
    
                } else {
                    result = data.setOptions(option);
                }
            });
    
            return result;
    
        };
    
        $.fn.bootstrapPaginator.sizeArray = {
    
            "2": {
                "large": "pagination-large",
                "small": "pagination-small",
                "mini": "pagination-mini"
            },
            "3": {
                "large": "pagination-lg",
                "small": "pagination-sm",
                "mini": ""
            }
    
        };
    
        $.fn.bootstrapPaginator.defaults = {
            containerClass: "",
            size: "normal",
            alignment: "center",
            bootstrapMajorVersion: 2,
            listContainerClass: "",
            itemContainerClass: function (type, page, current) {
                return (page === current) ? "active" : "";
            },
            itemContentClass: function (type, page, current) {
                return "";
            },
            currentPage: 1,
            numberOfPages: 5,
            totalPages: 1,
            pageUrl: function (type, page, current) {
                return null;
            },
            onPageClicked: null,
            onPageChanged: null,
            useBootstrapTooltip: false,
            shouldShowPage: function (type, page, current) {
                var result = true;
    
                switch (type) {
                case "prev":
                    result = (current !== 1);
                    break;
                case "next":
                    result = (current !== this.totalPages);
                    break;
                case "page":
                    result = true;
                    break;
                }
    
                return result;
    
            },
            itemTexts: function (type, page, current) {
                switch (type) {
                case "prev":
                    return "上页";
                case "next":
                    return "下页";
                case "page":
                    return page;
                }
            },
            tooltipTitles: function (type, page, current) {
    
                switch (type) {
                case "prev":
                    return "上页";
                case "next":
                    return "下页";
                case "page":
                    return (page === current) ? "Current page is " + page : "Go to page " + page;
                }
            },
            bootstrapTooltipOptions: {
                animation: true,
                html: true,
                placement: 'top',
                selector: false,
                title: "",
                container: false
            }
        };
    
        $.fn.bootstrapPaginator.Constructor = BootstrapPaginator;
    
    
    
    }(window.jQuery));

    二、前端代码

    1、html代码

    /* 高亮显示*/
    #pageLimit .active a{
    background-color: #42a5f5;
    border-color: #42a5f5;
    color:#fff;
    }
    <!--右侧是查询出来的文件列表 -->
            <div id="right-div">
                <ul class="law-content-con" id="showList">
                </ul>
                <!--分页组件 -->
                <div align="center">
                    <ul id="pageLimit" style="padding:30px 260px;"></ul>
                </div>
            </div>

    2、js文件

        init:function() {
            // 总条数
            var total;
            // 每页显示数量
            var pageSize=10;
            // 总页数
            var pages;
            // 显示页数
            var numberOfPages;
            // 关键字
            var keyName=window.localStorage.getItem("keyName");
             $.ajax({
                type: "post",
                cache: false,
                url: backbasePath+'/apia/v1/file/knowledgeList',
                dataType:"json",
                data:{// 当前的页数
                    currentPageName:1,
                    // 每页显示的条数
                    pageSizeName:pageSize,
                },
                async: true,
                success: function(data) {
                    if('000000'==data.code){
                        // 获取数据信息
                        var infoJson=data.data;
                        // 获取总共的条数
                        total=data.total;
                        pages=Math.ceil(total/pageSize);
                        if(total > 0 ){
                            if(pages < numberOfPages){
                                numberOfPages=pages;
                            }
                            // 插件的各项设置
                            var options = {
                                // 当前的请求页面
                                currentPage:1,
                                totalPages:pages,
                                bootstrapMajorVersion:3,
                                size:"normal",
                                alignment:"center",
                                itemTexts: function (type, page, current) {
                                    switch (type) {
                                        case "prev":
                                            return "上页";
                                        case "next":
                                            return "下页";
                                        case "page":
                                            return page;
                                    }
                                },
                                onPageClicked: function(event, originalEvent, type, page){
                                    if(page=="up" || page=="down"){
                                        return;
                                    }else{
                                        $.post(backbasePath+'/apia/v1/file/knowledgeList',
                                            {
                                                currentPageName:page,
                                                // 每页显示的条数
                                                pageSizeName:pageSize,
                                            },
                                            function (map) {
                                                var infoJson = map.data;
                                                knowledgeList.show(infoJson);
                                            },
                                            "json"
                                        );
                                    }
                                }
                            }
                            $('#pageLimit').bootstrapPaginator(options);
                        }
                        knowledgeList.show(infoJson);
                    } else {
                        toastr.error(data.msg);
                    }
                },
                error: function () {
                    toastr.error('知识库检索失败!!');
                }
          });
        },

    三、后端代码

      // 查询当前人所在部门上传的文件
        public String knowledgeList(HttpServletRequest req){
            String result="";
            try {
                // 封装前台参数为map
                Map<String, Object> m = getMaps(req);
                // 当前页
                int currentPage =Integer.parseInt(req.getParameter("currentPageName"));
                // 每页显示数量
                int pageSize =Integer.parseInt(req.getParameter("pageSizeName"));
                log.info("|" + m + "|");// 获取知识表中数据的个数
                int total = knowledgeDao.knowledgeCount(m);
           // 对应的分页方法,当前页数以及每页数量,limit PageHelper.startPage(currentPage, pageSize); List
    <Map<String, Object>> dataList = knowledgeDao.knowledgeList(m); // 获取的集合列表转换成json格式 String dataListStr = StringTool.listToJson(dataList); StringBuffer sb = new StringBuffer(); sb.append("{"); sb.append("" + getKey("code") + ":" + getKey(RespCode.commonSucc) + ","); sb.append("" + getKey("msg") + ":" + getKey(RespMsg.commonSucc) + ","); sb.append("" + getKey("total") + ":" + total + ","); // 将数据列表放在data参数里面 sb.append("" + getKey("data") + ":" + dataListStr); sb.append("}"); result = sb.toString(); } catch (Exception e) { rollBack(e, log); } log.info("|" + result + "|"); return result; }
  • 相关阅读:
    【JZOJ4244】yi【贪心】
    【JZOJ4244】yi【贪心】
    基本赋值运算符
    自增自减运算符
    字符串的+操作
    算术运算符
    数据类型转换
    键盘输入使用
    基本数据类型长度
    变量
  • 原文地址:https://www.cnblogs.com/flyShare/p/12511201.html
Copyright © 2020-2023  润新知