Tips for integrating an infinite scroll feature using HTTP requests?

My current project involves developing a webapp using AngularJS to interact with a long array of objects. To display these objects on my index, I am utilizing nested ng-repeat functions. Additionally, I have implemented infinite scroll functionality similar to Facebook, where making it activates an HTTP GET request for more objects.

However, in trying to incorporate this feature, I find myself confused between two libraries:

1:

2:

Here is the code snippet for my nested ng-repeat:


    <div ng-repeat="monthi in monthedIncidents"  ng-hide="showme">
        <div style="width: 100%;">
            <h3>{{monthi.name}}</h3>
                <div class="line-separator"></div>
        </div>
        <ul class="list">
            <li class="list__item" ng-repeat="incident in monthi.incidents">
                <!-- ngrepeat: mostrar total de incidentes-->
                <a href="#" data-toggle="modal" data-target="#incidentModal" ng-click="selectIncident(incident.index)">
                    <figure class="list__item__inner">
                        <div class="bagdets">
                            <span class="glyphicon glyphicon-comment"><span> {{incident._embedded.commentsCount}} </span>
                            <span class="glyphicon glyphicon-picture"><span> {{incident._embedded.attachmentsCount}} </span>
                        </div>
                        <div class="hoverMask"></div>
                        <div class="incident-image">
                            <img ng-src="{{incident._links.thumbnail.href || 'img/03.jpg'}}">
                            <p class="incident-type"><span>{{incident._embedded.incident_type}}</span>
                            </p>
                        </div>
                        <figcaption>
                            <p>{{incident.name}}</p>
                            <p>{{incident.id}}</p>
                            <p id="description">{{incident.description | strLimit: 90 }}</p>
                            <p><span class="glyphicon glyphicon-calendar"></span> {{incident.upload_date | date:'EEE - dd/MM/yyyy '}} <span class="glyphicon glyphicon-time"></span> {{incident.upload_date | date:'hh:mm:ss a '}}</p>
                            <p> <span class="glyphicon glyphicon-user"></span> {{incident._embedded.employee}}</p>
                        </figcaption>
                    </figure>
                </a>
            </li>
        </ul>
     </div>

This section demonstrates my controller's role:


    app.controller("IncidentIndexCtrl", function ($resource, $scope, Incident, Device, IncidentType, $http, $window) {
    
    /*Retrieve all incidents*/
    $scope.reloadIncidents = function () {
        
        Incident.getIncidents({
            
            startdate: $scope.startDateResult,
            enddate: $scope.endDateResult,
            description: $scope.descriptionResult,
            incidentType: $scope.incidentTypeResult,
            
        }, function (data) {
            $scope.incidents = data._embedded.incidents;
            
            
            var monthIncidents = [];
            
            for (var i in $scope.incidents) {
                var month = new Date($scope.incidents[i].upload_date).getMonth();
                
                if (!monthIncidents[month]) {
                    monthIncidents[month] = {
                        name: $scope.months[month],
                        incidents: []
                    };
                }
                
                var incident = $scope.incidents[i];
                incident.index = i;

                monthIncidents[month].incidents.push(incident);
            }

            $scope.monthedIncidents = [];

            for (var e in monthIncidents) {
                $scope.monthedIncidents.push(monthIncidents[e]);
            }

        });
    };

    $scope.reloadIncidents();

    $scope.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

startdate: $scope.startDateResult enddate: $scope.endDateResult description: $scope.descriptionResult incidentType: $scope.incidentTypeResult

The purpose behind using these values is to manipulate the following URL:

The JSON array provides me with the URL for accessing the next object based on the specified limit:


{
    "offset": 0,
    "limit": 4,
    "_links": {
        "self": {
            "href": "http://incidents-core/app_dev.php/incidents?order=ASC&orderBy=id&limit=4&offset=0"
        },
        "first": {
            "href": "http://incidents-core/app_dev.php/incidents?order=ASC&orderBy=id&limit=4&offset=0"
        },
        "next": {
            "href": "http://incidents-core/app_dev.php/incidents?order=ASC&orderBy=id&limit=4&offset=4"
        }
    }
}

Answer №1

Give this a shot:

Solution:

<div scroll-unlimited="fetch()" scroll-unlimited-disabled="isFetching || endScroll" scroll-unlimited-distance="1"><!-- Your data goes here --></div>

Handler:

    var limit = 5;
    var pageNo = 1;
    $scope.isFetching = false;
    $scope.endScroll = false;


    $scope.fetch = function() {
        if ($scope.isFetching) return;
        $scope.isFetching = true;

        //Perform necessary operations here with pagination support
        //Pass a callback
        //...

        function handleResponse(response){
            $scope.totalEntries = response.length;
            if(limit*pageNo >= $scope.totalEntries) $scope.endScroll = true;
            pageNo++;
            $scope.isFetching = false;
        }   

        });
    };

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Check the validity of the Angular regex to ensure that it does not contain the email addresses info@, admin@, help@, or

I need to implement a way to restrict certain emails using Angular's ng-pattern The following emails should be considered invalid [email protected] [email protected] [email protected] [email protected] The regex pattern shown below was function ...

