What is the best way to control the number of pages displayed in pagination?

I have implemented a pagination component in React, but encountered an issue where all pages were being displayed, causing the page to expand and require horizontal scrolling. Is there a way to limit the display to only 20 pages while still allowing for more to be added dynamically without taking up excessive space?

I attempted some logic using:

if (numberOfPages > 20 ) { return }

The variable numberOfPages represents the total number of pages.

I am unsure of the best approach to achieve this task.

Below is the code snippet I am currently working with:


const Pagination = ({ numberOfPages, page, setPage }: any) => {
  const pageNumbers = (numberOfPages: number) => {
    let pages = [];
    for (let i = 1; i <= numberOfPages; i++) {
      pages.push(
        <span
          key={i}
          onClick={e => setPage(i)}
          className={page === i ? "active" : ""}
        >
          {i}
        </span>
      );
        // if (numberOfPages > 20 ) {
        //      return 
        // }
    }
    return pages;
  };

  return (
    <div className="pagination">
      <div className="page-numbers">
        <span
          onClick={e => (page === 1 ? {} : setPage(page - 1))}
          className={page === 1 ? "disabled" : ""}
        >
          &laquo;
        </span>
        {pageNumbers(numberOfPages)}
        <span
          onClick={e => (page === numberOfPages ? {} : setPage(page + 1))}
          className={page === numberOfPages ? "disabled" : ""}
        >
          &raquo;
        </span>
      </div>
    </div>
  );
};

export default Pagination; 

Answer №2

Although it's not strictly React-related, the principles can still be applied and the logic extracted. I recently tackled a similar problem that may help you.

Note: The pageIndex refers to the currentPageIndex, starting from 0. Pages returned in array start with 1 for better user experience.

function calculateLastPageIndex(totalItems, pageSize) {
    return Math.floor((totalItems + pageSize - 1) / pageSize) - 1;
}

function generatePageIndexOptions(totalItems, maxOptions, pageSize, currIndex) {
    const options = [];
    const pivot = Math.ceil(maxOptions / 2);
    const lastPageIdx = calculateLastPageIndex(totalItems, pageSize);

    if (lastPageIdx <= maxOptions) {
        while(options.length < lastPageIdx) {
            options.push(options.length + 1);
        }
    } else if (currIndex < pivot) {
        while(options.length < maxOptions) {
            options.push(options.length + 1);
        }
    } else if (currIndex > (lastPageIdx - pivot)) {
        while(options.length < maxOptions) {
            options.unshift(lastPageIdx - options.length + 1);
        }
    } else {
        for (let i = currIndex - (pivot - 1); options.length < maxOptions; i++) {
            options.push(i + 1);
        }
    }

    return options;
}

generatePageIndexOptions(100, 5, 15, 0) // returns [1, 2, 3, 4, 5]
generatePageIndexOptions(100, 5, 15, 4) // returns [3, 4, 5, 6, 7]

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

The alignment of the Bootstrap Radio white circle is off when adjusting the HTML font size

I'm currently working on a simple web application using Bootstrap for styling. While I really like the overall look of Bootstrap, I've run into an issue with the appearance of my radio buttons when I adjust the font size in my HTML. Here is a sn ...

Querying MongoDB using aggregation to find documents with a specific date range difference

I have been working on setting up a cron job to synchronize my documents. The goal is for the job to attempt syncing a specified number of times, but only after waiting at least 2 hours since the last attempt. Each document has a "lastSyncAt" field that I ...

The document.write function causes the page to break

