AngularJS Infinite Scrolling with lots of data

JavascriptAngularjs

Javascript Problem Overview


So I'm trying to create an infinite scrolling table using AngularJS, similar to this: http://jsfiddle.net/vojtajina/U7Bz9/

The problem I'm having is that in the jsfiddle example, if I keep scrolling till I have a million results, the browser is going to slow to a crawl, wouldn't it? Because there would now be 1,000,000 results in $scope.items. It would be better if I only ever had, for example, 1000 results at a time inside $scope.items, and the results I was viewing happen to be within those 1000.

Example use case: page loads and I see the first 10 results (out of 1,000,000). Even though I only see 10, the first 1000 results are actually loaded. I then scroll to the very bottom of the list to see the last 10 items. If I scroll back up to the top again, I would expect that the top 10 results will have to be loaded again from the server.

I have a project I did with ExtJS that a similar situation: an infinite scrolling list with several thousand results in it. The ExtJS way to handle this was to load the current page of results, then pre-load a couple of extra pages of results as well. At any one time though, there was only ever 10 pages of results stored locally.

So I guess my question is how would I go about implementing this in AngularJS? I kow I haven't provided much code, so I'm not expecting people to just give the coded answer, but at least some advice in which direction to go.

Javascript Solutions


Solution 1 - Javascript

A couple of things:

  1. "Infinite scrolling" to "1,000,000" rows is likely to have issues regardless of the framework, just because you've created millions and millions of DOM nodes (presuming you have more than one element in each record)

  2. The implementation you're looking at doing with <li ng-repeat="item in items">{{item.foo}}</li> or anything like that will have issues very quickly for one big reason: {{item.foo}}} or any ngBind like that will set up a $watch on that field, which creates a lot of overhead in the form of function references, etc. So while 10,000 small objects in an "array" isn't going to be that bad... 10,000-20,000 additional function references for each of those 10,000 items will be.

What you'd want to do in this case would be create a directive that handles the adding and removing of DOM elements that are "too far" out of view as well as keeping the data up to date. That should mitigate most performance issues you might have.

... good infinite scrolling isn't simple, I'm sorry to say.

Solution 2 - Javascript

I like the angular-ui implementation ui-scroll...

https://github.com/angular-ui/ui-scroll

... over ngInfiniteScroll. The main difference with ui-scroll from a standard infinite scroll is that previous elements are removed when leaving the viewport.

From their readme...

> The common way to present to the user a list of data elements of undefined length is to start with a small portion at the top of the list - just enough to fill the space on the page. Additional rows are appended to the bottom of the list as the user scrolls down the list. > >The problem with this approach is that even though rows at the top of the list become invisible as they scroll out of the view, they are still a part of the page and still consume resources. As the user scrolls down the list grows and the web app slows down. > >This becomes a real problem if the html representing a row has event handlers and/or angular watchers attached. A web app of an average complexity can easily introduce 20 watchers per row. Which for a list of 100 rows gives you total of 2000 watchers and a sluggish app.

Additionally, ui-scroll is actively maintained.

Solution 3 - Javascript

It seems that http://kamilkp.github.io/angular-vs-repeat would be what you are looking for. It is a virtual scrolling directive.

Solution 4 - Javascript

So turns out that the ng-grid module for AngularJS has pretty much exactly what I needed. If you look at the examples page, the Server-Side Processing Example is also an infinite scrolling list that only loads the data that is needed.

Thanks to those who commented and answered anyway.

Latest URL : ng-grid

Solution 5 - Javascript

You may try using ng-infinite-scroll :

http://binarymuse.github.io/ngInfiniteScroll/

Solution 6 - Javascript

Check out virtualRepeat from Angular Material

It implements dynamic reuse of rows visible in the viewport area, just like ui-scroll

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongordonView Question on Stackoverflow
Solution 1 - JavascriptBen LeshView Answer on Stackoverflow
Solution 2 - JavascriptjfountainView Answer on Stackoverflow
Solution 3 - JavascriptPowerKiKiView Answer on Stackoverflow
Solution 4 - JavascriptgordonView Answer on Stackoverflow
Solution 5 - JavascriptHasanAboShallyView Answer on Stackoverflow
Solution 6 - JavascriptBernardo RamosView Answer on Stackoverflow