Angular 7 - 虚拟滚动与异步订阅相结合

标签 angular typescript scroll angular7 virtualscroll

我在 Angular 7 项目中使用 async 来自动订阅我想要显示的数据。数据显示为包含约 2000 项的表格。

以下代码来 self 的模板:

<table class="table table-responsive">
  <tr *ngFor="let s of data | async | searchFilter: {keyName: searchText}">
    <td>Display some data: {{s.someProperty}}</td>
  </tr>
</table>

我不清楚如何使用 Angular 7 的这个新功能来仅渲染可视数据,同时仍然使用我的管道async |搜索过滤器:{keyName:searchText}

由于性能原因,我想尝试此功能。

最佳答案

Angular Material 团队在 https://material.angular.io 上开发了一些很好的文档。帮助您成功应用其软件包的任何功能。特别是,您的代码可以轻松更改为使用虚拟滚动。

在您的模块中(app.module.ts 或您的特定功能的模块):

import { ScrollingModule } from '@angular/cdk/scrolling';

@NgModule({
  imports: [
    // other imports,
    ScrollingModule,
  ],
  // other normal module declaration stuff
})
export class AppModule { }

然后,在你的 component.html 中:

<cdk-virtual-scroll-viewport [itemSize]='32'> <!-- or whatever the size of each item is -->
  <table class="table table-responsive">
    <tr *cdkVirtualFor="let s of data | async | searchFilter: {keyName: searchText}">
      <td>Display some data: {{s.someProperty}}</td>
    </tr>
  </table>
</cdk-virtual-scroll-viewport>

注意事项:

  • 您应该使用而不是表行的 *ngFor 指令 使用 *cdkVirtualFor 指令
  • 您必须将整个表格包裹起来 一组标签并指定高度 itemSize(不要忘记 itemSize 两边的括号)
  • 除了使用上面提到的 *cdkVirtualFor 指令之外,您不必更改访问数据的方式;它的设计使用方式与 *ngFor 完全相同

有关使用表格和列表的虚拟滚动的更多信息可以在这里找到:https://material.angular.io/cdk/scrolling/overview#elements-with-parent-tag-requirements

关于Angular 7 - 虚拟滚动与异步订阅相结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53392062/

相关文章:

javascript - 我可以在加载内容时显示组件吗? - Angular2+

angular - 无法将脚本文件添加到组件html

typescript - 如何修复 TS2322 : "could be instantiated with a different subtype of constraint ' object'"?

angular - mat-tab-group 不显示所选默认选项卡的样式

javascript - 在 typescript 中重构一个长函数

javascript - 在 iPhone 面板的幻灯片中启用内容滚动

jquery - 改进 HTML 图片库滚动

html - 滚动文本 - 条目同时显示

angular - 找不到名称为 : '[object Object]' 的控件

reactjs - 使用 TypeScript 在 React.Component 中按名称(字符串)调用函数