87,995
社区成员




<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Go Coding!</title>
<style>
* {
margin: 0;
padding: 0;
border: none;
list-style: none;
text-decoration: none;
font-size: 12px;
}
body {
padding: 20px;
}
.title {
font-size: 16px;
color: #f80;
}
.list>li {
padding: 6px;
border-bottom: 1px solid #eee;
}
.pagination {
padding: 4px;
}
.pagination a {
display: inline-block;
padding: 4px;
margin: 0 4px 0 0;
color: #00c;
}
</style>
</head>
<body>
<ul class="list"></ul>
<section class="pagination"></section>
<script>
let data = {
"list": [
{ "id": 1, "title": 2001 },
{ "id": 2, "title": 2002 },
{ "id": 3, "title": 2003 },
{ "id": 4, "title": 2004 },
{ "id": 5, "title": 2005 },
{ "id": 6, "title": 2006 },
{ "id": 7, "title": 2007 },
{ "id": 8, "title": 2008 },
{ "id": 9, "title": 2009 },
{ "id": 10, "title": 2010 },
{ "id": 11, "title": 2011 },
{ "id": 12, "title": 2012 },
{ "id": 13, "title": 2013 },
{ "id": 14, "title": 2014 },
{ "id": 15, "title": 2015 },
{ "id": 16, "title": 2016 },
]
}
let list = data.list;
console.log(`list-length: ${list.length}`);
pagination({
"current-page": 1,
"page-size": 5,
});
function pagination(par) {
let listBox = document.querySelector(".list");
let num = data.list.length;
let totalPage = 0;
let pageSize = par["page-size"];
let currentPage = par["current-page"];
if (num / pageSize > parseInt(num / pageSize)) {
totalPage = parseInt(num / pageSize) + 1;
} else {
totalPage = parseInt(num / pageSize);
}
let sliceStart = (currentPage - 1) * pageSize;
let sliceEnd = currentPage * pageSize;
sliceEnd = sliceEnd > num ? num : sliceEnd;
let dataSlice = data.list.slice(sliceStart, sliceEnd);
let listText = "";
for (let item of dataSlice) {
listText += `
<li><h5 class="title">${item.id} - ${item.title}</h5></li>
`;
}
listBox.innerHTML = listText;
let htmlText = "";
if (currentPage > 1) {
htmlText += `
<a href="#" onClick="pagination({'current-page': ${currentPage - 1}, 'page-size': ${pageSize}})">
上一页
</a>
`;
}
for (let i = 1; i < totalPage + 1; i++) {
htmlText += `
<a href="#" onClick="pagination({'current-page': ${i}, 'page-size': ${pageSize}})">
${i}
</a>
`;
}
if (currentPage < totalPage) {
htmlText += `
<a href="#" onClick="pagination({'current-page': ${currentPage + 1}, 'page-size': ${pageSize}})">
下一页
</a>
`;
}
let pagination = document.querySelector(".pagination");
pagination.innerHTML = htmlText;
}
</script>
</body>
</html>
function pagination(par) {
let listBox = document.querySelector(".list");
let num = data.list.length;
let totalPage = 0;
let pageSize = par["page-size"] || 5; // 默认每页显示 5 条数据
let currentPage = par["current-page"] || 1; // 默认显示第一页
...
}
if (currentPage > 1) {
htmlText += `
<a href="#" onClick="pagination({'current-page': ${currentPage - 1}, 'page-size': ${pageSize}})">
上一页
</a>
`;
}
for (let i = 1; i < totalPage + 1; i++) {
htmlText += `
<a href="#" onClick="pagination({'current-page': ${i}, 'page-size': ${pageSize}})">
${i}
</a>
`;
}
if (currentPage < totalPage) {
htmlText += `
<a href="#" onClick="pagination({'current-page': ${currentPage + 1}, 'page-size': ${pageSize}})">
下一页
</a>
`;
}
let sliceStart = (currentPage - 1) * pageSize;
let sliceEnd = currentPage * pageSize;
sliceEnd = sliceEnd > num ? num : sliceEnd;
let dataSlice = data.list.slice(sliceStart, sliceEnd);
let listText = "";
for (let item of dataSlice) {
listText += `
<li><h5 class="title">${item.id} - ${item.title}</h5></li>
`;
}
listBox.innerHTML = listText;
https://blog.csdn.net/kkLeung/article/details/104701681
N年前的笔记,后端python django +前端的分页。
定义分页的类,可以查考!!
for (let i = 1; i < totalPage + 1; i++) {
htmlText += `
<a class="item-link" href="#" onClick="pagination({'current-page': ${i}, 'page-size': ${pageSize}})">
${i}
</a>
`;
}
[/quote]
就是计算呀,得出for中i的范围呀……大概下面这样子的意思吧
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Go Coding!</title>
<style>
* {
margin: 0;
padding: 0;
border: none;
list-style: none;
text-decoration: none;
font-size: 12px;
}
body {
padding: 20px;
}
.title {
font-size: 16px;
color: #f80;
}
.list > li {
padding: 6px;
border-bottom: 1px solid #eee;
}
.pagination {
padding: 4px;
}
.pagination a {
display: inline-block;
padding: 4px;
margin: 0 4px 0 0;
color: #00c;
}
.pagination a.curr {
display: inline-block;
padding: 4px;
margin: 0 4px 0 0;
background-color: #000;
color: #fff;
}
</style>
</head>
<body>
<ul class="list"></ul>
<section class="pagination"></section>
<script>
let data = {
list: [
{ id: 1, title: 2001 },
{ id: 2, title: 2002 },
{ id: 3, title: 2003 },
{ id: 4, title: 2004 },
{ id: 5, title: 2005 },
{ id: 6, title: 2006 },
{ id: 7, title: 2007 },
{ id: 8, title: 2008 },
{ id: 9, title: 2009 },
{ id: 10, title: 2010 },
{ id: 11, title: 2011 },
{ id: 12, title: 2012 },
{ id: 13, title: 2013 },
{ id: 14, title: 2014 },
{ id: 15, title: 2015 },
{ id: 16, title: 2016 },
{ id: 1, title: 2001 },
{ id: 2, title: 2002 },
{ id: 3, title: 2003 },
{ id: 4, title: 2004 },
{ id: 5, title: 2005 },
{ id: 6, title: 2006 },
{ id: 7, title: 2007 },
{ id: 8, title: 2008 },
{ id: 9, title: 2009 },
{ id: 10, title: 2010 },
{ id: 11, title: 2011 },
{ id: 12, title: 2012 },
{ id: 13, title: 2013 },
{ id: 14, title: 2014 },
{ id: 15, title: 2015 },
{ id: 16, title: 2016 },
],
}
let list = data.list
console.log(`list-length: ${list.length}`)
pagination({
current: 1,
size: 5,
nums: 5,
})
function pagination(config) {
let listBox = document.querySelector(".list")
let num = data.list.length
let totalPage = 0
let pageSize = config.size
let currentPage = config.current
let nums = config.nums || 5
if (num / pageSize > parseInt(num / pageSize)) {
totalPage = parseInt(num / pageSize) + 1
} else {
totalPage = parseInt(num / pageSize)
}
let sliceStart = (currentPage - 1) * pageSize
let sliceEnd = currentPage * pageSize
sliceEnd = sliceEnd > num ? num : sliceEnd
let dataSlice = data.list.slice(sliceStart, sliceEnd)
let listText = ""
for (let item of dataSlice) {
listText += `
<li><h5 class="title">${item.id} - ${item.title}</h5></li>
`
}
listBox.innerHTML = listText
let htmlText = ""
if (currentPage > 1) {
htmlText += `
<a href="#" onClick="pagination({current: ${
currentPage - 1
}, size: ${pageSize},nums:${nums}})">
上一页
</a>
`
}
let start = currentPage - Math.floor(nums / 2)
let end = currentPage + Math.floor(nums / 2)
//保证有nums个页码在显示
end < nums && (end = nums)
//最大页码修正
end > totalPage && (end = totalPage)
//保证有nums个页码在显示
end - start != nums - 1 && (start = end - nums + 1)
//最小页码修正
start < 1 && (start = 1)
for (let i = start; i <= end; i++) {
htmlText += `
<a href="#" ${
i === currentPage ? 'class="curr"' : ""
} onClick="pagination({current: ${i}, size: ${pageSize},nums:${nums}})">
${i}
</a>
`
}
if (currentPage < totalPage) {
htmlText += `
<a href="#" onClick="pagination({current: ${
currentPage + 1
}, size: ${pageSize},nums:${nums}})">
下一页
</a>
`
}
let pagination = document.querySelector(".pagination")
pagination.innerHTML = htmlText
}
</script>
</body>
</html>
for (let i = 1; i < totalPage + 1; i++) {
htmlText += `
<a class="item-link" href="#" onClick="pagination({'current-page': ${i}, 'page-size': ${pageSize}})">
${i}
</a>
`;
}