I have created a view that loads another view asynchronously and then replaces the current one using document.write (yes, I want to replace the whole page!): $.ajax({ type: "GET", url: myUrl, data: null, async: true, success: function (result) { ...

css setting the dimensions of list items in HTML

Welcome to my page at . Notice how the "Storia del Bernese" menu voice is positioned above the other list items. I would like all list items to have the same height but different widths based on text length. What adjustments should I make in my CSS to ach ...

Avoiding drops in frame rates caused by socket.io in Node.js

I'm currently working on creating animations using html5 canvas. My goal is to have these animations replicated on any client connected to my server. To achieve this, I send the function to be called along with its arguments as a string and then use e ...

Recurring loop in React while making a GET request

I'm facing an issue with my React code. I need to send a GET request to the backend, which will return a boolean value in response. Within the useEffect hook, I want to check this boolean value and if it's TRUE, display something in the console. ...

Is there a method to redirect and display a JavaScript message efficiently?

Once the user has updated a record, I want to automatically redirect them to another page where they will be notified with a JavaScript message (alertify.notify()) confirming that the update was successful. I am unsure if it's possible to dynamically ...

Transfer information from the client to the server using AJAX and PHP by

When attempting to post a JavaScript variable called posY to a PHP file, an error occurred with the message: Notice: Undefined index: data in C:\xampp\htdocs\Heads_in_the_clouds\submitposY.php The posY variable is defined in the JavaSc ...

Setting a background image for your Ionic page

I am relatively new to using Ionic and I'm currently working on setting up a gradient image as the background for the welcome page of my app. While I've been successful in loading images from the assets/imgs folder on other pages, I'm facing ...

Generate a customizable URL for my search bar

I have developed a single HTML page with full AJAX functionality, displaying all content including form submits, main page, form results, and more through various DOM manipulations. The issue I am currently encountering is related to a search box where use ...

Retrieve the title and display it as a caption using JavaScript

Below is a snippet of code that takes the title from an element with the class a.preview, appends it to another element with the ID p#preview, and generates some AJAX content on hover. While the title is printing elsewhere, the variable c in this script ...

Regular expression in Javascript to match a year

I'm still learning javascript and I have a question. How can I determine if a specific piece of text includes a four digit year? Here's an example: var copyright = $('#copyright').val(); if \d{4} appears in copyright: take ac ...

What is the best way to design three flexible divs using display: table without the use of JavaScript?

https://i.sstatic.net/bEC4Y.png Is it possible to create a responsive layout with three divs that behave in the following way: When the viewport is narrow, the three divs stack on top of each other When the viewport is average, the first div spans full ...

React Redux: Discrepancy in Variable Value Between Internal and External Function within Custom Hook

Encountering a challenge with a custom React hook utilizing Redux, where a variable's value inside and outside a function within the same hook is inconsistent. Simplified code snippet provided below: import { useAppSelector } from "Redux/helpers& ...

Creating a simple layout in React-Native with two text components perfectly centered within a single View component

Struggling with aligning two distinct text elements within their components using react-native and native-base. Unable to center the text both vertically and horizontally within the Text component itself. Segregated colors to visualize the issue. The comp ...

Utilize the identical ReactJS hook across multiple components

I've created a hook that pulls data from an API and stores the result in my component's state using setAllCommunitiesFromSponsor. Now I want to use this same hook in another component. Instead of copying and pasting, what is the best way to imp ...

Issues with the play() method in Javascript across Firefox, Safari, and IE 11 are causing functionality problems

I developed a basic video and audio player that smoothly fades out an introductory image and starts or stops playing an mp4 file upon click. Everything functions properly in Chrome, but unfortunately does not work in other major browsers. Despite checking ...

Guide to displaying a pop-up window message once the crossword puzzle is successfully solved using JavaScript

I have made some changes to this puzzle according to my preference, but I would like to display a message upon completing the crossword. Is there a way to accomplish this? Any helpful suggestions are greatly appreciated. Here is the GitHub link for refere ...

A guide on choosing a custom button color and automatically reverting to its original color when another button is clicked

I have a collection of 24 buttons, all in a dark grey (#333333) shade. Whenever I click on one of the buttons, it changes to a vibrant blue color (#0099ff), which is functioning correctly. However, when I proceed to click on another button, the previous ...

Foundational 5 Grid Shuffle Shift

I am currently utilizing Foundation 5 and am aiming to create a unique DIV layout on a mobile display: -------------------- | A | -------------------- | B | -------------------- | C | -------------------- | ...