有一个带有分页的角度材料数据表,用于显示数据,数据为数组形式,问题是当单击下一个处理页面时,索引值再次从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>
有什么方法可以避免分页时出现拼接现象。
答案 0 :(得分:0)
为什么要防止剪接?如果下一页显示的索引值不正确是您的问题,则可以如下更改位置列值,以在下一页显示正确的索引:
<!-- 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>