最近在写官网的分页功能。在网上找了很多案例都太复杂也太重。所以准备写一个简单一点的分页。

需求:把请求到的数据做分页。

准备:使用了网上一个简单的分页插件。

思路:分页相当于tab切换功能。具体实操把数组拆分成若干个数组。这样每个数组就是所需要的每个分页的内容。然后把所有的数组塞到一个对象中就是分页所需要的内容。

上代码

分页插件:

!(function(t, a, e, i) {
var n = function(a, e) {
this.ele = a,
this.defaults = {
currentPage: 1,
totalPage: 10,
isShow: !0,
count: 5,
homePageText: '首页',
endPageText: '尾页',
prevPageText: '上一页',
nextPageText: '下一页',
callback: function() {}
},
this.opts = t.extend({}, this.defaults, e),
this.current = this.opts.currentPage,
this.total = this.opts.totalPage,
this.init();
};
n.prototype = {
init: function() {
this.render(),
this.eventBind();
},
render: function() {
var t = this.opts;
var a = this.current;
var e = this.total;
var i = this.getPagesTpl();
var n = this.ele.empty();
this.isRender = !0,
this.homePage = '<a href="javascript:void(0);" class="ui-pagination-page-item" data-current="1">' + t.homePageText + '</a>',
this.prevPage = '<a href="javascript:void(0);" class="ui-pagination-page-item" data-current="' + (a - 1) + '">' + t.prevPageText + '</a>',
this.nextPage = '<a href="javascript:void(0);" class="ui-pagination-page-item" data-current="' + (a + 1) + '">' + t.nextPageText + '</a>',
this.endPage = '<a href="javascript:void(0);" class="ui-pagination-page-item" data-current="' + e + '">' + t.endPageText + '</a>',
this.checkPage(),
this.isRender && n.html("<div class='ui-pagination-container'>" + this.homePage + this.prevPage + i + this.nextPage + this.endPage + '</div>');
},
checkPage: function() {
var t = this.opts;
var a = this.total;
var e = this.current;
t.isShow || (this.homePage = this.endPage = ''),
e === 1 && (this.homePage = this.prevPage = ''),
e === a && (this.endPage = this.nextPage = ''),
a === 1 && (this.homePage = this.prevPage = this.endPage = this.nextPage = ''),
a <= 1 && (this.isRender = !1);
},
getPagesTpl: function() {
var t = this.opts;
var a = this.total;
var e = this.current;
var i = '';
var n = t.count;
if (a <= n) {
for (g = 1; g <= a; g++) {
i += g === e ? '<a href="javascript:void(0);" class="ui-pagination-page-item active" data-current="' + g + '">' + g + '</a>' : '<a href="javascript:void(0);" class="ui-pagination-page-item" data-current="' + g + '">' + g + '</a>';
}
} else {
var s = n / 2;
if (e <= s) {
for (g = 1; g <= n; g++) {
i += g === e ? '<a href="javascript:void(0);" class="ui-pagination-page-item active" data-current="' + g + '">' + g + '</a>' : '<a href="javascript:void(0);" class="ui-pagination-page-item" data-current="' + g + '">' + g + '</a>';
}
} else {
var r = Math.floor(s);
var h = e + r;
var o = e - r;
var c = n % 2 == 0;
h > a && (c ? (o -= h - a - 1,
h = a + 1) : (o -= h - a,
h = a)),
c || h++;
for (var g = o; g < h; g++) {
i += g === e ? '<a href="javascript:void(0);" class="ui-pagination-page-item active" data-current="' + g + '">' + g + '</a>' : '<a href="javascript:void(0);" class="ui-pagination-page-item" data-current="' + g + '">' + g + '</a>';
}
}
}
return i;
},
setPage: function(t, a) {
return t === this.current && a === this.total ? this.ele : (this.current = t,
this.total = a,
this.render(),
this.ele);
},
getPage: function() {
return {
current: this.current,
total: this.total
};
},
eventBind: function() {
var a = this;
var e = this.opts.callback;
this.ele.off('click').on('click', '.ui-pagination-page-item', function() {
var i = t(this).data('current');
a.current != i && (a.current = i,
a.render(),
e && typeof e === 'function' && e(i));
});
}
},
t.fn.pagination = function(t, a, e) {
if (typeof t === 'object') {
var i = new n(this,t);
this.data('pagination', i);
}
return typeof t === 'string' ? this.data('pagination')[t](a, e) : this;
}
;
}(jQuery, window, document));

