tianyunperfect vor 3 Jahren
Ursprung
Commit
0b77f3b291
3 geänderte Dateien mit 327 neuen und 8 gelöschten Zeilen
  1. 27 8
      simple-demo/index.html
  2. 1 0
      simple-demo/simple-demo.iml
  3. 299 0
      tmp/pages.js

+ 27 - 8
simple-demo/index.html

@@ -6,11 +6,13 @@
     <style>
     </style>
     <link rel="stylesheet" href="../tmp/bootstrap.min.css">
+    <script src="../tmp/jquery.slim.min.js"></script>
     <script src="../tmp/vue.min.js"></script>
+    <script src="../tmp/pages.js"></script>
 </head>
 <body>
 <div class="container">
-    <div class="col-md-6 col-md-offset-3">
+    <div class="col-md-12 col-md-offset-3">
         <h1>Vue demo</h1>
 
         <div id="app">
@@ -40,8 +42,8 @@
 
                 </tr>
                 </tbody>
-
             </table>
+            <div id="pageDiv"></div>
 
             <!--  添加书籍   -->
             <div id="add-book">
@@ -52,7 +54,9 @@
                         请输入查询书名:
                     </div>
                     <div class="col-md-5" style="text-align: left;">
-                        <input type="text" class="form-control" v-model="search"></input>
+                        <label>
+                            <input type="text" class="form-control" v-model="search"/>
+                        </label>
                     </div>
 
                 </div>
@@ -83,9 +87,7 @@
 </div>
 
 <script>
-    var id = 0;
-    var vm = new Vue({
-
+    const vm = new Vue({
         el: "#app",
         // 计算函数 动态变化的数据
         data: {
@@ -126,6 +128,23 @@
                 this.isUpdate = newValue !== 0;
             }
         },
+        mounted: () => {
+            $('#pageDiv').pagination({
+                pages: 50,  // 页数
+                edges: 2, // 边缘页数,不用修改
+                cssStyle: 'pagination',
+                displayedPages: 5, // 展示相连页数,不用修改
+                onPageClick: function (pageNumber, event) {
+                    //点击时调用
+                    // alert(pageNumber);
+                },
+                onInit: function (getid) {
+                    //刷新时调用
+                    // alert(getid);
+                }
+
+            });
+        },
         methods: {     //   写函数
 
             addBook: function () {
@@ -176,8 +195,8 @@
         },
 
 
-    })
+    });
 </script>
 
 </body>
-</html>
+</html>

+ 1 - 0
simple-demo/simple-demo.iml

@@ -3,6 +3,7 @@
   <component name="NewModuleRootManager" inherit-compiler-output="true">
     <exclude-output />
     <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
     <orderEntry type="sourceFolder" forTests="false" />
   </component>
 </module>

+ 299 - 0
tmp/pages.js

