Skip to content

Commit

Permalink
feat(module:table): expose CdkVirtualScrollViewport (#3297)
Browse files Browse the repository at this point in the history
close #3144 close #3073 close #2886
  • Loading branch information
vthinkxie authored Apr 18, 2019
1 parent cc1910a commit a942312
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
4 changes: 2 additions & 2 deletions components/table/demo/virtual.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ title:

## zh-CN

虚拟滚动,结合 [cdk scrolling](https://material.angular.io/cdk/scrolling/overview) 的虚拟滚动,用于巨量数据加载。
虚拟滚动,结合 [cdk scrolling](https://material.angular.io/cdk/scrolling/overview) 的虚拟滚动,用于巨量数据加载。可以通过获得 `cdkVirtualScrollViewport` 进行进一步操作,见本示例及 [API](https://material.angular.io/cdk/scrolling/api#CdkVirtualScrollViewport)

## en-US

Virtual scrolling combine with [cdk scrolling](https://material.angular.io/cdk/scrolling/overview) used to display large data.
Virtual scrolling combine with [cdk scrolling](https://material.angular.io/cdk/scrolling/overview) used to display large data, you can get `cdkVirtualScrollViewport` in `NzTableComponent` and find more API [here](https://material.angular.io/cdk/scrolling/api#CdkVirtualScrollViewport).
34 changes: 31 additions & 3 deletions components/table/demo/virtual.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { Component, OnInit } from '@angular/core';
import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { NzTableComponent } from 'ng-zorro-antd';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

@Component({
selector: 'nz-demo-table-virtual',
template: `
<button nz-button (click)="scrollToIndex(200)">Scroll To Index 200</button>
<br />
<br />
<nz-table
nzVirtualScroll
[nzVirtualItemSize]="54"
[nzData]="listOfData"
[nzFrontPagination]="false"
[nzShowPagination]="false"
[nzScroll]="{ x: '1200px', y: '240px' }"
[nzScroll]="{ x: '1300px', y: '240px' }"
>
<thead>
<tr>
<th nzWidth="200px" nzLeft="0px">Full Name</th>
<th nzWidth="100px" nzLeft="200px">Age</th>
<th nzWidth="100px">Index</th>
<th nzWidth="100px">Column 1</th>
<th nzWidth="100px">Column 2</th>
<th nzWidth="100px">Column 3</th>
Expand All @@ -31,6 +38,7 @@ import { Component, OnInit } from '@angular/core';
<tr>
<td nzLeft="0px">{{ data.name }} {{ index }}</td>
<td nzLeft="200px">{{ data.age }}</td>
<td>{{ data.index }}</td>
<td>{{ data.address }}</td>
<td>{{ data.address }}</td>
<td>{{ data.address }}</td>
Expand All @@ -48,16 +56,36 @@ import { Component, OnInit } from '@angular/core';
</nz-table>
`
})
export class NzDemoTableVirtualComponent implements OnInit {
export class NzDemoTableVirtualComponent implements OnInit, AfterViewInit, OnDestroy {
@ViewChild(NzTableComponent) nzTableComponent: NzTableComponent;
private destroy$ = new Subject();
listOfData: any[] = [];

scrollToIndex(index: number): void {
this.nzTableComponent.cdkVirtualScrollViewport.scrollToIndex(index);
}

ngOnInit(): void {
for (let i = 0; i < 20000; i++) {
this.listOfData.push({
index: i,
name: `Edward King`,
age: 32,
address: `London`
});
}
}

ngAfterViewInit(): void {
this.nzTableComponent.cdkVirtualScrollViewport.scrolledIndexChange
.pipe(takeUntil(this.destroy$))
.subscribe(data => {
console.log('scroll index to', data);
});
}

ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
}
2 changes: 2 additions & 0 deletions components/table/nz-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export class NzTableComponent<T = any> implements OnInit, AfterViewInit, OnDestr
@ViewChild('tableBodyElement', { read: ElementRef }) tableBodyElement: ElementRef;
@ViewChild('tableMainElement', { read: ElementRef }) tableMainElement: ElementRef;
@ViewChild(CdkVirtualScrollViewport, { read: ElementRef }) cdkVirtualScrollElement: ElementRef;
@ViewChild(CdkVirtualScrollViewport, { read: CdkVirtualScrollViewport })
cdkVirtualScrollViewport: CdkVirtualScrollViewport;
@ContentChild(NzVirtualScrollDirective) nzVirtualScrollDirective: NzVirtualScrollDirective;
@Input() nzSize: NzSizeMDSType = 'default';
@Input() nzShowTotal: TemplateRef<{ $implicit: number; range: [number, number] }>;
Expand Down
4 changes: 3 additions & 1 deletion scripts/site/_site/doc/app/share/nz-codebox/stack-blitz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ import 'zone.js/dist/zone'; // Included with Angular CLI.
const appModuleCode = `
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { DragDropModule } from '@angular/cdk/drag-drop';
import { ScrollingModule } from '@angular/cdk/scrolling';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientJsonpModule, HttpClientModule } from '@angular/common/http';
Expand All @@ -100,7 +102,7 @@ const antDesignIcons = AllIcons as {
const icons: IconDefinition[] = Object.keys(antDesignIcons).map(key => antDesignIcons[key])
@NgModule({
imports: [ BrowserModule, FormsModule, HttpClientModule, HttpClientJsonpModule, ReactiveFormsModule, NgZorroAntdModule, BrowserAnimationsModule ],
imports: [ BrowserModule, FormsModule, HttpClientModule, HttpClientJsonpModule, ReactiveFormsModule, NgZorroAntdModule, BrowserAnimationsModule, ScrollingModule, DragDropModule ],
declarations: [ ${componentName} ],
bootstrap: [ ${componentName} ],
providers : [ { provide: NZ_I18N, useValue: en_US }, { provide: NZ_ICONS, useValue: icons } ]
Expand Down

0 comments on commit a942312

Please sign in to comment.