gpt4 book ai didi

javascript - javascript中的简单分页

转载 作者:可可西里 更新时间:2023-11-01 01:22:20 27 4
gpt4 key购买 nike

我正在尝试为我的网站制作分页。 (http://anuntorhei.md)

代码:

var someVar = 50;


function someStupidFunction() {
if (objJson.length > 50) {
document.getElementById("nextPage").style.visibility = "visible";
}

if (someVar <= 50) {
document.getElementById("prevPage").style.visibility ="hidden";
} else {
document.getElementById("prevPage").style.visibility = "visible";
}
}


function nextPage() {
document.getElementById("listingTable").innerHTML = "";

if (someVar < objJson.length) {
document.getElementById("nextPage").style.visibility = "visible";
} else {
document.getElementById("nextPage").style.visibility = "hidden";
}

for (var i = someVar - 50; i < someVar; i++) {
document.getElementById("listingTable").innerHTML += objJson[i].adName + "<br>";
}

someVar += 50;

document.getElementById("prevPage").style.visibility = "visible";
}


function prevPage() {
document.getElementById("listingTable").innerHTML = "";

if (someVar > 50) {
document.getElementById("prevPage").style.visibility = "visible";
} else {
document.getElementById("prevPage").style.visibility = "hidden";
}

for (var i = someVar - 50; i < someVar; i++) {
document.getElementById("listingTable").innerHTML += objJson[i].adName + "<br>";
}

someVar -= 50;

document.getElementById("nextPage").style.visibility = "visible";
}

但是当 someVar 大于 objJson.length 时,我无法理解如何“隐藏”nextPage 按钮。

当我到达“结尾”时,nextPage 按钮在 objJson 小于 someVar 之后消失。这段代码有什么问题?

我怎样才能改变它以使其完美?抱歉我的英语不好,无法解释我需要的所有内容,希望您能理解我的需要!

最佳答案

我会解决您的任何问题...但这里有一个改进的模式,您应该遵循它来减少代码重复。

作为旁注,您应该考虑在客户端进行分页。因为如果你有一个巨大的数据集,这意味着你需要在页面加载之前下载所有数据。最好改为实现服务器端分页。

fiddle :http://jsfiddle.net/Lzp0dw83/

HTML

<div id="listingTable"></div>
<a href="javascript:prevPage()" id="btn_prev">Prev</a>
<a href="javascript:nextPage()" id="btn_next">Next</a>
page: <span id="page"></span>

Javascript(放在任何地方):

var current_page = 1;
var records_per_page = 2;

var objJson = [
{ adName: "AdName 1"},
{ adName: "AdName 2"},
{ adName: "AdName 3"},
{ adName: "AdName 4"},
{ adName: "AdName 5"},
{ adName: "AdName 6"},
{ adName: "AdName 7"},
{ adName: "AdName 8"},
{ adName: "AdName 9"},
{ adName: "AdName 10"}
]; // Can be obtained from another source, such as your objJson variable

function prevPage()
{
if (current_page > 1) {
current_page--;
changePage(current_page);
}
}

function nextPage()
{
if (current_page < numPages()) {
current_page++;
changePage(current_page);
}
}

function changePage(page)
{
var btn_next = document.getElementById("btn_next");
var btn_prev = document.getElementById("btn_prev");
var listing_table = document.getElementById("listingTable");
var page_span = document.getElementById("page");

// Validate page
if (page < 1) page = 1;
if (page > numPages()) page = numPages();

listing_table.innerHTML = "";

for (var i = (page-1) * records_per_page; i < (page * records_per_page); i++) {
listing_table.innerHTML += objJson[i].adName + "<br>";
}
page_span.innerHTML = page;

if (page == 1) {
btn_prev.style.visibility = "hidden";
} else {
btn_prev.style.visibility = "visible";
}

if (page == numPages()) {
btn_next.style.visibility = "hidden";
} else {
btn_next.style.visibility = "visible";
}
}

function numPages()
{
return Math.ceil(objJson.length / records_per_page);
}

window.onload = function() {
changePage(1);
};

更新 2014/08/27

上面有一个错误,当特定页面(通常是最后一页)不包含 records_per_page 条记录时,for 循环会出错,因为它试图访问不存在的索引.

修复很简单,通过在 for 循环中添加一个额外的检查条件来说明 objJson 的大小:

更新 fiddle :http://jsfiddle.net/Lzp0dw83/1/

for (var i = (page-1) * records_per_page; i < (page * records_per_page) && i < objJson.length; i++)

关于javascript - javascript中的简单分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25434813/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com