Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I work on a web application with ASP.NET Core 6 and Angular 13.

This application displays a list of items successfully without any issue.

My issue How to Add Previous and Next buttons Pagination using Angular 13?

What I have tried:

What I tried is:

(1) Create Web API action to display all items

public async Task<IActionResult> GetAll(int pageNumber = 1)
{
    var allitems = _iitem.GetAllItems();

    var result = _pageHelper.GetPage(allitems.AsQueryable(), pageNumber);

    var itemsdata = new ItemsPageViewModel
            {
                items = result.Items,
                Pager = result.Pager
            };

    return Ok(itemsdata);
}
When I call this action method, it returns json data as shown here:

{
    "items": [
        {
            "id": 3,
            "itemNameER": "قلم",
            "itemNameEN": "pen",
            "description": "1"
        },
        {
            "id": 4,
            "itemNameER": "قلم",
            "itemNameEN": "pencil",
            "description": null
        },
        {
            "id": 5,
            "itemNameER": "قلم",
            "itemNameEN": "pen2",
            "description": null
        }
    ],
    "pager": {
        "numberOfPages": 1,
        "currentPage": 1,
        "totalRecords": 3
    }
}
(2) In Angular 13, I created a service in service.ts:

export class ErpServiceService {

  constructor( private http: HttpClient ) { }

  getAll(): Observable<any> {
    return this.http.get(baseUrl);
  }

(3) I created a component items logic in component.ts:

ngOnInit(): void {
    this.retrieveAllItems();
  }
 
 retrieveAllItems(): void {
  this.erpservice.getAll()
    .subscribe(
      data => {
        // Here you need to assign the array inside the data-object:
        this.items = data.items;
        console.log(data);
      },
      error => {
        console.log(error);
      });
}
(4) I created a component view component.html:

<table id="customers">
  <tr>
      <th>Id</th>
      <th>ItemNameAR</th>
      <th>ItemNameEN</th>
      <th>Description</th>
  </tr>
  <tr *ngFor="let item of items">
      <td>{{item.id}}</td>
      <td>{{item.itemNameAR}}</td>
      <td>{{item.itemNameEN}}</td>
      <td>{{item.description}}</td>
  </tr>
</table>
How to apply Previous and Next Buttons pagination on item rows displayed on Angular 13?
Posted
Updated 30-Dec-22 17:42pm

1 solution

Web seems to have many examples like: Angular 13 Pagination example (server side) with ngx-pagination - BezKoder[^]

Above article demonstrates example using ngx-pagination provides NgxPaginationModule for displaying pagination with numbers and responsive style.

There are 2 main things that you would need to do:

1. PaginatePipe: placed at the end of an ngFor expression
HTML
<your-element *ngFor="let item of collection | paginate: { id: 'foo',
                                                          itemsPerPage: pageSize,
                                                          currentPage: page,
                                                          totalItems: total }">...</your-element>
Use the id if you need to support more than one instance of pagination at a time.

2. PaginationControlsComponent: a default component for displaying pagination controls
HTML
<pagination-controls></pagination-controls>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900