如何在Angular 4中创建分页组件? [英] How can I create a pagination component in Angular 4?
本文介绍了如何在Angular 4中创建分页组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个API端点,例如/list/students?page=5&rows=10
,具有分页参数page
和size
.我想创建一个angular2分页组件.
I have a API endpoint like /list/students?page=5&rows=10
with a pagination parameters page
and size
. I want to create a angular2 pagination component.
输入参数将为
- 页面
- 大小
此外,我想使用箭头按钮转到特定的页面和尺寸.
In addition, I want to go to specific page and size with arrow buttons.
如何实现此组件?
推荐答案
您可以使用下面的代码和服务创建分页组件
You can crate a paging component using my below codes and services
app.component.html
app.component.html
<paging-component
[TotalItems]="pagination.TotalItems"
[CurrentPage]="pagination.CurrentPage"
[PageSize]="pagination.PageSize"
[TotalPageLinkButtons]="pagination.TotalPageLinkButtons"
[RowsPerPageOptions]="pagination.RowsPerPageOptions"
(onPageChange)="myChanges($event)"></paging-component>
app.component.ts在下面;
app.component.ts is below;
import {Component} from '@angular/core';
@Component({
selector : 'app-root',
templateUrl: './app.component.html',
styleUrls : ['./app.component.css']
})
export class AppComponent {
pagination = {
TotalItems: 100,
CurrentPage: 1,
PageSize: 10,
TotalPageLinkButtons: 5,
RowsPerPageOptions: [10, 20, 30, 50, 100]
};
/* Paging Component metod */
myChanges(event) {
this.pagination.CurrentPage = event.currentPage;
this.pagination.TotalItems = event.totalItems;
this.pagination.PageSize = event.pageSize;
this.pagination.TotalPageLinkButtons = event.totalPageLinkButtons;
}
}
app.component.module
app.component.module
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HttpModule} from '@angular/http';
import {AppComponent} from './app.component';
import {PagingComponent} from './components/paging-component/paging-component.component';
import {PagingService} from './service/paging-service.service';
@NgModule({
declarations: [
AppComponent,
PagingComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [ PagingService],
bootstrap: [AppComponent]
})
export class AppModule { }
paging-service.service.ts是
paging-service.service.ts is
import { Injectable } from '@angular/core';
@Injectable()
export class PagingService {
/**
* @param totalItems : Total items to be listed
* @param currentPage : Current page number ( Pages starting from 1 not 0)
* @param pageSize : The number of items in the page
* @param totalPageLinkButtons : The number of total page link buttons
* @returns {{
* startPage: number,
* endPage: number,
* startIndex: number,
* endIndex: number,
* totalPageLinkButtons: number,
* totalItems: number,
* currentPage: number,
* pageSize: number,
* totalPages: number,
* pages: (Observable<number>|any)
* }}
*/
getPagingServiceItems(totalItems: number, currentPage: number = 1, pageSize: number = 10, totalPageLinkButtons: number = 5) {
totalItems = totalItems || 1;
/* if currentPage not exists default value will be '1' */
currentPage = currentPage || 1;
/* The default value of the number of items in the page is 10 if not exist */
pageSize = pageSize || 10;
/* The default value of the number of total page link buttons is 10 if not exist */
totalPageLinkButtons = totalPageLinkButtons || 10;
/* calculate total pages */
const totalPages = Math.ceil(totalItems / pageSize);
let startPage: number; // start Page Button number
let endPage: number; // end Page Button number
if (totalPages <= totalPageLinkButtons) {
// less than totalPageButtons then show all
// 1,2,3,.., totalPages are buttons
startPage = 1;
endPage = totalPages;
} else {
// more than totalPageButtons then calculate start and end pages
// currentPage will be on the center of the paging
if (currentPage <= Math.ceil(totalPageLinkButtons / 2)) {
startPage = 1;
endPage = totalPageLinkButtons;
} else if (currentPage + Math.ceil(totalPageLinkButtons / 2) > totalPages) {
startPage = totalPages - totalPageLinkButtons + 1;
endPage = totalPages;
} else {
startPage = currentPage - Math.ceil(totalPageLinkButtons / 2) + 1;
endPage = startPage + totalPageLinkButtons - 1;
}
}
// calculate start and end item indexes
// Indexes are started from 0 ! It is important
const startIndex = (currentPage - 1) * pageSize;
const endIndex = Math.min(startIndex + pageSize - 1, totalItems - 1);
const pages = [];
// create an array of pages to ng-repeat in the pager control
for ( let i = startPage; i <= endPage ; i++) {
pages.push(i);
}
// return object with all paging properties required by the view
return {
startPage : startPage,
endPage : endPage,
startIndex : startIndex,
endIndex : endIndex,
totalPageLinkButtons: totalPageLinkButtons,
totalItems : totalItems,
currentPage : currentPage,
pageSize : pageSize,
totalPages : totalPages,
pages : pages
};
}
}
这篇关于如何在Angular 4中创建分页组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文