Skip to content

Commit d1365ff

Browse files
committed
docs: add virtual scrolling examples
Adds examples of virtual scrolling to the CDK and Material tables.
1 parent 6fa67d5 commit d1365ff

File tree

16 files changed

+128
-150
lines changed

16 files changed

+128
-150
lines changed

src/cdk/table/table.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ top of the CDK data-table.
1919
The first step to writing the data-table template is to define the columns.
2020
A column definition is specified via an `<ng-container>` with the `cdkColumnDef` directive, giving
2121
the column a name. Each column definition can contain a header-cell template
22-
(`cdkHeaderCellDef`), data-cell template (`cdkCellDef`), and footer-cell
22+
(`cdkHeaderCellDef`), data-cell template (`cdkCellDef`), and footer-cell
2323
template (`cdkFooterCellDef`).
2424

2525
```html
@@ -120,9 +120,9 @@ cells that are displayed in the column `name` will be given the class `cdk-colum
120120
columns to be given styles that will match across the header and rows.
121121

122122
Since columns can be given any string for its name, its possible that it cannot be directly applied
123-
to the CSS class (e.g. `*nameColumn!`). In these cases, the special characters will be replaced by
123+
to the CSS class (e.g. `*nameColumn!`). In these cases, the special characters will be replaced by
124124
the `-` character. For example, cells container in a column named `*nameColumn!` will be given
125-
the class `cdk-column--nameColumn-`.
125+
the class `cdk-column--nameColumn-`.
126126

127127
#### Connecting the table to a data source
128128

@@ -158,23 +158,31 @@ table how to uniquely identify rows to track how the data changes with each upda
158158
```
159159

160160
##### `recycleRows`
161-
By default, `CdkTable` creates and destroys an internal Angular view for each row. This allows rows
162-
to participate in animations and to toggle between different row templates with `cdkRowDefWhen`. If
163-
you don't need these features, you can instruct the table to cache and recycle rows by specifying
161+
By default, `CdkTable` creates and destroys an internal Angular view for each row. This allows rows
162+
to participate in animations and to toggle between different row templates with `cdkRowDefWhen`. If
163+
you don't need these features, you can instruct the table to cache and recycle rows by specifying
164164
`recycleRows`.
165165

166166
```html
167167
<table cdk-table [dataSource]="dataSource" recycleRows>
168168
```
169169

170+
### Virtual scrolling
171+
172+
If you're showing a large amount of data in your table, you can use virtual scrolling to ensure a
173+
smooth experience for the user. To enable virtual scrolling, you have to wrap the CDK table in a
174+
`cdk-virtual-scroll-viewport` element and add some CSS to make it scrollable.
175+
176+
<!-- example(cdk-table-virtual-scroll) -->
177+
170178
### Alternate HTML to using native table
171179

172180
The CDK table does not require that you use a native HTML table. If you want to have full control
173181
over the style of the table, it may be easier to follow an alternative template approach that does
174182
not use the native table element tags.
175183

176184
This alternative approach replaces the native table element tags with the CDK table directive
177-
selectors. For example, `<table cdk-table>` becomes `<cdk-table>`; `<tr cdk-row`> becomes
185+
selectors. For example, `<table cdk-table>` becomes `<cdk-table>`; `<tr cdk-row`> becomes
178186
`<cdk-row>`. The following shows a previous example using this alternative template:
179187

180188
```html

src/components-examples/cdk/table/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ ng_project(
1212
deps = [
1313
"//:node_modules/@angular/core",
1414
"//:node_modules/rxjs",
15-
"//src/cdk/table",
1615
"//src/cdk/scrolling",
16+
"//src/cdk/table",
1717
],
1818
)
1919

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* Make the container scrollable */
2+
.example-container {
3+
height: 600px;
4+
overflow: auto;
5+
}
6+
7+
.example-container table {
8+
width: 100%;
9+
}
10+
11+
.example-container td,
12+
.example-container th {
13+
height: 48px;
14+
padding: 0;
15+
text-align: left;
16+
}

src/components-examples/cdk/table/cdk-virtual-table/cdk-virtual-table-example.html renamed to src/components-examples/cdk/table/cdk-table-virtual-scroll/cdk-table-virtual-scroll-example.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
<p>Showing {{dataSource.length}} rows</p>
2+
3+
<!-- Enable virtual scrolling -->
14
<cdk-virtual-scroll-viewport class="example-container" [itemSize]="48" [maxBufferPx]="1200" [minBufferPx]="400">
2-
<table class="example-virtual-table" cdk-table [dataSource]="dataSource" [trackBy]="trackBy">
5+
<table cdk-table [dataSource]="dataSource" [trackBy]="trackBy">
36
<!-- Position Column -->
47
<ng-container cdkColumnDef="position">
58
<th cdk-header-cell *cdkHeaderCellDef> No. </th>
@@ -28,8 +31,7 @@
2831
<th cdk-footer-cell *cdkFooterCellDef> Symbol </th>
2932
</ng-container>
3033

