For scenarios where the amount of data to be displayed in a table is likely to affect the page load, a nice enhancement is to have the data split in smaller pieces, being retrieved and rendered according to the way the user interacts with the table.
The user experience will be quite similar to what Facebook and Twitter have in their timeline pages. On the page load, the table will have just a subset of the data. The user can scroll the table, but before such a scrolling hits the bottom, the table content is augmented with another data subset. The user is then able to scroll over that new subset and the process of presenting data sets goes on.
This main purpose of this blog entry is to describe how to implement such a lazy loading capability (also referred as infinite scroll) in AngularJS, taking advantage of custom directives and some JQuery functions.
To ilustrate the concept, let us regard a table that displays historical prices of a fictitious stock.
A running version of the example can be accessed in this link
Below, the HTML code, along with AngularJS tags:
The table is being managed a StockHistoryController controller as part of a StockHistoryApp module. Each table record comes from the stockList collection in the
scope. Each row has a date and the corresponding stock price.
In order to have lazy loading in place, there has to be something to monitor the scrolling and trigger the load of more data. A custom AngularJS directive is an elegant approach, where a element can be enhanced in a declarative fashion. In our current example, that is the purpose of the when-scroll-ends directive in the tbody element
Now, the AngularJS code for the module:
The loadMoreRecords function added to the scope is in charge of retrieving the stock data. For the sake of simplificity, the data is made-up randomly, rather than fetched from another system.
The custom directive
The directive takes advantage of JQuery to bind any scrolling of an element (in our case, the table body) to the execution of a callback (for our example, invokes the loadMoreRecords function) in case the scroll bar is close to the bottom.
Notice that the name directive added to the module is whenScrollEnds, a camel case without dashes name to match the when-scroll-ends in the HTML code, according to the normalization process performed by AngularJS.
In terms of directive properties, only two: restrict to specify that the directive needs to be declared as an element attribute (the "A" value) in HTML code, and link, given the need to manipulate DOM elements.
The second argument in the link function is a JQuery wrapper to the table body DOM element. By the way, if JQuery had not been loaded in the HTML code, the argument in the function would be a jqLite (that has a subset of JQuery API) wrapper instead.
The element.scroll(...) invocation is a shorthand available in JQuery API that would have the same effect of invoking element.on('scroll', ....), namely the binding of a callback to the DOM onscroll event.
To verify whether the scroll bar is close to the bottom, what the callback does is checking the number of pixels scrolled down (the DOM scrollTop property) against the maximum number of pixels that can be scrolled, which is the difference between the entire table body height (the DOM scrollHeight property) and the visible table body height (the DOM height property). If there are less than 100 pixels (that is an arbitrary threshold) to the bottom, the loadMoreRecords is invoked. The attrs argument is a map of all attribute values inside the table body element and when-scroll-ends attribute is referable in AngularJS code through the same normalization process mentioned before.