ui-scroll

UI Scroll - AngularJS directive to provide infinite scroll over a limited element buffer

Build Status npm version Bower version Join the chat at https://gitter.im/angular-ui/ui-scroll

looking for next Angular version? try ngx-ui-scroll


Introduction

Why?

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.

How it works

The uiScroll directive solves the problem just described by dynamically destroying elements as they become invisible and recreating them if they become visible again.

The uiScroll directive is similar to the ngRepeat. Like the ngRepeat, uiScroll directive instantiates a template once per item from a collection. Each template instance gets its own scope, where the given loop variable is set to the current collection item. The collection content is provided by the datasource.

The directive is asking the datasource for data to build and render elements until it has enough elements to fill out the viewport. It will start retrieving new data for new elements again if the user scrolls up/down to the edge of visible element list.

Basic usage

<ANY ui-scroll-viewport>
  <ANY ui-scroll="items in datasource" ... >
      ...
  </ANY>
</ANY>

Listing ANY for the tag, the directive can be applied to, stretches the truth - a little bit. The directive works well with majority of the ‘usual’ tags - divs, spans, a, inputs, etc. For all of them the viewport should be a div (unless it is the window). Some other tags require special treatment. If the repeated tag is a li, it is best to use ul or ol as a viewport. For a tr as a repeated tag the viewport has to be the table or tbody. dl as a repeated tag is not supported.

The viewport is an element representing the space where the items from the collection are to be shown. Unless specified explicitly with the uiScrollViewport directive, browser window will be used as the viewport.

Important! The viewport height must be constrained. If the height of the viewport is not constrained (style=”height:auto”) it will pull the entire content of the datasource and may throw an Error depending on the number of items in the datasource. Even if it does not, using the directive this way does not provide any advantages over using ng-repeat, because item template will be always instantiated for every item in the datasource.

Important! There is a Scroll Anchoring feature enforced by some browsers (e.g Google Chrome since v56) which makes scroller behavior incorrect. The ui-scroll-viewport directive eliminates this effect by disabling the ‘overflow-anchor’ css-property on its element. But if the ui-scroll-viewport is not presented in the template, you should take care of this manually by switching it off on the body/html container.

Examples

We have built pull of examples covering a number of use-cases, showing various ways to use the ui-scroll directive. Each demo is absolutely independent and has its own html-page and the javascript file with the same name and .js extension.

To run the examples use this link.

Install and connect

To install the package via npm use

npm install --save angular-ui-scroll

After installation via npm, the ui-scroll distributive files will be available as

<script src="node_modules/angular-ui-scroll/dist/ui-scroll.min.js">
<script src="node_modules/angular-ui-scroll/dist/ui-scroll-grid.min.js">

To install the package via bower use

bower install angular-ui-scroll

After installation via bower, the ui-scroll distributive files will be available as

<script src="bower_components/angular-ui-scroll/dist/ui-scroll.min.js">
<script src="bower_components/angular-ui-scroll/dist/ui-scroll-grid.min.js">

There are also uncompressed versions (ui-scroll.js, ui-scroll-grid.js) and sourcemaps for all of js-files.

To use it in your angular-app you should add the module (modules)

angular.module('application', ['ui.scroll', 'ui.scroll.grid'])

Note: angular-ui-scroll works with AngularJS v1.2.0 and above.

Currently we have 2 regular modules which can be added to the angular-app you are developing:

Also, there is one more additional module in a separate file:


uiScroll directive

<div ui-scroll="item in myDatasource"
	 buffer-size="5"
	 padding="1"
	 start-index="100"
	 adapter="myAdapter"
></div>

Parameters

Some of the properties offered by the adapter can also be accessed directly from the directive by using matching attributes. In the same way as for the adapter attribute, syntax for such attributes allows for providing a reference expression to be used to access the corresponding value. Below is a list of such attributes:

The expression can be any angular expression (assignable expression where so specified). All expressions are evaluated once at the time when the scroller is initialized. Changes in the expression value after scroller initialization will have no impact on the scroller behavior.

Assignable expressions

The assignable expressions will be used by scroller to inject the requested value into the target scope. The target scope locating is based on AngularJS $parse service. If the viewport is presented (the element marked with the uiScrollViewport directive), then the scope associated with the viewport will be a start point in the target scope locating. If not, the scope next to the ui-scroll local scopes (ui-scroll parent scope) will be a start point.

Please notice that properties defined via assignable expression could be unavailable in case of nested scopes and non-explicit assignment. For example, if you want to have the Adapter on some controller’s scope and there are intermediate scopes in between (due to ng-if for example), you should use Controller As syntax or explicitly define at least two-level object hierarchy on your controller:

<div ng-controller="MyController">
  <div ng-if="show">
    <div ui-scroll="item in datasource" adapter="container.adapter">
.controller('MyController', function($scope) {
  $scope.container = { adapter: {} };

Datasource

Datasource is an object to be used by the uiScroll directive to access the data.

The directive will locate the object using the provided data source name. It will first look for a property with the given name on its $scope (here is the example of scope-based datasource usage). If none found it will try to get an angular service with the provided name (here is the example of service-based datasource usage).

The datasource object implements methods and properties to be used by the directive to access the data.

Important! Make sure to respect the index and count parameters of the request. The array passed to the success method should have exactly count elements unless it hit eof/bof.

Adapter

The adapter object is an internal object created for every instance of the scroller. Properties and methods of the adapter can be used to manipulate and assess the scroller the adapter was created for.

The adapter object is determined via ui-scroll attribute “adapter”, which value is construed as assignable expression.

Adapter object implements the following properties:

Adapter object implements the following methods

Manipulating the scroller content with the adapter methods

Adapter methods applyUpdates, append and prepend provide a way to update the scroller content without full reload of the content from the datasource. The updates are performed by changing the items in the scroller internal buffer after they are loaded from the datasource. Items in the buffer can be deleted or replaced with one or more items.

Important! Update datasource to match the scroller buffer content. Keep in mind that the modifications made by the adapter methods are only applied to the content of the buffer. As the items in response to scrolling are pushed out of the buffer, the modifications are lost. It is your responsibility to ensure that as the scroller is scrolled back and a modified item is requested from the datasource again the values returned by the datasource would reflect the updated state. In other words you have to make sure that in addition to manipulating the scroller content you also apply the modifications to the dataset underlying the datasource. Here is the example of such implementation.

Animations

In the fashion similar to ngRepeat the following animations are supported:

Animations are only supported for the updates made via applyUpdates method. Updates caused by scrolling are not going through animation transitions. Usual rules of working with AngularJS animations apply. Look here for an example of animations in the scroller


uiScrollViewport directive

The uiScrollViewport directive marks a particular element as viewport for the uiScroll directive. If no parent of the uiScroll directive is marked with uiScrollViewport directive, the browser window object will be used as viewport.

Usage

<ANY ui-scroll-viewport>
      ...
</ANY>

jqLiteExtras constant

This constant is a class that implements some DOM element methods of jQuery which are currently not implemented in jqLite, namely

These methods are being registered on angular.element during ‘ui.scroll’ module run automatically only if jQuery is not loaded. It is so since ui-scroll v1.6.0. In previous versions there was a separate module ‘ui.scroll.jqlite’ which should have been included in the dependency list of the main app module. So currently we leave ‘ui.scroll.jqlite’ module stub with no content to provide full backward compatibility.


uiScrollTh and uiScrollTd directives

The uiScrollTh and uiScrollTd directives provide a way to build flexible dynamic grids. Handling of grid rows is done by the uiScroll directive itself. In addition to this uiScrollTh and uiScrollTd directive provide tools to programmatically change grid layout, including applying styles to columns, changing column size and order, as well as saving the modifications to the layout and applying previously saved layouts. At this point the above functionality is supported only for table based scrollable grids.

Here is the basic html template for scrollable grid using the uiScrollTh and uiScrollTd directives. Keep in mind that the height of the scroll viewport (in this case the <TABLE> tag) should be constrained. Also, make sure that the initial column widths are applied uniformly to both headers (<TH>) and cells (<TD>)

<TABLE ui-scroll-viewport class="grid">
    <THEAD style="display:block">
       <TR>
         <TH ui-scroll-th class="col1">header 1...</TH>
         <TH ui-scroll-th class="col2">header 2...</TH>
         ...
       </TR>
    </THEAD>
    <TBODY>
       <TR ui-scroll="item in datasource" adapter="adapter">
         <TD ui-scroll-td class="col1">...</TD>
         <TD ui-scroll-td class="col2">...</TD>
         ...
       </TR>
    </TBODY>
</TABLE>

The grid directives have the same dependency requirements as the uiScroll directive itself. To use the directives make sure the ui.scroll.grid module is on the list of the module dependencies. Also you have to load the dist/ui-scroll-grid.js file in your page.

GridAdapter

GridAdapter object (along with ColumnAdapter objects) provides methods and properties to be used to change the scrollable grid layout. A reference to this object is injected as a property named gridAdapterin the scroller adapter.

GridAdapter object implements the following properties:

GridAdapter object implements the following methods:

ColumnAdapter object implements the following methods:


Development

Please feel free to make Pull Requests. Below is the information which could be useful for local developing and contributing.

The ui-scroll sources are in ./src folder. They could not be run as is because of ES6 modules (since v1.6.0), they should be built. The build process includes jshint sources verification, distributive files generating and tests running.

There are some npm scripts available for developing.

1. To run dev-server use

npm start

This should start development server on 5005 port over the ./demo folder. The dev-server proxy is configured to provide work with temporary distributive files (which are being built in-memory each time the sources from ./src have been changed) despite the direct links to public distributive files form ./dist folder. So the dist-folder should stay clear until the development is finished.

2. To run tests in keep-alive mode use

npm test

This runs Karma testing against temporary distributive files that are being built in-memory by the Webpack. We created a number of specifications which consist of more than 200 tests. They are living at the ./test folder. Karma watches both for ./src and ./test folders and automatically re-runs tests after the source code has been changed.

3. To run both dev-server and tests in keep-alive mode use

npm run dev

This is the combination of first two scripts running in concurrently mode. This allows you to work with the ui-scroll examples on 5005 port during continuous tests running.

4. To run full build process use

npm run build

After developing and testing are completed, the build process should be run to a) pass through jshint (both of ui-scroll and test specifications sources), b) generate compressed and uncompressed versions of the ui-scroll distributive in the public ./dist folder, c) run tests over minified distributive files.

Pull Request should include source code (./scr) changes, may include tests (./test) changes and may not include public distributive (./dist) changes.


Change log

v1.8.2

v1.8.1

v1.8.0

v1.7.6

v1.7.5

v1.7.4

v1.7.3

v1.7.2

v1.7.1

v1.7.0

v1.6.2

v1.6.1

v1.6.0

v1.5.2

v1.5.1

v1.5.0

v1.4.1

v1.4.0

v1.3.3

v1.3.2

v1.3.1

v1.3.0

v1.2.1

v1.2.0

v1.1.2

v1.1.1

v1.1.0