js:

$(function () {
var dt;
var agg = {};
var ihtml = [];
$.ajax({
url: 'website/news/list',
type: 'POST',
dataType: "json",
data: {
'type': '1'
},
success: function (datas) { //请求成功后处理函数。
dt = datas.result
data = datas.result
console.log(datas)
for (var i in data) {
ihtml.push('<div class="col-sm-12 col-md-4">' +
'<a href="news-child.html?articleId=' + data[i].id + '" target="_blank">' +
'<div class="f1">' +
'<span>' +
'<img src="' + data[i].img + '"/ alt="" width="262">' +
'</span>'
+
'<h2 class="f-tit">' + data[i].title + '</h2>' +
'<p class="f-cont">' + showHTML(data[i].content, 200, " ......") + '</p>' +
'<p class="f-time">' + data[i].updateTime.substring(0, 10) + '</p>'
+
'</div>' +
'</a>' +
'</div>') }
var cp = 12;
var len = ihtml.length / cp;
len = parseInt(ihtml.length % cp != 0 ? len + 1 : len); for (var i = 0; i < len; i++) {
var start = i * cp;
var end = start + cp;
end = end > ihtml.length ? ihtml.length : end;
ihtml.slice(start, end);
console.log(ihtml.slice(start, end));
agg[i] = (ihtml.slice(start, end));
htmltext = '' +
'' + (ihtml.slice(start, end)).join("") + '' +
''
}
ss(len);
},
error: function (d, msg) {
console.log("Could not find user " + msg);
}
}); var htmltext = '';
function ss(ind) { var i = 0;
i != 0 ? agghtml(agg[i]) : agghtml(agg[0]);
$("#pagination1").pagination({
currentPage: 1,
totalPage: ind,
callback: function (current) {
$("#current1").text(current)
console.log(current);
i = current - 1;
agghtml(agg[i]);
}
});
} function agghtml(arr) {
htmltext = '' +
'' + arr.join("") + '' +
''; $(".focus .container").html(htmltext);
}
}); function showHTML(str, length, endstr) {
if (str != null) {
var html = str.replace(/<[^>]+>/g, "").replace(/&nbsp;/ig, "").substring(0, length) + endstr;
return html;
}
return null; }

css

button {
display: inline-block;
padding: 6px 12px;
font-weight: 400;
line-height: 1.42857143;
text-align: center;
vertical-align: middle;
cursor: pointer;
border: 1px solid transparent;
border-radius: 4px;
border-color: #28a4c9;
color: #fff;
background-color: #5bc0de;
margin: 20px 20px 0 0;
} .box {
width: 800px;
margin: 100px auto 0;
height: 34px;
} .pages {
padding: 50px 0 0;
text-align: right;
margin: 0 416px;
} .info {
width: 200px;
height: 34px;
line-height: 34px;
} .ui-pagination-container {
height: 34px;
line-height: 34px
} .ui-pagination-container .ui-pagination-page-item {
font-size: 14px;
padding: 4px 10px;
background: #fff;
border: 1px solid #c5b7b7;
color: #888;
margin: 0 3px;
text-decoration: none
} .ui-pagination-container .ui-pagination-page-item:hover {
border-color: #568dbd;
color: #568dbd;
text-decoration: none
} .ui-pagination-container .ui-pagination-page-item.active {
background: #568dbd;
border-color: #568dbd;
color: #fff;
cursor: default
}

html:

<div class="content">
<div class="focus">
<!-- 渲染内容 -->
<div class="focus-1 container">
<!-- <div class="col-sm-12 col-md-4">
<a href="news-child.html" target="_blank">
<div class="f1"><span><img src="data:images/n01.jpg" alt="" width="262"></span>
<h2 class="f-tit">....</h2>
<p class="f-cont">
......
</p>
<p class="f-time">2020-05-25</p>
</div>
</a>
</div>--> </div> </div>
<!-- 分页 -->
<div id="pagination1" class="pages fl"></div>
</div>