31-
<tr cdk-header-row *cdkHeaderRowDef="displayedColumns; sticky: true;"></tr>
34+
<tr cdk-header-row *cdkHeaderRowDef="displayedColumns"></tr>
3235
<tr cdk-row *cdkRowDef="let row; columns: displayedColumns;"></tr>
33-
<tr cdk-footer-row *cdkFooterRowDef="displayedColumns; sticky: true;"></tr>
3436
</table>
3537
</cdk-virtual-scroll-viewport>

src/components-examples/cdk/table/cdk-virtual-flex-table/cdk-virtual-flex-table-example.ts renamed to src/components-examples/cdk/table/cdk-table-virtual-scroll/cdk-table-virtual-scroll-example.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Component} from '@angular/core';
22
import {CdkTableModule} from '@angular/cdk/table';
3-
import {CdkFixedSizeVirtualScroll, CdkVirtualScrollViewport} from '@angular/cdk/scrolling';
3+
import {ScrollingModule} from '@angular/cdk/scrolling';
44

55
export interface PeriodicElement {
66
name: string;
@@ -23,22 +23,22 @@ const ELEMENT_DATA: PeriodicElement[] = [
2323
];
2424

2525
const EXPANDED_ELEMENT_DATA: PeriodicElement[] = [];
26-
for (let x = 0; x < 100; x++) {
26+
for (let x = 0; x < 250; x++) {
2727
for (const entry of ELEMENT_DATA) {
2828
EXPANDED_ELEMENT_DATA.push({...entry, position: entry.position + 10 * x});
2929
}
3030
}
3131

3232
/**
33-
* @title Example of a flex table with virtual scroll enabled.
33+
* @title Example of a CDK table with virtual scroll enabled.
3434
*/
3535
@Component({
36-
selector: 'cdk-virtual-flex-table-example',
37-
styleUrls: ['cdk-virtual-flex-table-example.css'],
38-
templateUrl: 'cdk-virtual-flex-table-example.html',
39-
imports: [CdkTableModule, CdkVirtualScrollViewport, CdkFixedSizeVirtualScroll],
36+
selector: 'cdk-table-virtual-scroll-example',
37+
styleUrls: ['cdk-table-virtual-scroll-example.css'],
38+
templateUrl: 'cdk-table-virtual-scroll-example.html',
39+
imports: [CdkTableModule, ScrollingModule],
4040
})
41-
export class CdkVirtualFlexTableExample {
41+
export class CdkTableVirtualScrollExample {
4242
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
4343
dataSource = EXPANDED_ELEMENT_DATA;
4444
trackBy = (index: number, el: PeriodicElement) => el.position;

src/components-examples/cdk/table/cdk-virtual-flex-table/cdk-virtual-flex-table-example.css

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/components-examples/cdk/table/cdk-virtual-flex-table/cdk-virtual-flex-table-example.html

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/components-examples/cdk/table/cdk-virtual-table/cdk-virtual-table-example.css

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/components-examples/cdk/table/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ export {CdkTableFlexBasicExample} from './cdk-table-flex-basic/cdk-table-flex-ba
22
export {CdkTableBasicExample} from './cdk-table-basic/cdk-table-basic-example';
33
export {CdkTableFixedLayoutExample} from './cdk-table-fixed-layout/cdk-table-fixed-layout-example';
44
export {CdkTableRecycleRowsExample} from './cdk-table-recycle-rows/cdk-table-recycle-rows-example';
5-
export {CdkVirtualFlexTableExample} from './cdk-virtual-flex-table/cdk-virtual-flex-table-example';
6-
export {CdkVirtualTableExample} from './cdk-virtual-table/cdk-virtual-table-example';
5+
export {CdkTableVirtualScrollExample} from './cdk-table-virtual-scroll/cdk-table-virtual-scroll-example';

src/components-examples/material/table/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ export {TableDynamicArrayDataExample} from './table-dynamic-array-data/table-dyn
3030
export {TableDynamicObservableDataExample} from './table-dynamic-observable-data/table-dynamic-observable-data-example';
3131
export {TableGeneratedColumnsExample} from './table-generated-columns/table-generated-columns-example';
3232
export {TableFlexLargeRowExample} from './table-flex-large-row/table-flex-large-row-example';
33+
export {TableVirtualScrollExample} from './table-virtual-scroll/table-virtual-scroll-example';

0 commit comments

Comments
 (0)