<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<table border=1 width="500" align="center">
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>操作</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div class="pagination"></div>
</body>
<script>
//创建一个构建函数
function Page(classname, obj = {}) {
//创建好需要操作的对象
this.box = document.querySelector('.' + classname)
//自己创建一个div来放置分页
this.container = document.createElement('div')
this.setStyle(this.container, {
display: 'flex',
justifyContent: 'center',
alignItems: 'center'
})
// container中不允许选中文字
this.container.onselectstart = () => false
this.container.onmouseover = () => this.container.style.cursor = 'pointer';
//将做好的分页放入大的盒子中
this.box.appendChild(this.container)
//根据default创建div
//设置obj为空,是为了防止用户没有传任何参数,在循环遍历时报错
this.obj = obj
//给对象添加一个属性,用来放创建好的页码
this.pageList = ''
//定义了一个当前页
this.currentPage = 1
this.default = {
showTextObj: {
first: '首页',
prev: '上一页',
list: '',
next: '下一页',
last: '尾页'
},
pageData: {
total: 1000,
pageSize: 10
}
}
//处理参数
this.setParams()
// 定义一个属性 - 用来接收传进来的obj中的函数
this.show = obj.show || function(){}
//设置总页码
this.totalPage = Math.ceil(this.default.pageData.total / this.default.pageData.pageSize)
//根据default创建div
this.createDiv()
//创建页码
this.createPage()
//点击实现页码切换
this.click()
//设置禁用项
this.setDisabled()
//设置文本框和按钮
this.form()
this.show(this.currentPage)
}
//添加文本框和按钮的方法
Page.prototype.form = function () {
//创建文本框和按钮放在container里
var input = document.createElement('input')
input.setAttribute('type', 'number')
this.container.appendChild(input)
this.setStyle(input, {
height: '27px',
width: '50px',
'padding-left': '6px',
outline: 'none',
margin: '5px',
})
var btn = document.createElement('button')
btn.innerText = 'GO'
this.container.appendChild(btn)
this.setStyle(btn, {
height: '33px',
margin: '5px',
outline: 'none',
'background-color': 'skyblue',
border: 'none',
})
}
//处理用户传进来的参数,有就用,没有就使用默认值
Page.prototype.setParams = function () {
//如果用户没有传值,就是undefined,不会遍历;有就遍历
for (var attr in this.obj.showTextObj) {
//将用户的参数替换掉默认的参数
this.default.showTextObj[attr] = this.obj.showTextObj[attr]
}
for (var attr in this.obj.pageData) {
//将用户的参数替换掉默认的参数
this.default.pageData[attr] = this.obj.pageData[attr]
}
}
//创建div的代码
Page.prototype.createDiv = function () {
for (var attr in this.default.showTextObj) {
var div = document.createElement('div')
div.className = attr
div.innerText = this.default.showTextObj[attr]
this.container.appendChild(div)
if (attr !== 'list') {
this.setStyle(div, {
margin: '5px',
padding: '1px',
border: '1px solid black'
})
} else {
this.pageList = div
this.setStyle(div, {
display: 'flex',
justifyContent: 'center',
alignItems: 'center'
})
}
}
}
//设置样式函数
Page.prototype.setStyle = function (ele, styleObj) {
for (var attr in styleObj) {
ele.style[attr] = styleObj[attr]
}
}
//创建页码
Page.prototype.createPage = function () {
//根据总页码来判断页数显示
//创建页码时,先把之前创建的删除
this.pageList.innerHTML = ''
//小于5页时
if (this.totalPage <= 5) {
this.createp(1, this.totalPage)
} else {
//大于5页时
// 当前页是前两页
if (this.currentPage <= 2) {
this.createp(1, 5)
}
// 当前页是后两页
else if (this.currentPage >= this.totalPage - 1) {
this.createp(this.totalPage - 4, this.totalPage)
} else {
//当前页是中间页
this.createp(this.currentPage - 2, this.currentPage + 2)
}
}
}
//根据初始值和结束值将创建p标签的代码进行封装
Page.prototype.createp = function (start, end) {
for (var i = start; i <= end; i++) {
var p = document.createElement('p')
p.innerText = i
this.pageList.appendChild(p)
this.setStyle(p, {
margin: '5px',
padding: '1px',
border: '1px solid black'
})
if (this.currentPage == i) {
p.style.backgroundColor = 'rgb(255,165,0)'
p.style.color = '#000'
}
}
}
//设置禁用项
Page.prototype.setDisabled = function () {
//当前页为第一页时,首页和上一页就不能用了
if (this.currentPage == 1) {
this.container.children[0].setAttribute('disabled', true)
this.container.children[1].setAttribute('disabled', true)
//给禁用的按钮增加样式
this.container.children[0].style.backgroundColor = '#ccc'
this.container.children[1].style.backgroundColor = '#ccc'
}
else {
//当当前页不是第一页,清除样式
this.container.children[0].removeAttribute('disabled')
this.container.children[1].removeAttribute('disabled')
//给禁用的按钮增加样式
this.container.children[0].style.backgroundColor = 'transparent'
this.container.children[1].style.backgroundColor = 'transparent'
}
if (this.currentPage == this.totalPage) {
//当前页为最后一页时,尾页和下一页就不能用了
this.container.children[3].setAttribute('disabled', true)
this.container.children[4].setAttribute('disabled', true)
//给禁用的按钮增加样式
this.container.children[3].style.backgroundColor = '#ccc'
this.container.children[4].style.backgroundColor = '#ccc'
} else {
//当当前不是最后一页时,清楚样式
this.container.children[3].removeAttribute('disabled')
this.container.children[4].removeAttribute('disabled')
//给禁用的按钮增加样式
this.container.children[3].style.backgroundColor = 'transparent'
this.container.children[4].style.backgroundColor = 'transparent'
}
}
//设置点击事件
Page.prototype.click = function () {
this.container.onclick = () => {
var e = window.event
var target = e.target || e.srcElement
//点击尾页,跳转到最后一页
if (target.className === 'last' && target.getAttribute('disabled') !== 'true') {
this.currentPage = this.totalPage
this.createPage()
this.setDisabled()
this.show(this.currentPage)
}
//点击下一页,跳转
else if (target.className === 'next' && target.getAttribute('disabled') !== 'true') {
this.currentPage++
this.createPage()
this.setDisabled()
this.show(this.currentPage)
}
//点击上一页,跳转
else if (target.className === 'prev' && target.getAttribute('disabled') !== 'true') {
this.currentPage--
this.createPage()
this.setDisabled()
this.show(this.currentPage)
}
//点击首页,跳转到第一页
else if (target.className === 'first' && target.getAttribute('disabled') !== 'true') {
this.currentPage = 1
this.createPage()
this.show(this.currentPage)
}
//点击标签,进行跳转
else if (target.tagName === 'P') {
this.currentPage = +target.innerText
this.createPage()
this.setDisabled()
this.show(this.currentPage)
}
//点击按钮进行跳转
else if (target.tagName === 'BUTTON' && +target.previousElementSibling.value !== this.currentPage) {
if (target.previousElementSibling.value < 1) {
target.previousElementSibling.value = 1
} else if (target.previousElementSibling.value > this.totalPage) {
target.previousElementSibling.value = this.totalPage
}
this.currentPage = +target.previousElementSibling.value
this.createPage()
this.setDisabled()
this.show(this.currentPage)
}
}
}
// //new一个对象,传参
// var p = new Page('pagination', {
// // showTextObj: {
// // first: '|<',
// // prev: '<<',
// // next: '>>',
// // last: '>|'
// // },
// pageData: {
// total: 100,
// pageSize: 10
// }
// })
// 准备数据
var arr = [];
// 随机的姓
var firstName = '赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯昝管卢莫';
var secondName = '天地玄黄宇宙洪荒日月盈昃辰宿列张寒来暑往秋收冬藏闰余成岁律吕调阳云腾致雨露结为霜金生丽水玉出昆冈剑号巨阙珠称夜光果珍李柰菜重芥姜海咸河淡鳞潜羽翔龙师火帝鸟官人皇始制文字乃服衣裳推位让国有虞陶唐吊民伐罪周发殷汤坐朝问道垂拱平章爱育黎首臣伏戎羌遐迩一体率宾归王鸣凤在竹白驹食场化被草木赖及万方盖此身发四大五常';
var thirdName = '人之初性本善性相近习相远苟不教性乃迁教之道贵以专昔孟母择邻处子不学断机杼窦燕山有义方教五子名俱扬养不教父之过教不严师之惰子不学非所宜幼不学老何为玉不琢不成器人不学不知义为人子方少时亲师友习礼仪香九龄能温席孝于亲所当执融四岁能让梨弟于长宜先知首孝悌次见闻知某数识某文一而十十而百百而千千而万三才者天地人三光者日月星三纲者君臣义父子亲夫妇顺曰春夏曰秋冬此四时运不穷曰南北曰西东此四方应乎中曰水火木金土此五行本乎数';
for(var i=0;i<1000;i++){
// 创建一个对象放在arr中
var obj = {
id:i+1,
name:firstName[Math.floor(Math.random() * firstName.length)] + secondName[Math.floor(Math.random() * secondName.length)] + thirdName[Math.floor(Math.random() * thirdName.length)],
age:Math.floor(Math.random() * (80-18))+18,
sex:'男女'[Math.floor(Math.random()*2)]
}
arr.push(obj)
}
// console.log(arr);
var pageSize = 8
new Page('pagination',{
pageData:{
total:arr.length,
pageSize
},
show:function(currentPage){
console.log(currentPage);
/*
1 0,10
2 10,20
3 20,30
4 30,40
*/
var brr = arr.slice((currentPage-1)*pageSize, currentPage*pageSize)
// console.log(brr);
// 显示页面中
var str = '';
brr.forEach(v=>{
str += `
<tr>
<td>${v.id}</td>
<td>${v.name}</td>
<td>${v.sex}</td>
<td>${v.age}</td>
<td>删除</td>
</tr>
`
})
document.querySelector('tbody').innerHTML = str;
}
})
</script>
</html>
分页demo
最新推荐文章于 2022-05-13 11:43:36 发布