-
Notifications
You must be signed in to change notification settings - Fork 6.8k
docs(virtual-scroll): add embedded examples #13327
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/material-examples/cdk-virtual-scroll-context/cdk-virtual-scroll-context-example.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.example-viewport { | ||
height: 200px; | ||
width: 200px; | ||
border: 1px solid black; | ||
} | ||
|
||
.example-item-detail { | ||
height: 18px; | ||
} | ||
|
||
.example-alternate { | ||
background: rgba(127, 127, 127, 0.3); | ||
} |
17 changes: 17 additions & 0 deletions
17
src/material-examples/cdk-virtual-scroll-context/cdk-virtual-scroll-context-example.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<cdk-virtual-scroll-viewport [itemSize]="18 * 7" class="example-viewport"> | ||
<div *cdkVirtualFor="let item of items; | ||
let index = index; | ||
let count = count; | ||
let first = first; | ||
let last = last; | ||
let even = even; | ||
let odd = odd;" [class.example-alternate]="odd"> | ||
<div class="example-item-detail">Item: {{item}}</div> | ||
<div class="example-item-detail">Index: {{index}}</div> | ||
<div class="example-item-detail">Count: {{count}}</div> | ||
<div class="example-item-detail">First: {{first ? 'Yes' : 'No'}}</div> | ||
<div class="example-item-detail">Last: {{last ? 'Yes' : 'No'}}</div> | ||
<div class="example-item-detail">Event: {{even ? 'Yes' : 'No'}}</div> | ||
<div class="example-item-detail">Odd: {{odd ? 'Yes' : 'No'}}</div> | ||
</div> | ||
</cdk-virtual-scroll-viewport> |
12 changes: 12 additions & 0 deletions
12
src/material-examples/cdk-virtual-scroll-context/cdk-virtual-scroll-context-example.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {ChangeDetectionStrategy, Component} from '@angular/core'; | ||
|
||
/** @title Virtual scroll context variables */ | ||
@Component({ | ||
selector: 'cdk-virtual-scroll-context-example', | ||
styleUrls: ['cdk-virtual-scroll-context-example.css'], | ||
templateUrl: 'cdk-virtual-scroll-context-example.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class CdkVirtualScrollContextExample { | ||
items = Array.from({length: 100000}).map((_, i) => `Item #${i}`); | ||
} |
9 changes: 9 additions & 0 deletions
9
...terial-examples/cdk-virtual-scroll-data-source/cdk-virtual-scroll-data-source-example.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.example-viewport { | ||
height: 200px; | ||
width: 200px; | ||
border: 1px solid black; | ||
} | ||
|
||
.example-item { | ||
height: 50px; | ||
} |
3 changes: 3 additions & 0 deletions
3
...erial-examples/cdk-virtual-scroll-data-source/cdk-virtual-scroll-data-source-example.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<cdk-virtual-scroll-viewport itemSize="50" class="example-viewport"> | ||
<div *cdkVirtualFor="let item of ds" class="example-item">{{item || 'Loading...'}}</div> | ||
</cdk-virtual-scroll-viewport> |
57 changes: 57 additions & 0 deletions
57
...aterial-examples/cdk-virtual-scroll-data-source/cdk-virtual-scroll-data-source-example.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import {CollectionViewer, DataSource} from '@angular/cdk/collections'; | ||
import {ChangeDetectionStrategy, Component} from '@angular/core'; | ||
import {BehaviorSubject, Observable, Subscription} from 'rxjs'; | ||
|
||
/** @title Virtual scroll with a custom data source */ | ||
@Component({ | ||
selector: 'cdk-virtual-scroll-data-source-example', | ||
styleUrls: ['cdk-virtual-scroll-data-source-example.css'], | ||
templateUrl: 'cdk-virtual-scroll-data-source-example.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class CdkVirtualScrollDataSourceExample { | ||
ds = new MyDataSource(); | ||
} | ||
|
||
export class MyDataSource extends DataSource<string | undefined> { | ||
private length = 100000; | ||
private pageSize = 100; | ||
private cachedData = Array.from<string>({length: this.length}); | ||
private fetchedPages = new Set<number>(); | ||
private dataStream = new BehaviorSubject<(string | undefined)[]>(this.cachedData); | ||
private subscription = new Subscription(); | ||
|
||
connect(collectionViewer: CollectionViewer): Observable<(string | undefined)[]> { | ||
this.subscription.add(collectionViewer.viewChange.subscribe(range => { | ||
const startPage = this.getPageForIndex(range.start); | ||
const endPage = this.getPageForIndex(range.end - 1); | ||
for (let i = startPage; i <= endPage; i++) { | ||
this.fetchPage(i); | ||
} | ||
})); | ||
return this.dataStream; | ||
} | ||
|
||
disconnect(): void { | ||
this.subscription.unsubscribe(); | ||
} | ||
|
||
private getPageForIndex(index: number): number { | ||
return Math.floor(index / this.pageSize); | ||
} | ||
|
||
private fetchPage(page: number) { | ||
if (this.fetchedPages.has(page)) { | ||
return; | ||
} | ||
this.fetchedPages.add(page); | ||
|
||
// Use `setTimeout` to simulate fetching data from server. | ||
setTimeout(() => { | ||
this.cachedData.splice(page * this.pageSize, this.pageSize, | ||
...Array.from({length: this.pageSize}) | ||
.map((_, i) => `Item #${page * this.pageSize + i}`)); | ||
this.dataStream.next(this.cachedData); | ||
}, Math.random() * 1000 + 200); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/material-examples/cdk-virtual-scroll-dl/cdk-virtual-scroll-dl-example.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.example-viewport { | ||
height: 200px; | ||
width: 200px; | ||
border: 1px solid black; | ||
} | ||
|
||
.example-dt { | ||
height: 30px; | ||
font-weight: bold; | ||
} | ||
|
||
.example-dd { | ||
height: 30px; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/material-examples/cdk-virtual-scroll-dl/cdk-virtual-scroll-dl-example.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<cdk-virtual-scroll-viewport class="example-viewport" itemSize="60"> | ||
<dl class="example-dl"> | ||
<ng-container *cdkVirtualFor="let state of states"> | ||
<dt class="example-dt">{{state.name}}</dt> | ||
<dd class="example-dd">{{state.capital}}</dd> | ||
</ng-container> | ||
</dl> | ||
</cdk-virtual-scroll-viewport> |
63 changes: 63 additions & 0 deletions
63
src/material-examples/cdk-virtual-scroll-dl/cdk-virtual-scroll-dl-example.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {ChangeDetectionStrategy, Component} from '@angular/core'; | ||
|
||
/** @title Virtual scrolling `<dl>` */ | ||
@Component({ | ||
selector: 'cdk-virtual-scroll-dl-example', | ||
styleUrls: ['cdk-virtual-scroll-dl-example.css'], | ||
templateUrl: 'cdk-virtual-scroll-dl-example.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class CdkVirtualScrollDlExample { | ||
states = [ | ||
{name: 'Alabama', capital: 'Montgomery'}, | ||
{name: 'Alaska', capital: 'Juneau'}, | ||
{name: 'Arizona', capital: 'Phoenix'}, | ||
{name: 'Arkansas', capital: 'Little Rock'}, | ||
{name: 'California', capital: 'Sacramento'}, | ||
{name: 'Colorado', capital: 'Denver'}, | ||
{name: 'Connecticut', capital: 'Hartford'}, | ||
{name: 'Delaware', capital: 'Dover'}, | ||
{name: 'Florida', capital: 'Tallahassee'}, | ||
{name: 'Georgia', capital: 'Atlanta'}, | ||
{name: 'Hawaii', capital: 'Honolulu'}, | ||
{name: 'Idaho', capital: 'Boise'}, | ||
{name: 'Illinois', capital: 'Springfield'}, | ||
{name: 'Indiana', capital: 'Indianapolis'}, | ||
{name: 'Iowa', capital: 'Des Moines'}, | ||
{name: 'Kansas', capital: 'Topeka'}, | ||
{name: 'Kentucky', capital: 'Frankfort'}, | ||
{name: 'Louisiana', capital: 'Baton Rouge'}, | ||
{name: 'Maine', capital: 'Augusta'}, | ||
{name: 'Maryland', capital: 'Annapolis'}, | ||
{name: 'Massachusetts', capital: 'Boston'}, | ||
{name: 'Michigan', capital: 'Lansing'}, | ||
{name: 'Minnesota', capital: 'St. Paul'}, | ||
{name: 'Mississippi', capital: 'Jackson'}, | ||
{name: 'Missouri', capital: 'Jefferson City'}, | ||
{name: 'Montana', capital: 'Helena'}, | ||
{name: 'Nebraska', capital: 'Lincoln'}, | ||
{name: 'Nevada', capital: 'Carson City'}, | ||
{name: 'New Hampshire', capital: 'Concord'}, | ||
{name: 'New Jersey', capital: 'Trenton'}, | ||
{name: 'New Mexico', capital: 'Santa Fe'}, | ||
{name: 'New York', capital: 'Albany'}, | ||
{name: 'North Carolina', capital: 'Raleigh'}, | ||
{name: 'North Dakota', capital: 'Bismarck'}, | ||
{name: 'Ohio', capital: 'Columbus'}, | ||
{name: 'Oklahoma', capital: 'Oklahoma City'}, | ||
{name: 'Oregon', capital: 'Salem'}, | ||
{name: 'Pennsylvania', capital: 'Harrisburg'}, | ||
{name: 'Rhode Island', capital: 'Providence'}, | ||
{name: 'South Carolina', capital: 'Columbia'}, | ||
{name: 'South Dakota', capital: 'Pierre'}, | ||
{name: 'Tennessee', capital: 'Nashville'}, | ||
{name: 'Texas', capital: 'Austin'}, | ||
{name: 'Utah', capital: 'Salt Lake City'}, | ||
{name: 'Vermont', capital: 'Montpelier'}, | ||
{name: 'Virginia', capital: 'Richmond'}, | ||
{name: 'Washington', capital: 'Olympia'}, | ||
{name: 'West Virginia', capital: 'Charleston'}, | ||
{name: 'Wisconsin', capital: 'Madison'}, | ||
{name: 'Wyoming', capital: 'Cheyenne'}, | ||
]; | ||
} |
9 changes: 9 additions & 0 deletions
9
...rial-examples/cdk-virtual-scroll-fixed-buffer/cdk-virtual-scroll-fixed-buffer-example.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.example-viewport { | ||
height: 200px; | ||
width: 200px; | ||
border: 1px solid black; | ||
} | ||
|
||
.example-item { | ||
height: 50px; | ||
} |
4 changes: 4 additions & 0 deletions
4
...ial-examples/cdk-virtual-scroll-fixed-buffer/cdk-virtual-scroll-fixed-buffer-example.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<cdk-virtual-scroll-viewport itemSize="50" minBufferPx="200" maxBufferPx="400" | ||
class="example-viewport"> | ||
<div *cdkVirtualFor="let item of items" class="example-item">{{item}}</div> | ||
</cdk-virtual-scroll-viewport> |
12 changes: 12 additions & 0 deletions
12
...erial-examples/cdk-virtual-scroll-fixed-buffer/cdk-virtual-scroll-fixed-buffer-example.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {ChangeDetectionStrategy, Component} from '@angular/core'; | ||
|
||
/** @title Fixed size virtual scroll with custom buffer parameters */ | ||
@Component({ | ||
selector: 'cdk-virtual-scroll-fixed-buffer-example', | ||
styleUrls: ['cdk-virtual-scroll-fixed-buffer-example.css'], | ||
templateUrl: 'cdk-virtual-scroll-fixed-buffer-example.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class CdkVirtualScrollFixedBufferExample { | ||
items = Array.from({length: 100000}).map((_, i) => `Item #${i}`); | ||
} |
16 changes: 16 additions & 0 deletions
16
...material-examples/cdk-virtual-scroll-horizontal/cdk-virtual-scroll-horizontal-example.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.cdk-virtual-scroll-data-source-example .example-viewport { | ||
height: 200px; | ||
width: 200px; | ||
border: 1px solid black; | ||
} | ||
|
||
.cdk-virtual-scroll-data-source-example .example-viewport .cdk-virtual-scroll-content-wrapper { | ||
display: flex; | ||
flex-direction: row; | ||
} | ||
|
||
.cdk-virtual-scroll-data-source-example .example-item { | ||
width: 50px; | ||
height: 100%; | ||
writing-mode: vertical-lr; | ||
} |
5 changes: 5 additions & 0 deletions
5
...aterial-examples/cdk-virtual-scroll-horizontal/cdk-virtual-scroll-horizontal-example.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<div class="cdk-virtual-scroll-data-source-example"> | ||
<cdk-virtual-scroll-viewport orientation="horizontal" itemSize="50" class="example-viewport"> | ||
<div *cdkVirtualFor="let item of items" class="example-item">{{item}}</div> | ||
</cdk-virtual-scroll-viewport> | ||
</div> |
13 changes: 13 additions & 0 deletions
13
src/material-examples/cdk-virtual-scroll-horizontal/cdk-virtual-scroll-horizontal-example.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {ChangeDetectionStrategy, Component, ViewEncapsulation} from '@angular/core'; | ||
|
||
/** @title Horizontal virtual scroll */ | ||
@Component({ | ||
selector: 'cdk-virtual-scroll-horizontal-example', | ||
styleUrls: ['cdk-virtual-scroll-horizontal-example.css'], | ||
templateUrl: 'cdk-virtual-scroll-horizontal-example.html', | ||
encapsulation: ViewEncapsulation.None, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class CdkVirtualScrollHorizontalExample { | ||
items = Array.from({length: 100000}).map((_, i) => `Item #${i}`); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/material-examples/cdk-virtual-scroll-overview/cdk-virtual-scroll-overview-example.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.example-viewport { | ||
height: 200px; | ||
width: 200px; | ||
border: 1px solid black; | ||
} | ||
|
||
.example-item { | ||
height: 50px; | ||
} |
3 changes: 3 additions & 0 deletions
3
src/material-examples/cdk-virtual-scroll-overview/cdk-virtual-scroll-overview-example.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<cdk-virtual-scroll-viewport itemSize="50" class="example-viewport"> | ||
<div *cdkVirtualFor="let item of items" class="example-item">{{item}}</div> | ||
</cdk-virtual-scroll-viewport> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: Can be reduced to
new Array(100000).fill(0).map((_, i) =>
Item #${i})
which is slightly shorter.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.
Yeah I originally had that but switched it to this. This feels more readable to me, so I think its better for the purposes of an example.