angular - 移除 Angular Material Pagination 中的拼接功能
问题描述
有一个带有分页的角度材料数据表,用于显示数据,数据是数组的形式,问题是当单击下一个句柄页面时,索引值再次从 1 开始,但我需要数据必须继续索引值。
以下是代码:-
import {
Component,
OnInit,
ViewChild
} from '@angular/core';
import {
MatPaginator,
MatTableDataSource
} from '@angular/material';
/**
* @title Table with pagination
*/
@Component({
selector: 'table-pagination-example',
styleUrls: ['table-pagination-example.css'],
templateUrl: 'table-pagination-example.html',
})
export class TablePaginationExample implements OnInit {
displayedColumns: string[] = ['position', 'name'];
public array: any;
public pageSize = 5;
public currentPage = 0;
public totalSize = 0;
dataSource = [{
position: 1,
name: 'Hydrogen'
},
{
position: 2,
name: 'Helium'
},
{
position: 3,
name: 'Lithium'
},
{
position: 4,
name: 'Beryllium'
},
{
position: 5,
name: 'Boron'
},
{
position: 6,
name: 'Carbon'
},
{
position: 7,
name: 'Nitrogen'
},
{
position: 8,
name: 'Oxygen'
},
{
position: 9,
name: 'Fluorine'
},
{
position: 10,
name: 'Neon'
},
{
position: 11,
name: 'Sodium'
},
{`enter code here`
position: 12,
name: 'Magnesium'
},
{
position: 13,
name: 'Aluminum',
},
{
position: 14,
name: 'Silicon'
},
{
position: 15,
name: 'Phosphorus'
},
]
@ViewChild(MatPaginator) paginator: MatPaginator;
ngOnInit() {
this.array = this.dataSource;
this.totalSize = this.dataSource.length;
this.iterator();
console.log(this.totalSize, "total size")
}
// function for pagination
private iterator() {
const end = (this.currentPage + 1) * this.pageSize;
const start = this.currentPage * this.pageSize;
const part = this.array.slice(start, end);
this.dataSource = part;
}
public handlePage(e: any) {
this.currentPage = e.pageIndex;
this.pageSize = e.pageSize;
this.iterator();
}
export interface PeriodicElement {
name: string;
position: number;
weight: number;
symbol: string;
}
<div class="mat-elevation-z8">
<table mat-table [dataSource]="dataSource">
<!-- Position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element let i = index"> {{ i + 1 }} </td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<!-- Weight Column -->
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator #paginator [pageSize]="pageSize" [pageSizeOptions]="[5, 10, 20]" [showFirstLastButtons]="true" [length]="totalSize" [pageIndex]="currentPage" (page)="pageEvent = handlePage($event)">
</mat-paginator>
</div>
有什么办法可以避免分页中的拼接。
解决方案
为什么要防止拼接?如果下一页上显示的索引值不正确是您的问题,那么您可以如下更改您的位置列值以在下一页上显示正确的索引:
<!-- Position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element let i = index"> {{ (pageSize*currentPage) + i + 1 }} </td>
</ng-container>
推荐阅读
- javascript - 使用道具将数据从孩子发送到父母
- azure-data-factory - 如何将整个 SQL 语句传递给 Azure 数据工厂的 ForEach 中的查找活动?
- c - return c 函数只是返回一个奇怪的数字,没有任何原因。所以请任何人向我解释
- windows-10 - 带宽是有限的,没有任何进程实际消耗那么多带宽
- swift - Swift:作为一种类型的协议 X 不能符合“可哈希”
- c# - 在 Xamarin Forms 中运行时更改 Device.StartTimer() 间隔
- javascript - 如何在服务器端处理中支持 Datatable 的客户端排序
- xslt - 如何检查递归步骤的最后一个数字并为小于 n 的递归步骤创建一个空元素?
- python - 使用run_in_executor而不是直接使用executor有什么好处?
- angular - 传递输入值后不更新属性