What exactly does the .proxy() method do in jQuery?

Can you explain the purpose of the jQuery.proxy function in jQuery and describe the scenarios where it is most beneficial? I came across this link, but I'm struggling to grasp its concept fully. ...

Extracting data from the response object and injecting it into the request

Once the file has been successfully uploaded, the 'uploadSuccess' callback event is triggered, providing information about the newly created media. The 'hashed_id' value within this object is crucial for my PUT request. I attempted to ...

Is there a way to stop "window.location.replace()" from being replaced or overridden?

Is there a method to safeguard against alterations or overrides of window.location.replace()? For instance, attempting to change it like this: window.location.replace = function(){ return "Hi" }. Initially, I experimented with using Object.free ...

How can you interact between a parent's named controller and JavaScript files in AngularJS?

Lately, I've been exploring the use of named controllers in my code and find it to be a much better alternative to using traditional $scope variables. It makes accessing parent controller attributes and functions within the DOM seamless. See an exampl ...

upright scrolling mouse slider

In my project, I have implemented a vertical slider with mousewheel control using SwiperJS from https://swiperjs.com/. Although the slider is working perfectly fine, I am looking to fix the positions while scrolling on the page similar to the example pro ...

The innovative way Vue.js revolutionizes HTML updates within a Websocket onmessage event

I'm new to Vue.js and I'm trying to capture data inside the onmessage event of a websocket and update an HTML component (either a span or div). While console.log or alert functions work in onmessage, I couldn't get it to update the span. Th ...

The Arrow notations don't seem to be functioning properly in Internet Explorer

Check out my code snippet in this JSFiddle link. It's working smoothly on Chrome and Mozilla, but encountering issues on IE due to arrow notations. The problem lies within the arrow notations that are not supported on IE platform. Here is the specifi ...

When using jquery.hide() and show(), the content within the div disappears momentarily before reappearing

Hello! I am experiencing an issue where the content of my div disappears after using a jQuery hide() and show() function. It seems to be gone. <li class="gg3"><a class="link" href="#" data-rel="content3">Link1</a></li> <div clas ...

Is it possible to pass a class method to an onClick event from within the render method in ReactJS?

Excuse my lack of experience, but I haven't been able to find a solution to this yet. I am attempting to utilize a class method as a callback for the onClick event in my JSX. Below is the code for my App component: import React from 'react&apo ...

Adjusting HTML5 drag height while resizing the window

Code conundrum: var dragHeight = window.innerHeight - parseInt(jQuery("#drag_area").css("margin-top")) - 5;. It sets the drag height based on browser size, but there's a glitch. If I start with a non-maximized browser and then maximize it, the drag he ...

ClickAwayListener doesn't function correctly when used in conjunction with Collapse or Fade animations

Currently, I am working on implementing a notifications area on my website. The idea is to display a notification icon, and once the user clicks on it, a list of notifications will be shown. For reference, you can view the code in this codesandbox I have ...

The automatic opening of a modal in a Livewire component is not functioning as expected

I am having trouble displaying a modal when my component initializes. I have tried using $this->emit('show) to open the modal When I add a button in my view, the emit('show') works! However, I want the modal to be automatically shown whe ...

Hiding Bootstrap Popover When User Clicks Outside of it

My webpage has dynamically loaded content featuring popovers which need to be bound to the body for correct loading and appearance. I am looking for a solution to hide the popovers when a user clicks outside them or on another popover trigger. After some ...

Issues with the HTML required attribute not functioning properly are encountered within the form when it is

I am encountering an issue with my modal form. When I click the button that has onclick="regpatient()", the required field validation works, but in the console, it shows that the data was submitted via POST due to my onclick function. How can I resolve thi ...

Application crash imminent, alert: Uncaught TypeError detected - Unable to access property 'some' of undefined within React

My application has 3 sections. The first section consists of a form where users fill in details about watches, and the data is submitted using the submitHandler function. In the second part, users can enter watches from the first section. When a user click ...

Utilizing variables in Angular service to make API calls

Currently, I am working on accessing the Google Books API. Initially, I was able to directly access it in my controller but now I want to follow best practices by moving the business logic into a directive. HTML <form ng-submit="search(data)"> < ...

Is it considered acceptable to include a JSONP request within another JSONP request?

Can you confirm if the code below is valid? $.getJSON(server_url+"?callback=?",{id:deleteId,pw:password,action:"editpassword"},function(data){ $.getJSON(server_url+"?callback=?", {id:deleteId,pw:password,action:"editpassword"},function( ...

The total sum of values within labels that share the same class

I am attempting to sum up all the values of class tmpcpa and store the result in final_cpa, but for some reason final_cpa always ends up being 0. document.getElementById('cpa' + arr[0]).innerHTML = cpa + '(' + '<label id="tmp ...

Brick-themed HTML/CSS elements drift away from each other

I'm currently designing an image collage for my website and attempted to use masonry for the layout. However, when I adjust the size of the blocks, they seem to drift apart, creating large gaps between each block. Any suggestions on how to resolve thi ...