Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Gary 80 posts 377 karma points
    Nov 30, 2018 @ 16:23
    Gary
    0

    Detect Last item in API Call

    Hi,

    I have created a Controller for an Umbraco API call to retrieve child news Articles. It only takes 4 at a time and there is a "Load More" button to take the next four. So whenever someone selects the "Load More" button it will hit the api for the next 4.

    However, i want it to detect when there is no longer any more to get, so i can hide the Load More button. I could do this if the API returns nothing but i would want so it knows when it has retrieved the last one from the foreach loop.

    I have tried .IsLast() however, there will always be a "last" item when grabbing 4 at a time. Does anyone know how i can approach this? Do i need to do some kind of nested foreach to grab the inital collection first then a foreach for taking 4 of the parent collection?

    Any help would be really appreciated.

    Thanks :)

    Query to grab the next 4 items

    nodesToSearchThrough = help.Content(pageId)
                                .Children
                                .Where("Visible")
                                .OrderBy("createDate desc")
                                .Skip(currentCount)
                                .Take(moreCount);
    
  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Nov 30, 2018 @ 16:40
    Nik
    1

    Hi Gary

    Could you extend your returned data model to include a "Is more" flag along with the contents?

    Then in your API you can check if currentCount + moreCount is greater than or equal to the total number of children. If it is, you could set the "Is more" flag to be false.

    Something to consider though (and I don't know how you want to handle this)... what if someone browses the website, is loading more and at the same time a new article is published? How would your page handle that scenario?

    Nik

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies