Table of Contents generated with DocToc
- New features:
- Get Started
- Getting view port items without events
- Additional elements in scroll
- Use scrollbar of parent block
- Use scrollbar of window
- Items with variable size
- Loading in chunk
- If container size change
- Focus on an item
- Sorting Items
Virtual Scroll displays a virtual, "infinite" list. Supports multi-column. Based on angular2-virtual-scroll by @rintoj
This module displays a small subset of records just enough to fill the viewport and uses the same DOM elements as the user scrolls. This method is effective because the number of DOM elements are always constant and tiny irrespective of the size of the list. Thus virtual scroll can display infinitely growing list of items in an efficient way.
- Angular 4+ compatible module
- Angular Universal/SSR compatible
- Supports multi-column
- Easy to use apis
- OpenSource and available in GitHub
- Added ability to put other elements inside of scroll (Need to wrap list itself in @ContentChild('container'))
- Added ability to use any parent with scrollbar instead of this element (@Input() parentScroll)
Step 1: Install angular2-virtual-scroll
npm install @nomadreservations/virtual-scroll --save
Step 2: Import virtual scroll module into your app module
Step 3: Wrap virtual-scroll tag around list items;
virtual-scroll ="items" ="viewPortItems = $event"list-item *ngFor="let item of viewPortItems" ="item"/list-item/virtual-scroll
You must also define width and height for the container and for it's children.
Step 4: Create 'list-item' component.
Child component is not a necessity if your item is simple enough. See below.
|items||any||The data that builds the templates within the virtual scroll. This is the same data that you'd pass to ngFor. It's important to note that when this data has changed, then the entire virtual scroll is refreshed.|
|childWidth||number||The minimum width of the item template's cell. This dimension is used to help determine how many cells should be created when initialized, and to help calculate the height of the scrollable area. Note that the actual rendered size of the first cell is used by default if not specified.|
|childHeight||number||The minimum height of the item template's cell. This dimension is used to help determine how many cells should be created when initialized, and to help calculate the height of the scrollable area. Note that the actual rendered size of the first cell is used by default if not specified.|
|bufferAmount||number||The the number of elements to be rendered outside of the current container's viewport. Useful when not all elements are the same dimensions.|
|scrollAnimationTime||number||The time in milliseconds for the scroll animation to run for. Default value is 1500|
|parentScroll||Element / Window||Element (or window), which will have scrollbar. This element must be one of the parents of virtual-scroll|
|update||Event||This event is fired every time
|change||Event||This event is fired every time
Getting view port items without events
If you are using AOT compilation (hope you do it) then in classic usage (with listening to
update event) it is required to create field
viewPortItems in your component.
There is a way how to avoid it.
Additional elements in scroll
If inside of content of virtual scroll element you want to show also additional elements except list itself (e.g. search field), you need to specify block with list using id "container".
Use scrollbar of parent block
If you want to use scrollbar of parent block, instead of scrolling block, set
Use scrollbar of window
If you want to use scrollbar of window, instead of scrolling block, set
Items with variable size
Items must have fixed height and width for this module to work perfectly. However if your list happen to have items with variable width and height, set inputs
childHeight to the smallest possible values to make this work.
Loading in chunk
end is fired every time scroll reaches at the end of the list. You could use this to load more items at the end of the scroll. See below.
If container size change
If virtual scroll is used within a dropdown or collapsible menu, virtual scroll needs to know when the container size change. Use
refresh() function after container is resized (include time for animation as well).
Focus on an item
You could use
scrollInto(item) api to scroll into an item in the list. See below:
Always be sure to send an immutable copy of items to virtual scroll to avoid unintended behavior. You need to be careful when doing non-immutable operations such as sorting:
This will be deprecated once Resize Observer is fully implemented.
Contributions are very welcome! Just send a pull request. Feel free to contact me or checkout my GitHub page.
- Paul Ryan (paullryan)
- Rinto Jose (rintoj)
- Pavel Kukushkin (kykint)
The MIT License (MIT) Copyright (c) 2016 Rinto Jose (rintoj) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.