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

Creating a tooltip for a specific cell within an AG grid react component is a useful customization feature

How can I customize the tooltip for a specific row in my AG Grid? I see that there is a tooltipField available in ColDefs, but I want to provide a custom string instead of using a field value. Is there a way to achieve this customization? ...

Ways to interpret and contrast the information within files (verifying if the lengths of the strings are identical)

I have a web tool where users can upload text and code files, which are then saved in a local directory. I'm trying to create a function that reads these files, compares their lengths, and generates a report indicating whether they are equal or not. B ...

Invoking a class method in Javascriptcore on iOS

I'm currently trying to comprehend the inner workings of JavascriptCore. Initially, I attempted calling a single function. Now, my focus has shifted to invoking a function within a class. This is what my javascript code looks like: var sayHelloAlf ...

Incorporating a Drawer and AppBar using Material-UI and React: Dealing with the error message "Unable to access property 'open' of null."

Currently, I am working on developing an app using React and Material-UI. I have successfully implemented the AppBar component, but I am facing difficulties with setting up the Drawer functionality. The main issue I am encountering is an error message sta ...

When I send data using axios, I receive the response in the config object instead of the data

Currently, my web app is being developed using NextJS NodeJS and Express. I have set up two servers running on localhost: one on port 3000 for Next and the other on port 9000 for Express. In the app, there is a form with two input fields where users can e ...

IONIC App detecting lack of internet connectivity

I've successfully installed the Cordova network plugin with no errors on the code side. When I run ionic serve to test the app in my browser, it detects my WiFi and the plugin recognizes a connection. If I disable WiFi on my computer, it correctly id ...

Does MongoDB provide an array of "m" objects as output?

I've configured the backend using NodeJS and MongoDB. For the frontend, AngularJS and ngResource are being utilized. When I execute this section of code: $scope.users = User.query(function() { console.log($scope.users); }); it outputs the follow ...

Comparing strings with Ajax

I have been working on a basic ajax function setInterval(function() { var action = ''; var status = ''; $('#php-data').load('../Data/Dashboard.Data.php'); $.ajax({type: 'POST', u ...

The scope's attribute is present, but the variable within the scope's attribute is not

Currently, I am delving into angularJS development, but encountering a perplexing issue. I am attempting to format a JSON into a table with intricate rules. The JSON structure currently is as follows: { "id":"test", "title":"Test Sample", "de ...

Upon the creation of a new Post and attempting to make edits, the field is found to be blank

<template> <div class="card m-3"> <div class="card-body"> <Form method="post" @submit="submitData" :validation-schema="schema" ref="myForm" v-slot="{ errors, ...

How to Use JQuery to Display Elements with a Vague Name?

Several PHP-generated divs are structured as follows: <div style="width:215px;height:305px;background-color: rgba(255, 255, 255, 0.5);background-position: 0px 0px;background-repeat: no-repeat;background-size: 215px 305px;display:none;position:fixed;top ...

Avoid losing focus on href="#" (preventing the page from scrolling back up to the top)

Is there a way to prevent an empty href link from causing the page to scroll up when clicked? For example, if I have: <a href="#">Hello world</a> And this "Hello world" link is in the middle of the page. When clicked, the URL would look like ...

A guide on efficiently incorporating a php variable into json format, then transferring it to ajax

My Sample Code var ajaxResponse = xmlhttp.responseText; //ajax response from my php file jsonData = JSON.parse(ajaxResponse); alert(jsonData.result); And in my PHP Script $resultValue = 'Hello'; echo '{ "result":"' . $result ...

Attempting to minimize the repetition of code in Redux by implementing some utility functions

Is there a potential issue with the method I'm attempting in this URL: The concept involves altering only the actions file when introducing a new action. One improvement I am considering is recursively merging the status passed from the actions with ...

Customize the toggle icon for the accordion in Framework7

I took inspiration from the custom accordion elements provided in the documentation and made some alterations to the icons. However, I'm facing an issue with getting the toggle functionality of the icons to work properly. My goal is to have a "+" dis ...

Error in Next.js when the value of the target does not change in React-Select's

I am brand new to the world of react/nextjs and I keep encountering a frustrating error that says "Cannot read properties of undefined (reading 'value')". Despite trying various approaches, including treating select as a simple HTML tag, I have s ...

Test your knowledge of Javascript with this innerHtml quiz and see

How can I display the output of a score from a three button radio button quiz without using an alert popup? I want the output to be displayed within a modal for a cleaner look. I tried using InnerHTML but now there is no output when the button is clicked. ...

Is it possible to load a JavaScript file from a different domain using a bookmarklet?

I'm a newcomer to bookmarklets and I am experimenting with loading a JavaScript file from my own server/domain using the following bookmarklet/javascript code: javascript:(function(){s=document.createElement('script'); s.type=' ...

Connect the scroll wheel and then proceed to scroll in the usual way

There are two div elements with a height and width of 100%. One has the CSS property top:0px, while the other has top 100%. I have implemented an animation that triggers when the mousewheel is used to scroll from one div to the other. The animation functi ...

When using Jquery, hovering over an element will cause the full title to be

I have created a toggle div that displays an ellipsis (...) when the long title is longer than 30 characters. Now, I want the full text of the long title to appear when hovering over the div. Check out this JS Fiddle for reference. JS $('#popu ...