angular - Angular 6 材质表服务器和客户端分页
问题描述
我想寻求一些帮助。我必须制作一个通用的角度材料数据表,从 Jira Web 服务中获取数据。我的问题是,在某些情况下,我必须在客户端站点上进行分页,而在某些情况下,我必须在 Jira 上进行,因为它可以返回的最大结果设置为 1000 个元素。
我必须在客户端进行分页的原因是,在许多情况下,图表嵌入在数据表旁边,必须与所有可用元素一起使用,而不仅仅是在材料表。
我想避免的是有更多的参数来检查我是否必须使用客户端/服务器端分页。
相关代码:
import {
Component,
Input,
ViewChild,
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef} from '@angular/core';
import { MatPaginator, MatSort, MatTableDataSource, MatSlideToggle } from '@angular/material';
import { TableService } from './table.service';
import { merge } from 'rxjs';
@Component({
selector: 'app-table-component',
templateUrl: './table.component.html',
styleUrls: ['./table.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class TableComponent implements AfterViewInit {
@Input() dataTypes: any[];
@Input() columnsToDisplay: string[];
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatSlideToggle) toggle: MatSlideToggle;
private dataSource: any = new MatTableDataSource<any>();
private currentPage;
public selectedTab: any;
public resultsLength: number;
constructor(private service: TableService, private ref: ChangeDetectorRef) {}
ngAfterViewInit() {
this.service.changeFilter({
previousPageIndex: 0,
pageIndex: this.paginator._pageIndex,
pageSize: 5,
length: this.paginator._length,
active: this.sort.active,
direction: 'asc',
checked: false
});
merge(this.paginator.page, this.sort.sortChange, this.toggle.change).subscribe(res => {
this.currentPage = this.paginator.pageIndex;
this.service.changeFilter(res);
})
this.service.dataChanged$.subscribe(res => {
this.dataSource.paginator = this.paginator;
this.dataSource.data = res.tickets;
this.dataSource.paginator.pageIndex = this.currentPage;
this.dataSource.paginator.length = res.results;
}
}
这基本上所做的是,在任何事件更改(排序、分页和切换开关上)时,都会将 HTTP 请求发送到 Jira 后端。在大多数情况下,最大结果将是 10-20 个结果,但我有一个屏幕,我必须在其中获取 1000-3000 个结果。在后一种情况下,服务器端分页是我知道的正确方法,但如果结果低于合理数字,我想使用客户端分页,同样因为图表使用材料表中的数据。
在不向表格组件添加另一个 @Input 的情况下,我应该如何解决这个问题?
提前谢谢你们!
编辑 从代码中删除超时以避免混淆
解决方案
推荐阅读
- python - set_author with discord.py 抛出 NameError
- sqlite - 将 2 个表格中的小时和分钟相加并以 HH:MM 显示结果
- r - 将站点构建到网站时,Blogdown 不显示图表
- javascript - casperjs无法使用css选择器找到元素
- c# - 以前的实现是同步的时如何使用异步代码
- spring - jpa Hibernate 使模式名称在实体类中可配置
- vuejs2 - Vuevalidate 异步验证结果到一个循环
- python-3.x - 在函数中调用驱动程序时,如何在 Python3 中保持 Selenium Webdriver 浏览器打开?
- java - 如何安排类和接口以清理我的数据模型中的架构?
- io - WebAssembly 可以做 IO 吗?