简体   繁体   English

来自谷歌表的Javascript分页JSON数据

[英]Javascript pagination JSON data from google sheet

Here I am fetching some JSON date from a google sheet as you can tell, I addition I would like this data to be sorted on pages containing only three records per page using pagination, with prev/next functions that will update dynamical without the page reloading.在这里,我正在从谷歌表中获取一些 JSON 日期,正如您所知道的,我另外我希望使用分页在每页仅包含三条记录的页面上对这些数据进行排序,使用 prev/next 函数将动态更新而无需重新加载页面.

At this moment I am stuck trying to connect the JSON data to the pagination functions to determine the data length and sort the records in multiple pages.此时,我一直在尝试将 JSON 数据连接到分页函数以确定数据长度并对多个页面中的记录进行排序。

How can I make the JSON data sort it selves in multiple pages, while navigating between the pages using next/prev?如何让 JSON 数据在多个页面中自行排序,同时使用 next/prev 在页面之间导航?

 var url = "https://spreadsheets.google.com/feeds/list/1xo6dUfcVOwPA5Lkd8pKevLj6lP-0gOaPXNBZk4jyuGw/od6/public/values?alt\=json"; var perPage = 3; var currentPage = 1; $.getJSON(url, function(data) { var output = ""; var length = data.length; $.each(data.feed.entry, function(index,value) { output += "Category: " + value.gsx$largeimage.$t + "</br>"+ "Name: " + value.gsx$imagetitle.$t + "</br></br>"; }); $(".test").append(output); }); function prevPage() { if (currentPage > 1) { currentPage--; changePage(currentPage); } } function nextPage() { if (currentPage < numPages()) { currentPage++; changePage(currentPage); } } function changePage(page) { // ?? } function numPages() { return Math.ceil(length / perPage); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="test"></div> <a href="javascript:prevPage()" id="btn_prev">Prev</a> <a href="javascript:nextPage()" id="btn_next">Next</a> page: <span id="page"></span>

  • You want to turn the page of data retrieved from Spreadsheet every perPage .您希望每perPage从 Spreadsheet 检索到的数据页。

If my understanding is correct, how about this modification?如果我的理解是正确的,这个修改怎么样? I think that there are several methods for your situation.我认为针对您的情况有几种方法。 So please think of this as just one of them.因此,请将此视为其中之一。

The flow of this modified script is as follows.这个修改后的脚本的流程如下。

  1. Retrieve all data from Spreadsheet.从电子表格中检索所有数据。 At that time, 1st paget is displayed.此时,显示第 1 页。
  2. When "Next" or "Prev" is clicked, the content of <div class="test"></div> is cleared.当点击“Next”或“Prev”时, <div class="test"></div>被清除。
  3. Put the next page using the retrieved data from Spreadsheet and currentPage .使用从电子表格和currentPage检索到的数据放置下一页。

Modified script:修改后的脚本:

 var url = "https://spreadsheets.google.com/feeds/list/1xo6dUfcVOwPA5Lkd8pKevLj6lP-0gOaPXNBZk4jyuGw/od6/public/values?alt\=json"; var perPage = 3; var currentPage = 1; var values = []; // Added // Modified $.getJSON(url, function(data) { var output = ""; $.each(data.feed.entry, function(index,value) { values.push({Category: value.gsx$largeimage.$t, Name: value.gsx$imagetitle.$t}); if (index < perPage) { output += "Category: " + value.gsx$largeimage.$t + "</br>"+ "Name: " + value.gsx$imagetitle.$t + "</br></br>"; } }); $(".test").append(output); }); function prevPage() { if (currentPage > 1) { currentPage--; changePage(); } } function nextPage() { if (currentPage < numPages()) { currentPage++; changePage(); } } // Modified function changePage() { $(".test").html(""); var start = (currentPage - 1) * perPage; var output = values.slice(start, start + perPage).reduce((s, e) => { return s += "Category: " + e.Category + "</br>"+ "Name: " + e.Name + "</br></br>"; }, ""); $(".test").append(output); } // Modified function numPages() { return Math.ceil(values.length / perPage); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="test"></div> <a onclick="prevPage()" href="#" id="btn_prev">Prev</a> <!-- Modified --> <a onclick="nextPage()" href="#" id="btn_next">Next</a> <!-- Modified --> page: <span id="page"></span>

If I misunderstood your question and this was not the result you want, I apologize.如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM