Guide on navigating to a specific page with ngx-bootstrap pagination

Is there a way to navigate to a specific page using ngx-bootstrap pagination by entering the page number into an input field?

Check out this code snippet:

***Template:***

<div class="row">
  <div class="col-xs-12 col-12">
    <div class="content-wrapper">
      <p class="content-item" *ngFor="let content of returnedArray">{{content}}</p>
    </div>
    <pagination [totalItems]="contentArray.length" (pageChanged)="pageChanged($event)"></pagination>
  </div>
</div>

**.ts file**
import { Component, OnInit } from '@angular/core';
import { PageChangedEvent } from 'ngx-bootstrap/pagination';

@Component({
    // eslint-disable-next-line @angular-eslint/component-selector
    selector: 'demo-pagination-content-switching',
    templateUrl: './content-switching.html',
    styles: [`
.content-wrapper {
border: 1px solid #ddd;
border-radius: 4px;
padding-left: 10px;
margin-bottom: 10px;
}
`]
})
export class DemoPaginationContentSwitchingComponent implements OnInit {
    contentArray = new Array(90).fill('');
    returnedArray?: string[];

    ngOnInit(): void {
        this.contentArray = this.contentArray.map((v: string, i: number) => `Content line ${i + 1}`);
        this.returnedArray = this.contentArray.slice(0, 10);
    }

    pageChanged(event: PageChangedEvent): void {
        const startItem = (event.page - 1) * event.itemsPerPage;
        const endItem = event.page * event.itemsPerPage;
        this.returnedArray = this.contentArray.slice(startItem, endItem);
    }
}

If you have any tips or guidance on this matter, it would be greatly appreciated!

Thank you in advance.

Answer №1

Based on the information provided in the official documentation, it is recommended to bind the pagination element with [(ngModel)] to ensure synchronization with a specific instance member.

To implement this in your

DemoPaginationContentSwitchingComponent
, you should first declare a variable called currentPage and connect it to Angular Bootstrap's pagination component like so:

<pagination [totalItems]="contentArray.length (pageChanged)="pageChanged($event)" [(ngModel)]="currentPage"></pagination>

Next, include an input element that will be used to control the pagination. You can handle changes by listening for the (changed) event on the input and adjusting the value of

currentPage</code accordingly, or by adding a <code>button
for this purpose.

Regardless of the approach chosen, it is essential to validate the input to prevent any potential errors originating from the pagination component.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Customizing service providers for each individual test in Angular

When testing Ngrx Effects, I am trying to override a service provider on a per-test basis in order to simulate both success and failure responses. In my attempt to achieve this, I have included my service in the TestBed setup like so: describe('Accou ...

Issue with ESLint error in TypeScript PrimeReact async Button click handler

I am currently facing an issue with exporting data from a DataTable in PrimeReact. The onClick function for the Button does not allow async callbacks as flagged by eslint. Can someone guide me on how to properly call this function? const exportCSV = us ...

Is there a way to switch between showing and hiding all images rather than just hiding them one by one?

Is there a way I can modify my code to create a button that toggles between hiding and showing all images (under the user_upload class), instead of just hiding them? function hidei(id) { $('.user_upload').toggle(); Any suggestions would be grea ...

The Angular 2 error message states that the type 'Observable<{}>' is not compatible with the assigned type

Observable is having trouble being assigned, my package.json file contains "angular2": "2.0.0-beta.17", "systemjs": "0.19.24", "es6-promise": "^3.0.2", "es6-shim": "^0.35.0", "reflect-metadata": "0.1.2", "rxjs": "5.0.0-beta.6", "zone.js": "0.6.12" The is ...

Running a TypeScript file on Heroku Scheduler - A step-by-step guide

I have set up a TypeScript server on Heroku and am attempting to schedule a recurring job to run hourly. The application itself functions smoothly and serves all the necessary data, but I encounter failures when trying to execute a job using "Heroku Schedu ...

Disabling end date options in an Angular Material date range picker based on the selected start date

I am currently using an Angular Material 15 date range picker and it is functioning properly. I can select a start date and an end date, which are then displayed in the associated input fields. However, I am facing an issue with setting the minimum and m ...

Troubleshooting a problem with a personalized webkit scrollbar in Chrome

Using the CSS property scroll-snap-type: y mandatory; to customize my scrollbar with the following styles: ::-webkit-scrollbar { width: 12px; background: transparent; } ::-webkit-scrollbar-track { background-color: rgba(0, 0, 0, 0.5); -webkit-box-s ...

What is the best way to iterate through the result of an HTTP request in Angular 11?

I am a beginner with Angular and currently working in Angular 11. I am facing issues with making an http request. Despite going through numerous Stack Overflow posts, none of the solutions seem to work for me, even though some questions are similar to mine ...

Twofold Arrow Styling in CSS

Can someone help me achieve this design: https://i.stack.imgur.com/eBQ1a.png I am attempting to create a double arrow using just one class. I have tried various methods, but nothing seems to be working for me. If anyone has any suggestions or can point ...

Incorporate Canvg version 4 into a TypeScript project

I am currently facing an issue with an older TypeScript project that has the following tsconfig setup: { "compilerOptions": { "baseUrl": "./src", "outDir": "build/dist", "module": &q ...

I am completely baffled by the meaning of this Typescript error

In my code, there is a button component setup like this: export interface ButtonProps { kind?: 'normal' | 'flat' | 'primary'; negative?: boolean; size?: 'small' | 'big'; spinner?: boolean; ...

The dynamically rendered component cannot be assigned to the type 'IntrinsicAttributes & ContentOutlineProps & ContentBrainstormProps'

I am facing an issue on my page where a <SideBar /> component is causing a Typescript error with the setActivePage hook. The error message points to a specific line in my code: const Content: (({ question_blocks, }: ContentBrainstormProps) => JSX. ...

Angular - Set value on formArrayName

I'm currently working on a form that contains an array of strings. Every time I try to add a new element to the list, I encounter an issue when using setValue to set values in the array. The following error is displayed: <button (click)="addNewCom ...

The custom FontAwesome icon is failing to display properly

UPDATE : Here is the StackBlitz as requested. A custom SVG icon was created based on instructions provided by FontAwesome's collaborator found here. Despite mirroring the exact element structure of FontAwesome's icons, the custom icon does not ...

Is there a way to conceal 'private' methods using JSDoc TypeScript declarations?

If we consider a scenario where there is a JavaScript class /** * @element my-element */ export class MyElement extends HTMLElement { publicMethod() {} /** @private */ privateMethod() {} } customElements.define('my-element', MyElement) ...

What is the best way to include multiple ng-repeats within a single ng-repeat?

Hey, I'm facing quite a tricky situation with this grid and could use some assistance. I'm trying to figure out how to populate the data using ng-repeat. I've attached an image showcasing the desired layout of the grid and the order in which ...

Ways to ensure that both the Search text field and search button are of equal height

I am facing an issue with the layout of my HTML code within my ASP.NET MVC web application. Below is the snippet of code causing the problem: <form class="customSearch" method="GET" action="@Url.Action("Search", "Home")"> <input class="searchIn ...

What are the steps to adjust image size using CSS in an email signature?

Having trouble with my email signature. It appears fine on my end, but when the recipient replies, the images are oversized and the css is not rendering correctly in their response. I'm puzzled as to why this is happening. Here's the code snippe ...

Is it possible to utilize the output of a function to determine the styling of a div element in Vue?

Hi, I'm trying to use the v-bind:style in my div to apply the textposit function with the textpos prop as a parameter. The function should adjust the style of the div based on the value of the parameter. <div class="container" :style=&qu ...

Ensuring Unique CSS for Diverse Devices: A User-Agent Driven Approach

I am facing multiple challenges and working towards finding solutions. I have developed a webpage and now I want to redirect the link to the CSS file based on the user agent. `<script type="text/css" src="style.css"> </script>` However, inst ...