javascript - Angular 2 : pagination of a component with table

标签 javascript angular pagination

我有一个 Angular2 模块,它显示一个表格,其中包含从 Webervice 检索到的一些数据。 Web服务每次给我30个结果(以索引作为参数),所以我需要在我的模块上实现一种分页,当用户单击索引时,模块下载新数据并将其显示给用户。 现在我有这个组件:

<table border="1" style="width:100%" *ngIf="messages" >
   <tr>
      <th>Date</th>
      <th>Mesage</th>
   <tr>
   <tr  *ngFor="let message of messages">
      <td>{{message.sendDate}}</td>
      <td>{{message.text}}</td>
   </tr>
 </table>

export class MessageListComponent implements OnInit {
    messages: Message[];

    ngOnInit() {
        this.messages = [];

        this.myServices.getMessages('0').subscribe(
             messages => this.messages = messages,
             error => alert(error),
             () => console.log('ok')
        )
    }
}

getMessage param '0' 给了我第一个结果,所以现在我只能显示 30 个元素。 我怎样才能对所有内容进行分页?

最佳答案

这就是我使用的: 在组件中:

  ngOnInit(){
  this.exampleService.getAll(this.currentPage,10).subscribe(res=>{
                                                this.result=res.content;
                                                this.pages=new Array(res.totalPages);
                                                this.isFirst=res.first;
                                                },
                                                   error=> this.hasError("Erreur Cote Serveur"));
}

next(){
        this.exampleService.getAll((++this.currentPage),10).subscribe(res=>{
                                                this.brandsList=res.content;
                                                this.pages= new Array(res.totalPages);
                                                this.isFirst=res.first;
                                                this.isLast=res.last;
                                                },
                                                   error=> this.hasError("Erreur Cote Serveur"));    
}
previous(){
        this.exampleService.getAll((--this.currentPage),10).subscribe(res=>{
                                                this.brandsList=res.content;
                                                this.pages=new Array(res.totalPages);
                                                this.isFirst=res.first;
                                                this.isLast=res.last;
                                                },
                                                   error=> this.hasError("Erreur Cote Serveur"));    
}

getPage(page:number){
        this.exampleService.getAll(page,10).subscribe(res=>{
                                                this.currentPage=page;
                                                this.brandsList=res.content;
                                                this.pages=new Array(res.totalPages);
                                                this.isFirst=res.first;
                                                this.isLast=res.last;
                                                },
                                                   error=> this.hasError("Erreur Cote Serveur"));    
 }  

在模板中:

        <nav>
          <ul class="pagination">
            <li [class]="isFirst?'disabled':''" class="page-item"><a class="page-link btn"  (click)="previous()">precedent</a></li>
            <ul *ngFor="let page of pages; let i= index" class="pagination">
                    <li    [class]="i===currentPage?'active':''"     class="page-item active">
                                <a class="page-link btn"  (click)="getPage(i)" >{{i+1}}</a>
                    </li>
            </ul>
            <li [class]="isLast?'disabled':''" class="page-item"><a class="page-link btn" (click)="next()" >suivant</a></li>
          </ul>
        </nav>

关于javascript - Angular 2 : pagination of a component with table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42005619/

相关文章:

javascript - 每个语句的 for 循环等效吗?

javascript - 如何测试构造函数中抛出的错误?

javascript - 如何实现/模仿 jQuery UI 的日期选择器?

Laravel 分页在第一页后不起作用

javascript - 如何在 node.js 中解压缩 .gz 字符串

Angular 7 CDK 门户 : Error Must provide a portal to attach

Angular 用参数翻译innerHTML

javascript - 为 Angular 应用程序中的不同构建更改主 css 和 html 文件中的代码行

javascript - Chrome 中的 DOM 操作速度很慢(隐藏/显示元素)

mysql - 如何使用 MySQL 数据库快速创建分页?