jq分页功能。的更多相关文章

  1. JQ分页功能

    HTML <div id='page'></div> <div id='con'></div> CSS span{width: 60px;height: ...

  2. jq分页插件,支持动态,静态分页的插件,简单易用。

    工作中经常要用到分页功能.为了方便封装了一个比较通用的分页插件开源出来了,简单易用. 官网:https://cwlch.github.io/Ch_Paging 下载地址:https://github. ...

  3. 项目中的一个分页功能pagination

    项目中的一个分页功能pagination <script> //总页数 ; ; //分页总数量 $(function () { // $("#pagination"). ...

  4. 简单封装分页功能pageView.js

    分页是一个很简单,通用的功能.作为一个有经验的前端开发人员,有义务把代码中类似这样公共的基础性的东西抽象出来,一来是改善代码的整体质量,更重要的是为了将来做类似的功能或者类似的项目,能减少不必要的重复 ...

  5. php对文本文件进行分页功能简单实现

    php对文本文件进行分页功能简单实现 <!DOCTYPE> <html> <head> <meta http-equiv="Content-type ...

  6. Asp.net MVC3表格共用分页功能

    在建立的mvc3项目中,在Razor(CSHTML)视图引擎下,数据会在表格中自动的生成,但分页没有好的控件实现,这里我们开发了设计了一个分页的模板,适合于没有数据提交和有数据提交的分页的分页. 第一 ...

  7. WinForm DataGridView分页功能

    WinForm 里面的DataGridView不像WebForm里面的GridView那样有自带的分页功能,需要自己写代码来实现分页,效果如下图: 分页控件  .CS: 1 using System; ...

  8. Net 分页功能的实现

    首先写一个接口   1 2 3 4 5 6 public interface IPagedList     {         int CurrentPageIndex { get; set; }   ...

  9. 自己封装的JS分页功能[用于搭配后台使用]

    * 2016.7.03 修复bug,优化代码逻辑 * 2016.5.25 修复如果找不到目标对象的错误抛出. * 2016.5.11 修复当实际页数(pageNumber)小于生成的页码间隔数时的bu ...

随机推荐

  1. js animation & requestAnimationFrame

    js animation & requestAnimationFrame https://developer.mozilla.org/en-US/docs/Web/API/window/req ...

  2. HTTPS clone !== SSH clone

    HTTPS clone !== SSH clone https clone bug SSH clone OK testing SSH key https://www.cnblogs.com/xgqfr ...

  3. RT-Thread学习笔记3-线程间通信 & 定时器

    目录 1. 事件集的使用 1.1 事件集控制块 1.2 事件集操作 2. 邮箱的使用 2.1 邮箱控制块 2.2 邮箱的操作 3. 消息队列 3.1 消息队列控制块 3.2 消息队列的操作 4. 软件 ...

  4. 「NGK每日快讯」2021.1.14日NGK公链第72期官方快讯!

  5. Github Action 快速上手指南

    前言 各位读者,新年快乐,我是过了年匆忙赶回上海努力搬砖的蛮三刀. Github之前更新了一个Action功能(应该是很久以前了),可以实现很多自动化操作.用来替代用户自己设置的自动化脚本(比如:钩子 ...

  6. oracle中关键字的执行顺序

    执行顺序: from where group by having select order by ******当having/select 中出现组函数,那么其他没有被组函数修饰的列就必须出现下gro ...

  7. Iterative learning control for linear discrete delay systems via discrete matrix delayed exponential function approach

    对于一类具有随机变迭代长度的问题,如功能性电刺激,用户可以提前结束实验过程,论文也是将离散矩阵延迟指数函数引入到状态方程中. 论文中关于迭代长度有三个定义值:\(Z^Ta\) 为最小的实验长度,\(Z ...

  8. WPF 数据绑定实例一

    前言: 数据绑定的基本步骤: (1)先声明一个类及其属性 (2)初始化类赋值 (3)在C#代码中把控件DataContext=对象: (4)在界面设计里,控件给要绑定的属性{Binding 绑定类的属 ...

  9. Java线程池 ExecutorService了解一下

    本篇主要涉及到的是java.util.concurrent包中的ExecutorService.ExecutorService就是Java中对线程池的实现. 一.ExecutorService介绍 E ...

  10. 痞子衡嵌入式:自识别特性(Auto Probe)可以让i.MXRT1060无需FDCB也能从NOR Flash启动

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是自识别特性(Auto Probe)可以让i.MXRT1060无需FDCB也能从NOR Flash启动. 接着上篇文章 <了解i.M ...