@@ -0,0 +1,299 @@
+(function ($) {
+//修改地址id
+    function pageurl(id) {
+        var urlSearch = location.href;
+        var unformatStr = changeURLArg(urlSearch, "page", id);
+        if (!!(window.history && history.pushState)) {
+            history.replaceState(null, "", unformatStr);
+        }
+    }
+
+    function changeURLArg(url, arg, arg_val) {
+        var pattern = arg + '=([^&]*)';
+        var replaceText = arg + '=' + arg_val;
+        if (url.match(pattern)) {
+            var tmp = '/(' + arg + '=)([^&]*)/gi';
+            tmp = url.replace(eval(tmp), replaceText);
+            return tmp;
+        } else {
+            if (url.match('[\?]')) {
+                return url + '&' + replaceText;
+            } else {
+                return url + '?' + replaceText;
+            }
+        }
+    }
+
+    function getUrlParms(name) {
+        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
+        var r = window.location.search.substr(1).match(reg);
+        if (r != null)
+            return unescape(r[2]);
+        return "1";
+    }
+
+//获取地址id
+    var getid = getUrlParms("page");
+
+
+    var methods = {
+        init: function (options) {
+            var o = $.extend({
+                items: 1,
+                itemsOnPage: 1,
+                pages: 0,
+                displayedPages: 5,
+                edges: '',
+                currentPage: getid,
+                prevText: '上一页',
+                nextText: '下一页',
+                ellipseText: '…',
+                cssStyle: '',
+                labelMap: [],
+                selectOnClick: true,
+                onPageClick: function (pageNumber, event) {
+                    //点击加载
+                },
+                onInit: function (getid) {
+                    //初始页面
+                }
+            }, options || {});
+
+
+            var self = this;
+
+            o.pages = o.pages ? o.pages : Math.ceil(o.items / o.itemsOnPage) ? Math.ceil(o.items / o.itemsOnPage) : 1;
+            o.currentPage = o.currentPage - 1;
+            o.halfDisplayed = o.displayedPages / 2;
+
+            this.each(function () {
+                self.addClass(o.cssStyle + ' simple-pagination').data('pagination', o);
+                methods._draw.call(self);
+            });
+
+            o.onInit(getid);
+
+            return this;
+        },
+
+        selectPage: function (page) {
+            methods._selectPage.call(this, page - 1);
+            return this;
+        },
+
+        prevPage: function () {
+            var o = this.data('pagination');
+            if (o.currentPage > 0) {
+                methods._selectPage.call(this, o.currentPage - 1);
+            }
+            return this;
+        },
+
+        nextPage: function () {
+            var o = this.data('pagination');
+            if (o.currentPage < o.pages - 1) {
+                methods._selectPage.call(this, o.currentPage + 1);
+            }
+            return this;
+        },
+
+        getPagesCount: function () {
+            return this.data('pagination').pages;
+        },
+
+        getCurrentPage: function () {
+            return this.data('pagination').currentPage + 1;
+        },
+
+        destroy: function () {
+            this.empty();
+            return this;
+        },
+
+        drawPage: function (page) {
+            var o = this.data('pagination');
+            o.currentPage = page - 1;
+            this.data('pagination', o);
+            methods._draw.call(this);
+            return this;
+        },
+
+        redraw: function () {
+            methods._draw.call(this);
+            return this;
+        },
+
+        disable: function () {
+            var o = this.data('pagination');
+            o.disabled = true;
+            this.data('pagination', o);
+            methods._draw.call(this);
+            return this;
+        },
+
+        enable: function () {
+            var o = this.data('pagination');
+            o.disabled = false;
+            this.data('pagination', o);
+            methods._draw.call(this);
+            return this;
+        },
+
+        updateItems: function (newItems) {
+            var o = this.data('pagination');
+            o.items = newItems;
+            o.pages = methods._getPages(o);
+            this.data('pagination', o);
+            methods._draw.call(this);
+        },
+
+        updateItemsOnPage: function (itemsOnPage) {
+            var o = this.data('pagination');
+            o.itemsOnPage = itemsOnPage;
+            o.pages = methods._getPages(o);
+            this.data('pagination', o);
+            methods._selectPage.call(this, 0);
+            return this;
+        },
+
+        _draw: function () {
+            var o = this.data('pagination'),
+                interval = methods._getInterval(o),
+                i,
+                tagName;
+
+            methods.destroy.call(this);
+
+            tagName = (typeof this.prop === 'function') ? this.prop('tagName') : this.attr('tagName');
+
+            var $panel = tagName === 'UL' ? this : $('<ul class="pagination"></ul>').appendTo(this);
+
+            // Generate Prev link
+            if (o.prevText) {
+                methods._appendItem.call(this, o.currentPage - 1, {text: o.prevText, classes: 'prev'});
+            }
+
+            // Generate start edges
+            if (interval.start > 0 && o.edges > 0) {
+                var end = Math.min(o.edges, interval.start);
+                for (i = 0; i < end; i++) {
+                    methods._appendItem.call(this, i);
+                }
+                if (o.edges < interval.start && (interval.start - o.edges !== 1)) {
+                    $panel.append('<li class="page-item disabled"><span class="page-link">' + o.ellipseText + '</span></li>');
+                } else if (interval.start - o.edges === 1) {
+                    methods._appendItem.call(this, o.edges);
+                }
+            }
+
+            // Generate interval links
+            for (i = interval.start; i < interval.end; i++) {
+                methods._appendItem.call(this, i);
+            }
+
+            // Generate end edges
+            if (interval.end < o.pages && o.edges > 0) {
+                if (o.pages - o.edges > interval.end && (o.pages - o.edges - interval.end !== 1)) {
+                    $panel.append('<li class="page-item disabled"><span class="page-link">' + o.ellipseText + '</span></li>');
+                } else if (o.pages - o.edges - interval.end === 1) {
+                    methods._appendItem.call(this, interval.end++);
+                }
+                var begin = Math.max(o.pages - o.edges, interval.end);
+                for (i = begin; i < o.pages; i++) {
+                    methods._appendItem.call(this, i);
+                }
+            }
+
+            // Generate Next link
+            if (o.nextText) {
+                methods._appendItem.call(this, o.currentPage + 1, {text: o.nextText, classes: 'next'});
+            }
+        },
+
+        _getPages: function (o) {
+            var pages = Math.ceil(o.items / o.itemsOnPage);
+            return pages || 1;
+        },
+
+        _getInterval: function (o) {
+            return {
+                start: Math.ceil(o.currentPage > o.halfDisplayed ? Math.max(Math.min(o.currentPage - o.halfDisplayed, (o.pages - o.displayedPages)), 0) : 0),
+                end: Math.ceil(o.currentPage > o.halfDisplayed ? Math.min(o.currentPage + o.halfDisplayed, o.pages) : Math.min(o.displayedPages, o.pages))
+            };
+        },
+
+        _appendItem: function (pageIndex, opts) {
+            var self = this, options, $link, o = self.data('pagination'),
+                $linkWrapper = $('<li class="page-item"></li>'), $ul = self.find('ul');
+            pageIndex = pageIndex < 0 ? 0 : (pageIndex < o.pages ? pageIndex : o.pages - 1);
+
+            options = {
+                text: pageIndex + 1,
+                classes: ''
+            };
+
+            if (o.labelMap.length && o.labelMap[pageIndex]) {
+                options.text = o.labelMap[pageIndex];
+            }
+
+            options = $.extend(options, opts || {});
+
+            if (pageIndex === o.currentPage || o.disabled) {
+                if (o.disabled) {
+                    $linkWrapper.addClass('disabled');
+                } else if (options.classes !== "prev" && options.classes !== "next"){
+                    $linkWrapper.addClass('active');
+                }
+                $link = $('<span class="page-link">' + (options.text) + '</span>');
+            } else {
+
+                $link = $('<span class="page-link">' + (options.text) + '</span>');
+
+                $link.click(function (event) {
+                    return methods._selectPage.call(self, pageIndex, event);
+                });
+            }
+
+            if (options.classes) {
+                $link.addClass(options.classes);
+            }
+
+            $linkWrapper.append($link);
+
+            if ($ul.length) {
+                $ul.append($linkWrapper);
+            } else {
+                self.append($linkWrapper);
+            }
+        },
+
+        _selectPage: function (pageIndex, event) {
+            var o = this.data('pagination');
+            o.currentPage = pageIndex;
+            if (o.selectOnClick) {
+                methods._draw.call(this);
+            }
+
+            pageurl(pageIndex + 1);
+            return o.onPageClick(pageIndex + 1, event);
+        }
+
+    };
+
+    $.fn.pagination = function (method) {
+
+        // Method calling logic
+        if (methods[method] && method.charAt(0) !== '_') {
+            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
+        } else if (typeof method === 'object' || !method) {
+            return methods.init.apply(this, arguments);
+        } else {
+            $.error('Method ' + method + ' does not exist on jQuery.pagination');
+        }
+
+    };
+
+})(jQuery);
+
+
+/* www.jq22.com */