我在我的角度应用程序中使用ngb分页,但无法正常工作

问题描述 投票:1回答:1

我已经在表格中的角度应用程序上应用了NGB分页,但是它不起作用。它仅显示2页,而不在表的页之间划分数据。我的代码如下:

import { UserService } from './../services/user.service';
import { Component, OnInit } from '@angular/core';
import { UserInfo } from 'src/app/models/user-info';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.scss']
})
export class UserComponent implements OnInit {

  constructor(private _userService:UserService) { }

  ngOnInit() {
    this.getUserData();

  }


userData=[
{"name":"Akash","email":"[email protected]"},
{"name":"sumit","email":"[email protected]"},
{"name":"mohit","email":"[email protected]"},
{"name":"rajesh","email":"[email protected]"},
{"name":"ramesh","email":"[email protected]"},
{"name":"vikas","email":"[email protected]"},
{"name":"suresh","email":"[email protected]"},
{"name":"ramesh","email":"[email protected]"},
];

elementPerpage:number=5;
currentPage:number=1;
arrayLength:number=this.userData.length;
  userInfo:UserInfo[];

这是我的HTML文件

<h3>User Details</h3>
<div class="table-responsive">
    <table class="table table-striped ">
        <thead>
            <tr>
                <th scope="col">User Name</th>
                <th scope="col">User Email</th>
            </tr>
        </thead>
        <tbody>
            <tr *ngIf="!userData || userData.length === 0">
                <td colspan="2" class="text-center">No records</td>
            </tr>
            <tr *ngFor="let info of userData">
                <td>
                    {{info.name}}
                </td>
                <td>
                   {{info.email}}
                </td>
            </tr>
        </tbody>
    </table>
</div>
<div >
    <ngb-pagination [collectionSize]="arrayLength" [(page)]="currentPage" [pageSize]="elementPerpage">
    </ngb-pagination>
</div>

我已将Bootstrap添加到项目中,没有错误。但是分页不起作用。如何使ngb分页工作?

angular bootstrap-4 bootstrap-modal
1个回答
2
投票

请看the pagination docs。基本用法示例:

<table>
  <tr *ngFor="let item of items | slice: (page-1) * pageSize : (page-1) * pageSize + pageSize">
    <!-- content here -->
  </tr>
</table>

<ngb-pagination
  [(page)]="page"
  [pageSize]="pageSize"
  [collectionSize]="items.length"></ngb-pagination>

* ngFor包含答案(:

© www.soinside.com 2019 - 2024. All rights reserved.