gpt4 book ai didi

pagination - Angular2 分页不起作用

转载 作者:太空狗 更新时间:2023-10-29 17:48:58 26 4
gpt4 key购买 nike

我正在使用 Angular 2 Beta。我还使用 ng2 boostrap 来显示分页。但是我收到一个错误 EXCEPTION: Template parse errors: The pipe 'paginate' could not be found

这是我的代码:

下面显示的是 .ts 文件,我在其中动态加载表中的数据。我还包含了来自 ng-bootsrap 的分页指令。

import {Component, EventEmitter, OnInit, Input} from 'angular2/core';
import {pagination, tableContent} from './interface';
import {CORE_DIRECTIVES} from 'angular2/common';
import {PAGINATION_DIRECTIVES} from './ng2-bootstrap/ng2-bootstrap';


@Component({
selector: 'table-content',
template: `
<pagination [totalItems]="totalItems" [itemsPerPage]='2' (pageChanged)="pageChanged($event)" [(ngModel)]="currentPage" [maxSize]="maxSize" class="pagination-sm" [boundaryLinks]="true"></pagination>
<thead>
<tr>
<th></th><th></th><th></th><th></th><th></th><th></th><th></th>
</tr>
</thead>
<tbody>
<tr *ngFor="#i of tablecontents" >
<td class="text-centre">
<div class="rm-checkbox">
<input type="checkbox" id="" name="" value="1" tabindex="109">
</div>
</td>
<td>{{i.name}}</td>
<td>{{i.email}}</td>
<td>{{i.type}}</td>
<td>{{i.content}}</td>
<td>{{i.priority}}</td>
<td>{{i.TTL}}</td>
</tr>
</tbody>`,
directives: [PAGINATION_DIRECTIVES, FORM_DIRECTIVES, CORE_DIRECTIVES]
})
export class tablecontent implements OnInit {

constructor() {
this.name = 'Rules';
this.ruleCount = 100;
this.tablecontents = 'table.json'
this.totalItems = tablecontents.length;
this.bigTotalItems = tablecontents.length;
this.currentPage = 4;
this.maxSize = 5;
this.bigCurrentPage = 1;
}
perPageCounts: perPageCount[] = [
{ text: '10', value: 1 },
{ text: '25', value: 2 },
{ text: '50', value: 3 },
{ text: '100', value: 4 }
];


private setPage(pageNo: number): void {
this.currentPage = pageNo;
};

private pageChanged(event: any): void {
console.log('Page changed to: ' + event.page);
console.log('Number items per page: ' + event.itemsPerPage);
};
}

有人可以帮忙吗

最佳答案

另一个选项是自定义分页服务而不是 ng2 bootstrap,我刚刚发布了 this pagination example它使用类似谷歌搜索结果的逻辑。

PagerService 处理分页逻辑:

import * as _ from 'underscore';

export class PagerService {
getPager(totalItems: number, currentPage: number = 1, pageSize: number = 10) {
// calculate total pages
var totalPages = Math.ceil(totalItems / pageSize);

var startPage, endPage;
if (totalPages <= 10) {
// less than 10 total pages so show all
startPage = 1;
endPage = totalPages;
} else {
// more than 10 total pages so calculate start and end pages
if (currentPage <= 6) {
startPage = 1;
endPage = 10;
} else if (currentPage + 4 >= totalPages) {
startPage = totalPages - 9;
endPage = totalPages;
} else {
startPage = currentPage - 5;
endPage = currentPage + 4;
}
}

// calculate start and end item indexes
var startIndex = (currentPage - 1) * pageSize;
var endIndex = Math.min(startIndex + pageSize - 1, totalItems - 1);

// create an array of pages to ng-repeat in the pager control
var pages = _.range(startPage, endPage + 1);

// return object with all pager properties required by the view
return {
totalItems: totalItems,
currentPage: currentPage,
pageSize: pageSize,
totalPages: totalPages,
startPage: startPage,
endPage: endPage,
startIndex: startIndex,
endIndex: endIndex,
pages: pages
};
}
}

AppComponent 使用寻呼机服务:

import { Component, OnInit } from '@angular/core';

import * as _ from 'underscore';

import { PagerService } from './_services/index'

@Component({
moduleId: module.id,
selector: 'app',
templateUrl: 'app.component.html'
})

export class AppComponent {
constructor(private pagerService: PagerService) { }

// dummy array of items to be paged
private dummyItems = _.range(1, 151);

// pager object
pager: any = {};

// paged items
pagedItems: any[];

ngOnInit() {
// initialize to page 1
this.setPage(1);
}

setPage(page: number) {
if (page < 1) {
return;
}

// get pager object from service
this.pager = this.pagerService.getPager(this.dummyItems.length, page);

// get current page of items
this.pagedItems = this.dummyItems.slice(this.pager.startIndex, this.pager.endIndex + 1);
}
}

AppComponent HTML 显示分页项和分页器控件:

<div>
<div class="container">
<div class="text-center">
<h1>Angular 2 - Pagination Example with logic like Google</h1>

<!-- items being paged -->
<div *ngFor="let item of pagedItems">Item {{item}}</div>

<!-- pager -->
<ul *ngIf="pager.pages.length" class="pagination">
<li [ngClass]="{disabled:pager.currentPage === 1}">
<a (click)="setPage(1)">First</a>
</li>
<li [ngClass]="{disabled:pager.currentPage === 1}">
<a (click)="setPage(pager.currentPage - 1)">Previous</a>
</li>
<li *ngFor="let page of pager.pages" [ngClass]="{active:pager.currentPage === page}">
<a (click)="setPage(page)">{{page}}</a>
</li>
<li [ngClass]="{disabled:pager.currentPage === pager.totalPages}">
<a (click)="setPage(pager.currentPage + 1)">Next</a>
</li>
<li [ngClass]="{disabled:pager.currentPage === pager.totalPages}">
<a (click)="setPage(pager.totalPages)">Last</a>
</li>
</ul>
</div>
</div>
</div>

有关更多详细信息和工作演示,请访问 this post .

关于pagination - Angular2 分页不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35549258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com