猿问

如何使用“加载更多”按钮加载 API 数据的下一页?

我的目标是从 API https://reqres.in/api/users?page= 渲染一些用户数据(如果页面可用,这可以是 1,2 或更多)并使用 JS / Promises 将其输出到 html 表。因此,最初我已经设法将第一页的数据添加到表中,现在我需要对其进行修改,当我单击“加载更多”按钮时,它应该删除表中的当前数据并显示第 2 页的数据。这是我到目前为止的代码


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());

另外,当没有剩余页面可供加载时(例如:当最后一页的数据显示在表格中时,“加载更多”按钮不应该可见)


我将解释我的想法是如何实现此任务的:我试图创建一个全局变量(tot_pages),将其初始化为 1。然后在承诺中,我试图分配通过 reqres.in 渲染的对象的总页数并将其作为计数器或其他东西返回给按钮。因此,作为前例:当我单击按钮时,计数器将会增加(在这种情况下,tot_pages 变量将会增加)。无论如何,经过几个小时的尝试,我自己还无法完成这项工作。如果有人能帮助我,我真的很感激。感谢您的时间和考虑!


开心每一天1111
浏览 106回答 1
1回答

凤凰求蛊

我认为您的代码可以pagesCount在每次成功的 API 获取上更新变量时正常工作,请检查此更新的代码。我更改了一些变量名称let totalPages,&nbsp; &nbsp; currentPage = 0,&nbsp; &nbsp; loadMoreBtn = document.getElementById("load_btn");// bind load more buttonloadMoreBtn.addEventListener("click",getUSerInfo);// fetch people&nbsp;function getUSerInfo() {&nbsp; &nbsp; // ignore if all data has been loaded&nbsp; &nbsp; if(currentPage >= totalPages) return&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; const nextPage = currentPage + 1;&nbsp; &nbsp; fetch(`https://reqres.in/api/users?page=${nextPage}`)&nbsp; &nbsp; &nbsp; &nbsp; .then((response) => response.json())&nbsp; &nbsp; &nbsp; &nbsp; .then((people) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const users = people.data,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;userDataTable = document.getElementById("userData");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;totalPages = people.total_pages;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// hide load more button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(totalPages == nextPage) loadMoreBtn.style.visibility = 'hidden';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// udate table data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (let i = 0; i < users.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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}<&nbsp; /td><td>${users[i].email}</td></tr>`;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userDataTable.insertAdjacentHTML("beforeend", htmlContent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;currentPage = nextPage;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .catch(function (error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(error);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; };&nbsp;// fetch initial page&nbsp;getUSerInfo();
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答