【问题标题】:Why pagination with Bootstrap does not work?为什么使用 Bootstrap 进行分页不起作用?
【发布时间】:2021-08-22 22:49:11
【问题描述】:

我试图通过以下链接使用分页:

https://ng-bootstrap.github.io/#/components/table/examples(在分页下)

它只是不能正常工作。 事实上,如果我想在第一页上只有 3 行(然后我通过将其设置为 3 来更改 pageSize),我会得到所有行 (4) 而不是 3。

我哪里做错了?谢谢大家的帮助

ts 文件

import { Product } from '../product';
import { Observable, Subject } from "rxjs";
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import {map} from 'rxjs/operators';

import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-product-list',
  templateUrl: './product-list.component.html',
  styleUrls: ['./product-list.component.css']
})
export class ProductListComponent implements OnInit {

  constructor(private productservice: ProductService, private fb: FormBuilder, private modalService: NgbModal) {} 

  productsArray: any[] = [];
  dtTrigger: Subject<any> = new Subject();

  products: Product[] = [];
  product: Product = new Product();
  productlist: any;
  page = 1;
  pageSize = 4;
  collectionSize = 8;


  ngOnInit() {
      this.productservice.getProductList().subscribe(data => {
          this.products = data;
          this.dtTrigger.next();
          this.refreshProduct();
      })
      this.editProfileForm = this.fb.group({
          productcode: [''],
          name: ['']
      });
  }

  refreshProduct() {
      this.productsArray = this.products
          .map((product: any, i: number) => ({ id: i + 1, ...product }))
          .slice((this.page - 1) * this.pageSize, (this.page - 1) * this.pageSize + this.pageSize);
  }

html文件

<div class="panel-body">
    <table class="table table-hover table-sm">
      <thead class="thead-light">
        <tr>
          <th>Product code</th>
          <th>Product name</th>
          <th>Product image</th>
          <th>Action</th>

        </tr>
      </thead>
      <tbody>
        <tr *ngFor="let product of products">
          <td>{{product.productcode}}</td>
          <td>{{product.name}}</td>
          <td class="py-1"><img src="data:image/png;base64"  width="80" height="80"></td>
          <td><button (click)="deleteProduct(product.productcode)" class='btn btn-primary'><i
                class="fa fa-futboll-0">Delete</i></button>
            <button type="button" class="btn btn-primary" (click)="openModal(editProfileModal, product)" >Update</button>
          </td> 
        </tr>
      </tbody><br>
    </table>
    <div class="d-flex justify-content-between p-2">
      <ngb-pagination [collectionSize]="collectionSize" [(page)]="page" [pageSize]="pageSize" (pageChange)="refreshProducts()">
      </ngb-pagination>
    </div>
  </div>
</div>

【问题讨论】:

  • 这个 .slice((this.page - 1) * this.pageSize, (this.page - 1) * this.pageSize + this.pageSize);每次分页返回 0 ?
  • 我不知道这个,因为我是新手。我认为错误在那里,因为它不起作用。你建议我最终做什么?我不希望错误出现在 id = i + 1 ... 产品中

标签: angular bootstrap-4 pagination


【解决方案1】:

您需要将 ngFor 中的 products 更改为 productsArray。

“产品”将始终包含完整列表。你总是使用“products”来填充“productsArray”,具体取决于你的需要,以及你想在 UI 上显示的内容。

【讨论】:

  • 对,我错过了。我非常感谢你。最后一个问题。是否可以添加两个按钮让我分别直接返回第一页或最后一页?
  • 没问题!是的,你绝对可以。例如转到最后:您只需更改 page = {yourLastPageIndex} 并调用 refreshProduct.
  • 好的。或者我看到您可以添加 {boundaryLinks] = "true" 并自动将它们添加到已经存在的其他按钮中。我注意到一个小问题;在链接中发布的示例中,它将 collectionSize = COUNTRIES.length;为了找到最大的元素数。我尝试自己在代码中输入 product.length 并返回 0,实际上我手动输入了数字 8。我该如何纠正这最后一件事?
  • 在“this.products = data;”之后把“this.collectionSize=this.products.length”
  • 总是离开 collectionSize = product.length 对吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-20
  • 2022-06-10
  • 1970-01-01
  • 2020-12-27
相关资源
最近更新 更多