【问题标题】:Ng-Bootstrap Pagination in Angular 4Angular 4 中的 Ng-Bootstrap 分页
【发布时间】:2018-03-17 01:32:41
【问题描述】:

我正在尝试在 Angular 4 中实现分页,但它仍然不起作用。我使用了一项服务来订阅我的数据并将其输出到 html 中,我想通过在其中添加分页来限制它。下面是我的代码。

ts

export class ProductListComponent implements OnInit {
  closeResult: string;
    products: any;
    subscription: Subscription;

  constructor(private modalService: NgbModal, private productService: ProductsService) { }
    page :number = 1;
  ngOnInit() {
    this.subscription = this.productService.getAll()
        .subscribe(
          (data:any) => {
            this.products = data;
          },
          error => {

          });
  }
}

html

<tbody>
  <tr *ngFor="let product of products">
    <td>{{ product.name }}</td>
    <td>{{ product.desc }}</td>
    <td>{{ product.qty }}</td>
    <td>{{ product.price }}</td>
    <td>
      <button type="button" class="btn btn-sm btn-primary"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Edit</button>
    </td>
  </tr>
</tbody>
</table>
<nav class="pull-right">
  <ul class="pagination pagination-sm">
    <ngb-pagination [collectionSize]="50" [(page)]="currentPage" size="sm"></ngb-pagination>
  </ul>
</nav>

【问题讨论】:

    标签: angular pagination ng-bootstrap angular-bootstrap


    【解决方案1】:

    您必须对 pageProducts 执行 *ngFor

    <tr *ngFor="let product of pageProducts">
        <td>{{ product.name }}</td>
         ...
    
    //And in your module:
    currentPage:number=1;
    get pageProducts(){
       return this.products.slice((currentPage-1)*10,currentPage*10);
    }
    

    【讨论】:

    • 我的订阅数据会怎样?如何使用此分页连接到我的订阅数据?
    • 您必须维护您的 productService 订阅(此订阅是否填写“this.products”)。使用“getter”强制角度来询问“pageProduct”,它负责只给你你想要的项目
    • 是的 this.products 是“让产品的产品”。你能编辑你的代码并将我的代码和你的代码放在一起吗?非常感谢
    • 我对你的回答有点困惑。你必须有: products;currentPage;get pageProduct(){..}constructor(..){}ngOnInit(this.productService.getAll().subscribe((data:any) => {this.products = data;} ..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    相关资源
    最近更新 更多