Issues with pagination in Vue.js due to miscalculation

Within my application, there is a section for pagination that utilizes data sourced from a REST API. However, I have encountered an issue where the pagination starts counting from page 0 instead of page 1. This results in displaying "0 of 9" initially and reaching "8 of 9" at the end, rather than starting with "1 of 9" and ending with "9 of 9". Below is a snippet of the current implementation:

HTML

<p>Page {{page}} of {{pageCount}}</p>

JS

data: function() {
    return {
      page: 0
    };
  },

computed: {
    pageCount() {
      let l = this.result.length,
        s = this.size;
      return Math.floor(l / s);
    },
    paginated() {
      const start = this.page * this.size,
        end = start + this.size;
      return this.result.slice(start, end);
    }
  },

Could it be possible that the calculation using the Math.floor method is causing this issue?

Answer №1

Your page variable starts from 0 instead of 1, which is fine for the pagination function to work correctly. However, when displaying it to users, you can simply add 1 to make it more intuitive for them.

<p>Page {{page + 1}} of {{pageCount}}</p>

Answer №2

Based on my interpretation, it seems that your pageCount() function is accurate as you do have a total of 9 pages. The issue may lie in the value of your page variable, although I am unable to pinpoint where this variable is being sourced from. One possible solution could be to increment the variable by adding 1.

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

Error: The fetch API encountered an unexpected termination before completing the request

A new issue has come up in my project, and although I found a similar problem on this link, it does not address my specific issue. What I have set up is a straightforward API using nodejs, express-framework, and mongoose. The issue lies with the fetch API ...

JavaScript Error: Unable to execute getJsonData due to function not found

I am encountering an issue with a function that retrieves JSON data from a URL Here is the code snippet: var retrieveJsonData = function(uri,callback){ $.ajax({ type: "GET", dataType: "jsonp", url: uri, jsonpCallback: 'r ...

React's router activeClassName feature fails to apply the active class to child routes

<ul className="right hide-on-med-and-down"> <li><IndexLink to="/" activeClassName="active">ABOUT</IndexLink></li> <li><Link to="blog" activeClassName="active">BLOG</Link></li> <li><Link t ...

Guide on showcasing file content in a modal popup within a Kendo Grid

Currently, I am facing an issue with displaying the content of a JSON file within a modal window. All I can manage to do is display the file name as a link, but what I really want is to display the actual content of the file. Do you have any ideas on how ...

Unable to update state in my React app when clicking on a button

There is a requirement for a button click to filter the job-card array by one specific category. For example, clicking on the button "Marketing" should only show jobs from the array that have the property "jobstags: Marketing". I followed a similar procedu ...

Deleting an element from a JavaScript array

I have a collection of javascript functions that manage intervals by setting and clearing them. The intervals are stored in an array called intervals[]. config: { settings: { timezone: 'Australia/Perth,', base_url: location.p ...

Displaying the response from the render function inside a div using express

In my current project, I am utilizing nodejs with express and attempting to load an ejs file into a div element as shown below: app.js (server side) //SHOW app.get("/courses/:id", function(req,res){ console.log("requested"); res.render('cour ...

The process of embedding variables within a JSON Array function in JavaScript

As a newcomer to JavaScript, I am facing an issue while trying to create a simple program. I am attempting to store the variables 'name', 'document', and 'code' inside the JSON array called 'records'. var records = ...

Javascript: Understanding Error Handling in the Context of Async Await

I am looking to strengthen my logical reasoning, not diving into abstract concepts. Scenario 1: try { var result = await new IamErrorAlways() if (result && result instanceof Error) return result // Is this the appropriate error handling method? } ca ...

Prevent body scrolling when modal view is activated

As I work on developing this website, I have encountered an issue. Upon clicking a link located at the far right end of the second row in the header, a modal window appears with embedded YouTube videos. The problem arises when scrolling through the modal: ...

Cookie authentication with Wp-api 2

I'm currently working on a php page within WordPress (using WP-Api 2): <?php /** * Template Name: WP-Api */ add_action("wp_enqueue_scripts", "enqueue_"); function enqueue_() { wp_localize_script( 'wp-api', 'wpApiSettings&apos ...

Converting a TypeScript class to a plain JavaScript object using class-transformer

I have a few instances of TypeScript classes in my Angular app that I need to save to Firebase. However, Firebase does not support custom classes, so I stumbled upon this library: https://github.com/typestack/class-transformer which seems to be a good fit ...

Ensuring consistent geometry size regardless of renderer size is crucial - Plane resizing based on screen dimensions

https://i.sstatic.net/CwUxD.gif Here is a codepen showcasing the issue along with a GIF illustrating the confusion. Codepen: https://codepen.io/carelesscourage/pen/bGMWjNg My expectation I desire the plane to maintain the same size regardless of screen ...

Securing RESTful APIs with stringent security measures

Lately, I've been delving into using modern front end technologies such as React and Angular. This has led me to explore tools like JSON Server for simulating restful database interactions. I've noticed that most REST APIs require authentication ...

What is the best way to invoke a computed property using dynamic styling?

Looking for a way to dynamically style an Ant Design Vue button within a table row of an Ant Design table? <template #status="{ text }"> <a-button ghost :style="{'border-color': getColor(text) }"> </a-b ...

Generating nested arrays using Vue.js

Calculating the total costs of products selected by a user for each company is my current task. Below is the code snippet I am using: let companiesProductPrices = []; let prices = []; this.selectedCompaniesDetails.forEach ...

Collaborating with multiple forms and files in PHP using jQuery and AJAX operations

Need help with the title for this question. There are two input fields and buttons in index.php <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST"> <input type="text" class="for ...

Issue with Nuxt: Data inaccessible within a method

Incorporating CKEditor 5 + CKFinder (Modal Mode) to initiate image selection through the @click event presents a challenge in accessing data within the onInit function block. Below is the function in question: data() { return { post: { ...

Positioning a div beside another div without containing it

Check out the snippet below. I am looking to vertically center an element, 'modal', over another div, 'element', regardless of its position or margins. However, I cannot place the 'modal' div inside the 'element' di ...

Ways to organize JSON data from a fetch request into multiple divisions

I have written a JavaScript code to fetch JSON information. I plan on storing this JSON file locally (I downloaded an example file and added a birthdate object for my usage example from https://jsonplaceholder.typicode.com/users) My goal is to parse the r ...