Javascript Dom表分页
我是JavaScript新手。我尝试将分页添加到JavaScript表中,但该表没有形成,搜索/排序也不起作用。下面是我尝试分页的代码。我认为问题在于分页和主代码的集成 我的完整代码:Javascript Dom表分页,javascript,dom,pagination,Javascript,Dom,Pagination,我是JavaScript新手。我尝试将分页添加到JavaScript表中,但该表没有形成,搜索/排序也不起作用。下面是我尝试分页的代码。我认为问题在于分页和主代码的集成 我的完整代码: 文件: 输出: 我试图解决你的问题 这是我的最新消息 我做了以下更改 创建了Next、Prev和显示当前页面的位置 创建了一个称为分页的对象,负责执行prev、next或reset分页 将数据从文件加载到表中后,需要调用pagination.init(),以便它将分页设置为初始阶段 在init函数中,我只为ne
文件: 输出:
我试图解决你的问题 这是我的最新消息 我做了以下更改
var pagination = {
currentPage: 0,
itemPerPage: 2,
currentPageItems: [],
next: function () {
if(!!tableObj.rows && (tableObj.rows.length >= (this.itemPerPage*(this.currentPage+1)))) {
var startIndex = this.itemPerPage*this.currentPage;
++this.currentPage;
var endIndex = (this.itemPerPage*this.currentPage);
pagination.currentPageItems = tableObj.rows.slice(startIndex, endIndex);
buildTable(tableObj.headers, pagination.currentPageItems);
document.getElementById("current-page-number").textContent = "Page Number: "+this.currentPage;
} else {
alert("No Items to go next")
}
},
prev: function () {
--this.currentPage;
if ((this.currentPage - 1) >= 0) {
var endIndex = this.itemPerPage*this.currentPage;
--this.currentPage;
var startIndex = (this.itemPerPage*this.currentPage);
pagination.currentPageItems = tableObj.rows.slice(startIndex, endIndex);
buildTable(tableObj.headers, pagination.currentPageItems);
document.getElementById("current-page-number").textContent = "Page Number: "+this.currentPage;
} else {
alert("Not allowed");
this.currentPage = 0;
}
},
reset: function () {
this.currentPage = 0;
this.currentPageItems = [];
},
init: function() {
this.reset();
this.next();
document.getElementById("gotonext").onclick = function() {
pagination.next()
};
document.getElementById("gotoprev").onclick = function() {
pagination.prev();
};
}
};
分页。currentPageItems保存当前视图(页面)的数据,因此您只需要对pagination.currentPageItems执行筛选或排序操作。这将对当前页面的数据进行筛选/排序。非常感谢您的帮助。
var pagination = {
currentPage: 0,
itemPerPage: 2,
currentPageItems: [],
next: function () {
if(!!tableObj.rows && (tableObj.rows.length >= (this.itemPerPage*(this.currentPage+1)))) {
var startIndex = this.itemPerPage*this.currentPage;
++this.currentPage;
var endIndex = (this.itemPerPage*this.currentPage);
pagination.currentPageItems = tableObj.rows.slice(startIndex, endIndex);
buildTable(tableObj.headers, pagination.currentPageItems);
document.getElementById("current-page-number").textContent = "Page Number: "+this.currentPage;
} else {
alert("No Items to go next")
}
},
prev: function () {
--this.currentPage;
if ((this.currentPage - 1) >= 0) {
var endIndex = this.itemPerPage*this.currentPage;
--this.currentPage;
var startIndex = (this.itemPerPage*this.currentPage);
pagination.currentPageItems = tableObj.rows.slice(startIndex, endIndex);
buildTable(tableObj.headers, pagination.currentPageItems);
document.getElementById("current-page-number").textContent = "Page Number: "+this.currentPage;
} else {
alert("Not allowed");
this.currentPage = 0;
}
},
reset: function () {
this.currentPage = 0;
this.currentPageItems = [];
},
init: function() {
this.reset();
this.next();
document.getElementById("gotonext").onclick = function() {
pagination.next()
};
document.getElementById("gotoprev").onclick = function() {
pagination.prev();
};
}
};