[英]How to load the next page of the API's data using button click “Load More”?
my goal is to render some user data from API https://reqres.in/api/users?page=( this can be 1,2 or more if pages are available) and output it to a html table using JS / Promises. my goal is to render some user data from API https://reqres.in/api/users?page=( this can be 1,2 or more if pages are available) and output it to a html table using JS / Promises. So initially I have managed to get the first page's data to the table and now I need it to be modified when I click "Load More" button it should delete the current data on the table and shows the page 2's data.
所以最初我已经设法将第一页的数据放到表中,现在我需要在单击“加载更多”按钮时对其进行修改,它应该删除表上的当前数据并显示第 2 页的数据。 This is my code so far
到目前为止,这是我的代码
let userDataTable = document.getElementById("userData");
var tot_pages;
let pagesCount = 1;
console.log(tot_pages);
let getUSerInfo = function () {
fetch(`https://reqres.in/api/users?page=${pagesCount}`)
.then((response) => response.json())
.then((people) => {
let users = people.data;
tot_pages = people.total_pages;
console.log(users);
console.log(tot_pages);
for (let i = 0; i < users.length; i++) {
let htmlContent = `<tr><td>${users[i].id}</td><td><img src="${users[i].avatar}"/></td><td>${users[i].first_name}</td><td>${users[i].last_name}</td><td>${users[i].email}</td></tr>`;
userDataTable.insertAdjacentHTML("beforeend", htmlContent);
}
})
.catch(function (error) {
console.log(error);
});
};
getUSerInfo();
console.log(tot_pages);
document
.getElementById("load_btn")
.addEventListener("click", () => getUSerInfo());
also when there are no pages left to load (ex: when the last page's data is showing in the table then the "Load More" button should not be visible)还有当没有页面要加载时(例如:当最后一页的数据显示在表格中时,“加载更多”按钮不应该是可见的)
I'll explain what my idea was achieving this task: I was trying to create a global variable(tot_pages) initializing it to 1. Then inside the promise I was trying to assign the total pages from the object which I render via reqres.in and return it to the button as a counter or something.我将解释我的想法是如何完成这项任务:我试图创建一个全局变量(tot_pages),将其初始化为 1。然后在 promise 中,我试图从 object 中分配总页数,我通过 reqres.in 呈现并将其作为计数器或其他东西返回到按钮。 So as an ex: When I click the button the counter will increase(in this case the tot_pages variable will increase).
例如:当我单击按钮时,计数器会增加(在这种情况下,tot_pages 变量会增加)。 Anyway after hours of trying on my own could not get this done yet.
无论如何,经过数小时的尝试,我还是无法完成这项工作。 I do really appreciate if someone can help me out.
如果有人可以帮助我,我真的很感激。 Thank you for you time and consideration!
感谢您的时间和考虑!
I think your code will work fine with you updating pagesCount
variable on each successful API fetch, check this updated code.我认为您的代码可以在每次成功获取 API 时更新
pagesCount
变量,检查此更新后的代码。 I've changed some variable names我更改了一些变量名
let totalPages,
currentPage = 0,
loadMoreBtn = document.getElementById("load_btn");
// bind load more button
loadMoreBtn.addEventListener("click",getUSerInfo);
// fetch people
function getUSerInfo() {
// ignore if all data has been loaded
if(currentPage >= totalPages) return
const nextPage = currentPage + 1;
fetch(`https://reqres.in/api/users?page=${nextPage}`)
.then((response) => response.json())
.then((people) => {
const users = people.data,
userDataTable = document.getElementById("userData");
totalPages = people.total_pages;
// hide load more button
if(totalPages == nextPage) loadMoreBtn.style.visibility = 'hidden';
// udate table data
for (let i = 0; i < users.length; i++) {
let htmlContent = `<tr><td>${users[i].id}</td><td><img src="${users[i].avatar}"/></td><td>${users[i].first_name}</td><td>${users[i].last_name}< /td><td>${users[i].email}</td></tr>`;
userDataTable.insertAdjacentHTML("beforeend", htmlContent);
}
currentPage = nextPage;
})
.catch(function (error) {
console.log(error);
});
};
// fetch initial page
getUSerInfo();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.