Jump to content
Sign in to follow this  
seblondres

Pagination with API

Recommended Posts

Hi,

 

I have this URL from the Groupon API:

https://partner-int-api.groupon.com/deals.json?country_code=IE&tsToken=IE_AFF_0_201236_212556_0&division_id=dublin&offset=0&limit=20

The &offset=0 is use for pagination. So I was wondering how could I add a "Next" link or "Button" to add +10 to &offset= so users can call the next 10 deals? I've been working with JSON to retrieve the deals and I was thinking of an onclick event, if someone could point me to the right direction.

 

Many Thanks,

Seb

Edited by seb_london

Share this post


Link to post
Share on other sites

That's right, I'd like to be able to add +10 to "&offset=" so on click on a button it will looks like "&offset=10", on another click it will looks like "&offset=20" etc.

Edited by seb_london

Share this post


Link to post
Share on other sites

You'll need to figure out what the current offset is, then. Worst case, you can get the URL and parse it to split up all of the parameters, then go through the parameter list to find the offset parameter, get the value, and increase it. Another way would be to store the URL without the offset parameter and add it each time you use the URL. The offset could be tracked separately in that case and you just add the current offset to the URL whenever you need to use the URL.

Share this post


Link to post
Share on other sites

ok thanks for your answer. In PHP, I'm able to add a variable in the URL (see: &offset='.$start.') and then rebuild the url with http_build_query and $start+10. It works fine, but since Groupon only support JSON, I was looking for a similar solution in JavaScript by passing a variable in the URL.

https://partner-int-api.groupon.com/deals.json?country_code=IE&tsToken=IE_AFF_0_201236_212556_0&division_id=dublin&offset='.$start.'&limit=20
Edited by seb_london

Share this post


Link to post
Share on other sites

ok, I have slice up the URL so now I have a variable for offset. How can I increase offset +10 from a button or link?

 

code:

 

<script>$(document).ready(function() {var URL = "https://partner-int-api.groupon.com/deals.json";var countryCode = "UK";var token = "&tsToken=IE_AFF_0_202176_212556_0";var categoryFilter = "category:food-and-drink";var offset = "0";var limit = "10";var jsonCallBack = "&callback=?";var ajaxURL = URL + "?country_code=" + countryCode + token + "&filters=" + categoryFilter + "&offset=" + offset + "&limit=" + limit + jsonCallBack;$.getJSON(ajaxURL,function(data) {$.each(data.deals, function(i,data) {var div_data ="<div><a href='"+data.dealUrl+"'>"+data.title+"</a><p>"+data.highlightsHtml+"</p></div>";$(div_data).appendTo("#showdata");}); // end each}); // end get JSON}); // end ready</script>

Thanks,

Share this post


Link to post
Share on other sites

One option would be to have the offset value saved in the global namespace, maybe as a property of a global object, and each time they click on the next button you would check the current offset, add 10 to it, and use that value in the next link to load. This is all running in one page without a refresh, right?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...