Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
angular2-virtual-scroll
Advanced tools
Angular 4+ module for virtual -infinite- list. Supports multi-column
Virtual Scroll displays a virtual, "infinite" list. Supports multi-column.
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 an infinitely growing list of items in an efficient way.
<virtual-scroll [items]="items" (update)="viewPortItems = $event">
<my-custom-component *ngFor="let item of viewPortItems">
</my-custom-component>
</virtual-scroll>
alternatively
<virtual-scroll #scroll [items]="items">
<my-custom-component *ngFor="let item of scroll.viewPortItems">
</my-custom-component>
</virtual-scroll>
alternatively
<div virtualScroll [items]="items" (update)="viewPortItems = $event">
<my-custom-component *ngFor="let item of viewPortItems">
</my-custom-component>
</div>
Step 1: Install angular2-virtual-scroll
npm install angular2-virtual-scroll --save
Step 2: Import virtual scroll module into your app module
....
import { VirtualScrollModule } from 'angular2-virtual-scroll';
....
@NgModule({
...
imports: [
....
VirtualScrollModule
],
....
})
export class AppModule { }
Step 3: Wrap virtual-scroll tag around elements;
<virtual-scroll [items]="items" (update)="viewPortItems = $event">
<my-custom-component *ngFor="let item of viewPortItems">
</my-custom-component>
</virtual-scroll>
You must also define width and height for the container and for its children.
virtual-scroll {
display: block;
width: 350px;
height: 200px;
}
my-custom-component {
display: block;
width: 100%;
height: 30px;
}
Step 4: Create 'my-custom-component' component.
'my-custom-component' must be a custom angular2 component, outside of this library.
Child component is not necessary if your item is simple enough. See below.
<virtual-scroll [items]="items" (update)="viewPortItems = $event">
<div *ngFor="let item of viewPortItems">{{item?.name}}</div>
</virtual-scroll>
Attribute | Type | Description |
---|---|---|
enableUnequalChildrenSizes | boolean | If you want to use the "unequal size" children feature. This is not perfect, but hopefully "close-enough" for most situations. Defaults to false. |
scrollbarWidth | number | If you want to override the auto-calculated scrollbar width. This is used to determine the dimensions of the viewable area when calculating the number of items to render. |
scrollbarHeight | number | If you want to override the auto-calculated scrollbar height. This is used to determine the dimensions of the viewable area when calculating the number of items to render. |
horizontal | boolean | Whether the scrollbars should be vertical or horizontal. Defaults to false. |
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. Use this if enableUnequalChildrenSizes isn't working well enough. (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. Use this if enableUnequalChildrenSizes isn't working well enough. (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 above & below the current container's viewport. Useful when not all elements are the same dimensions. (defaults to enableUnequalChildrenSizes ? 5 : 0) |
scrollAnimationTime | number | The time in milliseconds for the scroll animation to run for. Default value is 750. 0 will completely disable the tween/animation. |
parentScroll | Element / Window | Element (or window), which will have scrollbar. This element must be one of the parents of virtual-scroll |
start | Event | This event is fired every time start index changes and emits ChangeEvent which of format: { start: number, end: number } |
end | Event | This event is fired every time end index changes and emits ChangeEvent which of format: { start: number, end: number } |
update | Event | This event is fired every time the start or end indexes change and emits the list of items which should be visible based on the current scroll position from start to end . The list emitted by this event must be used with *ngFor to render the actual list of items within <virtual-scroll> |
change | Event | This event is fired every time the start or end indexes change and emits ChangeEvent which of format: { start: number, end: number } |
If you are using AOT compilation (I hope you are) then with classic usage (listening to update
event) you are required to create a public field viewPortItems
in your component.
Here's a way to avoid it:
<virtual-scroll #scroll [items]="items">
<my-custom-component *ngFor="let item of scroll.viewPortItems">
</my-custom-component>
</virtual-scroll>
If you want to nest additional elements inside virtual scroll besides the list itself (e.g. search field), you need to wrap those elements in a tag with an angular selector name of #container.
<virtual-scroll [items]="items"
(update)="viewPortItems = $event">
<input type="search">
<div #container>
<my-custom-component *ngFor="let item of viewPortItems">
</my-custom-component>
</div>
</virtual-scroll>
If you want to use the scrollbar of a parent element, set parentScroll
.
<div #scrollingBlock>
<virtual-scroll [items]="items"
[parentScroll]="scrollingBlock.nativeElement"
(update)="viewPortItems = $event">
<input type="search">
<div #container>
<my-custom-component *ngFor="let item of viewPortItems">
</my-custom-component>
</div>
</virtual-scroll>
</div>
Note: The parent element should have a width and height defined.
If you want to use the window's scrollbar, set parentScroll
.
<virtual-scroll
#scroll
[items]="items"
[parentScroll]="scroll.window">
<input type="search">
<div #container>
<my-custom-component *ngFor="let item of scroll.viewPortItems">
</my-custom-component>
</div>
</virtual-scroll>
Items must have fixed height and width for this module to work perfectly. However if you have items with variable width and height, set inputs childWidth
and childHeight
to their smallest possible values.
<virtual-scroll [items]="items"
[childWidth]="80"
[childHeight]="30"
(update)="viewPortItems = $event">
<my-custom-component *ngFor="let item of viewPortItems">
</my-custom-component>
</virtual-scroll>
The event end
is fired every time the scrollbar reaches the end of the list. You could use this to dynamically load more items at the end of the scroll. See below.
import { ChangeEvent } from '@angular2-virtual-scroll';
...
@Component({
selector: 'list-with-api',
template: `
<virtual-scroll [items]="buffer" (update)="scrollItems = $event"
(end)="fetchMore($event)">
<my-custom-component *ngFor="let item of scrollItems"> </my-custom-component>
<div *ngIf="loading" class="loader">Loading...</div>
</virtual-scroll>
`
})
export class ListWithApiComponent implements OnChanges {
@Input()
items: ListItem[];
protected buffer: ListItem[] = [];
protected loading: boolean;
protected fetchMore(event: ChangeEvent) {
if (event.end !== this.buffer.length) return;
this.loading = true;
this.fetchNextChunk(this.buffer.length, 10).then(chunk => {
this.buffer = this.buffer.concat(chunk);
this.loading = false;
}, () => this.loading = false);
}
protected fetchNextChunk(skip: number, limit: number): Promise<ListItem[]> {
return new Promise((resolve, reject) => {
....
});
}
}
Note: This should now be auto-detected, however the 'refresh' method can still force it if neeeded. This was implemented using the setInterval method which may cause minor performance issues. It shouldn't be noticeable, but can be disabled via [checkResizeInterval]="0" Performance will be improved once "Resize Observer" (https://wicg.github.io/ResizeObserver/) is fully implemented.
Refresh method (DEPRECATED)
If virtual scroll is used within a dropdown or collapsible menu, virtual scroll needs to know when the container size changes. Use refresh()
function after container is resized (include time for animation as well).
import { Component, ViewChild } from '@angular/core';
import { VirtualScrollComponent } from 'angular2-virtual-scroll';
@Component({
selector: 'rj-list',
template: `
<virtual-scroll [items]="items" (update)="scrollList = $event">
<div *ngFor="let item of scrollList; let i = index"> {{i}}: {{item}} </div>
</virtual-scroll>
`
})
export class ListComponent {
protected items = ['Item1', 'Item2', 'Item3'];
@ViewChild(VirtualScrollComponent)
private virtualScroll: VirtualScrollComponent;
// call this function after resize + animation end
afterResize() {
this.virtualScroll.refresh();
}
}
You can use the scrollInto(item, alignToBeginning?, additionalOffset?, animationMilliseconds?, animationCompletedCallback?)
api to scroll into an item in the list.
You can also use the scrollToIndex(index, alignToBeginning?, additionalOffset?, animationMilliseconds?, animationCompletedCallback?)
api for the same purpose.
See below:
import { Component, ViewChild } from '@angular/core';
import { VirtualScrollComponent } from 'angular2-virtual-scroll';
@Component({
selector: 'rj-list',
template: `
<virtual-scroll [items]="items" (update)="scrollList = $event">
<div *ngFor="let item of scrollList; let i = index"> {{i}}: {{item}} </div>
</virtual-scroll>
`
})
export class ListComponent {
protected items = ['Item1', 'Item2', 'Item3'];
@ViewChild(VirtualScrollComponent)
private virtualScroll: VirtualScrollComponent;
// call this function whenever you have to focus on second item
focusOnAnItem() {
this.virtualScroll.items = this.items;
this.virtualScroll.scrollInto(items[1]);
}
}
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:
sort() {
this.items = [].concat(this.items || []).sort()
}
Contributions are very welcome! Just send a pull request. Feel free to contact me or checkout my GitHub page.
Follow me: GitHub | Facebook | Twitter | Google+ | Youtube
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.
v0.4.4
FAQs
Angular 4+ module for virtual -infinite- list. Supports multi-column
The npm package angular2-virtual-scroll receives a total of 0 weekly downloads. As such, angular2-virtual-scroll popularity was classified as not popular.
We found that angular2-virtual-scroll demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.