-
Notifications
You must be signed in to change notification settings - Fork 6.8k
docs(virtual-scroll): add initial documentation #11496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The easiest way to provide a strategy is with one of the directives `itemSize` or `autosize`. | ||
|
||
For some example usages, | ||
[see the demo app](https://github.com/angular/material2/tree/master/src/demo-app/virtual-scroll). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a TODO here to add live examples once this lands in stable?
|
||
### Creating items in the viewport | ||
`*cdkVirtualFor` should be used instead of `*ngFor` to create items in the | ||
`<cdk-virtual-scroll-viewport>`. `*cdkVirtualFor` supports most of the same features as `*ngFor`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it mention which features it doesn't support?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote that because I'm naturally hedgey, but I think it supports all of the same features as *ngFor
, updated to say that.
|
||
#### View recycling | ||
In order to improve performance `*cdkVirtualFor` saves the views it creates in a cache when they are | ||
no longer needed. This way the next time a new view is needed once can be recycled from the cache |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "once" seems out of place here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoops, was supposed to read "one"
In order to improve performance `*cdkVirtualFor` saves the views it creates in a cache when they are | ||
no longer needed. This way the next time a new view is needed once can be recycled from the cache | ||
rather than being created from scratch. The size of this cache can be adjusted using the | ||
`templateCacheSize` input. The cache size defaults to `20` and caching can be disabled by setting it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we should avoid specifying defaults in the docs since they could change and we'll probably forget to update them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its useful for people to know. I'll put it in the jsdoc rather than here though.
connect process. | ||
|
||
### Scrolling over fixed size items | ||
If you're scrolling over a list of items that are all the same fixed size, you can use the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mention the advantages/disadvantages of using a fixed-size viewport?
@@ -435,6 +436,7 @@ export class CdkAutoSizeVirtualScroll implements OnChanges { | |||
* The number of pixels worth of buffer to shoot for when rendering new items. | |||
* If the actual amount turns out to be less it will not necessarily trigger an additional | |||
* rendering cycle (as long as the amount of buffer is still greater than `minBufferPx`). | |||
* The default add buffer px is 200. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defaults to 200px
@@ -425,6 +425,7 @@ export class CdkAutoSizeVirtualScroll implements OnChanges { | |||
/** | |||
* The minimum amount of buffer rendered beyond the viewport (in pixels). | |||
* If the amount of buffer dips below this number, more items will be rendered. | |||
* The default minBufferPx is 100. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defaults to 100px
/** The number of extra elements to render on either side of the scrolling viewport. */ | ||
/** | ||
* The number of extra elements to render on either side of the scrolling viewport. | ||
* The default buffer size is 5. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defaults to 5 elements
@@ -109,6 +109,10 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy | |||
} | |||
} | |||
|
|||
/** | |||
* The size of the cache used to store templates that are not being used for re-use later. | |||
* Setting the cache size to `0` will disable caching. The default cache size is 20. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defaults to 20 templates
**Warning: this component is still experimental. It may have bugs and the API may change at any | ||
time** | ||
|
||
`<cdk-virtual-scroll-viewport>` is used to display a scrolling list containing a large number of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would shorten the lede here and move the bits about the strategies to their own section immediately after this. The section for strategies should talk about why you would use one or the other.
Also some editing for brevity and passive voice:
`<cdk-virtual-scroll-viewport>` displays large lists of elements performantly by only
rendering the items that appear on-screen. This components works with `*cdkVirtualFor`,
a virtualized replacement for `*ngFor`.
</cdk-virtual-scroll-viewport> | ||
``` | ||
|
||
The `autosize` strategy allows the buffer to be configured through two inputs `minBufferPx` and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would break up the paragraph structure a bit here:
The `autosize` strategy is configured through two inputs: `minBufferPx` and `addBufferPx`.
**`minBufferPx`** determines the minimum space outside virtual scrolling viewport that will be filled
with content. Increasing this will increase the amount of content a user will see before more content
must be rendered. However, too large a value will cause more content to be rendered than is necessary.
**`addBufferPx`** determines the amount of content that will be added incrementally as
the viewport is scrolled. This should be greater than the size of `minBufferPx` so that one
"render" is needed at a time.
**Note: The fixed size strategy will likely be changed to allow specifying a separate | ||
`minBufferPx` and `addBufferPx` like the autosize strategy** | ||
|
||
The fixed size viewport strategy is less flexible than the autosize strategy because it requires all |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just explain this once near the beginning of the doc; it's currently mentioned a few times throughout
Because the auto size strategy needs to measure the size of the elements, its performance may not | ||
be as good as the fixed size strategy. | ||
|
||
### Setting the viewport orientation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just #### Viewport orientation
?
`vertical` which virtualizes scrolling along the y-axis. It can be set to `horizontal` to virtualize | ||
scrolling along the x-axis. If you use this option you need to make sure that the content is | ||
actually laid out so that it extends along the x-axis. To do this you may want to target CSS at | ||
`.cdk-virtual-scroll-content-wrapper` which is the wrapper element that contains the rendered |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better/possible to recommend styling their own element instead of a class built into the virtual scroller?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's eventually going to be possible to specify that element themselves, or just allow it to be implicitly created if they don't specify. That's part of the work for making it work with <table>
, <ul>
, etc.
be as good as the fixed size strategy. | ||
|
||
### Setting the viewport orientation | ||
The orientation of the viewport can be adjusted by using the `orientation` input. It defaults to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The virtual-scroll viewport defaults to a vertical orientation, but can also be set to
`orientation="horizontal"`. When changing the orientation, ensure that the item are laid
out horizontally via CSS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like some of the comments from the first pass still apply
time** | ||
|
||
`<cdk-virtual-scroll-viewport>` displays large lists of elements performantly by only | ||
rendering the items that appear on-screen. This components works with `*cdkVirtualFor`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
appear
-> fit
(just occurred to me on second reading)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you push your commits? Still seeing text w/ comments on it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few nits
**Warning: this component is still experimental. It may have bugs and the API may change at any | ||
time** | ||
|
||
`<cdk-virtual-scroll-viewport>` displays large lists of elements performantly by only |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to explain virtual scroll a little bit. I don't think people understand the difference between virtual scroll and infinite scroll. A idea gist.
Virtual scrolling enables developers to render large lists of data in a performant way. Loading hundreds of elements can be slow in any browser, virtual scrolling enables a performant want to simulate all items are rendered by making the height of the container element the total number of elements to be rendered and then only rendering the items in view. Virtual scroll is different from strategies like infinite scroll where it renders a set amount of elements and then when you hit the bottom renders the rest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made some slight tweaks and merged with my sentence above
[see the demo app](https://github.com/angular/material2/tree/master/src/demo-app/virtual-scroll). | ||
|
||
### Creating items in the viewport | ||
`*cdkVirtualFor` replaces `*ngFor` inside of a `<cdk-virtual-scroll-viewport>`, supporting the exact |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The replacement is mentioned right above this. Perhaps remove from one or the other?
connect process. | ||
|
||
### Scrolling over fixed size items | ||
When all items are the same fixed size,, you can use the `FixedSizeVirtualScrollStrategy`. This can |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two commas: fixed size,,
|
||
```html | ||
<cdk-virtual-scroll-viewport itemSize="50"> | ||
<div *cdkVirtualFor="let item of items; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'd be surprised ....
</cdk-virtual-scroll-viewport> | ||
``` | ||
|
||
A `trackBy` function can be specified and works the same as the `*ngFor` `trackBy`. The `index` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this trackBy
is just randomly mentioned at the end here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's because trackBy isn't a context variable, so it didn't fit in the part above, but I do want to mention that it's still supported
portion. | ||
|
||
#### View recycling | ||
To improve rendering performance, `*cdkVirtualFor` caches previously created views after |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth mentioning things like portals that are attached to the VCR can cause issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might even put this at the end too. Its a implementation detail rather than something important for the user to know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be important for the user to know depending on their use case, some components may not deal well with having their templates recycled.
It does sound like a good idea to mention somewhere that the ViewContainer is managed by the virtual-scroller and so any components that attempt to do things with it are going to run into issues. I'm not sure where the best place for this is... In the future I also think we could mark the views that are managed by the virtual scroll in order to play better with other components that may modify the ViewContainer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably put it at the bottom as a note.
content. | ||
|
||
```html | ||
<cdk-virtual-scroll-viewport autosize orientation="horizontal"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd generally discourage users from using autosize, thus removing it as the default on examples like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, we don't want users putting autosize
just because its less typing. If they know the itemSize
it's going to be more performant.
</cdk-virtual-scroll-viewport> | ||
``` | ||
|
||
The fixed size strategy also supports setting the buffer size, i.e. the number of items rendered |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whats the default buffer size?
|
||
```html | ||
<cdk-virtual-scroll-viewport itemSize="50"> | ||
... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the examples should show the for in them, people like to just copy/paste.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add full working embedded example code when I move this out of cdk-experimental
``` | ||
|
||
#### Specifying data | ||
`*cdkVirtualFor` accepts data from an `Array`, `Observable<Array>`, or `DataSource`. The |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd show an example code snippet here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add that once we move it into the cdk proper and have actual embedded examples
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
fixes #10123