Implementing Vue.js pagination along with making http requests

My table features a pagination-nav component:

<b-pagination-nav :link-gen="linkGen"
  limit="5" :number-of-pages="10" use-router>
</b-pagination-nav>

To retrieve the table content, I am utilizing the getTrades method that makes an HTTP request to a JSON API:

axios.get(`http://localhost:5000/trades/page/${page}`)

Each ${page} corresponds to a specific slice of data. The server-side code and requests are functioning properly. My goal now is to pass the page number from the clicked button to the getTrades method. To achieve this, I invoke the getTrades method within the linkGen method.

linkGen(pageNum) {
  console.log(pageNum);
  this.getTrades(pageNum);
  return pageNum === 1 ? '?' : `?page=${pageNum}`;
},

However, instead of receiving the correct page number, I am getting a random value from the list of page numbers. The linkGen function logs out multiple random values from the list, but it still returns the correct page number.

EDIT: detailed code has been added

Here is the template section:

 <!-- Template Code Goes Here --> 

The script section includes:

 <!-- Script Code Goes Here --> 

An example of the server response looks like:

{
  "status": "success", 
  "trades": [
    {
      "Asset": "GOLD-12.20", 
      "Date": "15.08.2020", 
      "Fee": 1.0, 
      "Operation": "Sell", 
      "Order": 61310215, 
      "Price": 1726.8, 
      "Qty": 1.0, 
      "Time": "21:34:17"
    }, 
    {
      "Asset": "GOLD-12.20", 
      "Date": "15.08.2020", 
      "Fee": 1.0, 
      "Operation": "Buy", 
      "Order": 61310216, 
      "Price": 1726.8, 
      "Qty": 1.0, 
      "Time": "21:34:17"
    }
  ]
}

Answer №1

After some exploration, I managed to find a resolution on my own. The key was utilizing pagination instead of pagination-nav, along with adding an event listener @change

<b-pagination
  v-model="currentPage"
  :total-rows="rows"
  :per-page="perPage"
  first-text="First"
  prev-text="Prev"
  next-text="Next"
  last-text="Last"
  @change="loadPage"
></b-pagination>

Furthermore, within the callback function loadPage, we utilize the page number sent from the button to retrieve the required data from the API.

loadPage(pageNum) {
  this.getTrades(pageNum);
},

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

Retrieve combination values through an AJAX request using ExtJS

My UI is developed using ExtJS, and I have a specific set of tasks that need to be executed when the page loads: Initiate an ajax call to the server to fetch a HashMap. Create a combobox within the main Panel on the page. var combo = Ext.create(' ...

unique color schemes for pie charts with nvd3-angular

I'm attempting to personalize the colors of a pie chart by utilizing the angular-nvd3 extension. I discovered the issue in the plunker example. Although I was able to customize the color of the legend, the chart itself remained unchanged. I would li ...

Passing asynchronous data from method1 to method2 without impacting the functionality of the script responsible for fetching the asynchronous data in method1

When working with TypeScript, I encountered an issue while trying to invoke an external script called SPCalendarPro within a private method that asynchronously fetches data. The script is invoked in the following manner: private _getSPCalendarPro() { con ...

If the session cannot be located, users will be redirected to the sign-in page

I have a small application that utilizes next-auth to display a signin/signout button based on the user's sign-in status. The buttons function correctly and redirect me to the signin page when clicked. However, I am wondering how can I automatically ...

Guide to dividing a URL in reactjs/nextjs

Here is the complete URL: /search-results?query=home+floor&categories=All+Categories. I am looking to separate it into two sections - /search-results and query=home+floor&categories=All+Categories. My objective is to extract the second part of t ...

What is the best way to apply a single slot-element to multiple elements?

Considering a slot structure like: <span slot="?" slot-scope="data>...</span> What placeholder should be used in place of the question mark to make it versatile for any implementation? ...

Organizing menu options into subcategories using jQuery UI for easy sorting

I am currently working on a horizontal jQuery menu item that includes the sortable option and has one submenu. My goals for this project are as follows: To keep the menu item with the submenu locked at the end of the list To be able to drag menu items fr ...

The section element cannot be used as a <Route> component. Every child component of <Routes> must be a <Route> component

I recently completed a React course on Udemy and encountered an issue with integrating register and login components into the container class. The course used an older version of react-router-dom, so I decided to upgrade to v6 react router dom. While makin ...

Restrict user input to only numerical values in an input field using jQuery

Is there a way to create an input field that only accepts integer numbers? Even after using regular expressions, the decimal part of the value still shows up. Here's the function I've tried: $(document).on("input", "input", function(e) { t ...

When a user clicks on JavaScript, it will return true if clicked and false if

Currently working with selenium Java code and incorporating JavaScript execution using the executeScript method, as seen below: WebElement menu = driver.findElement(By.cssSelector(string1)); ((JavascriptExecutor) driver).executeScript("arguments ...

Incorporate the 'noty' javascript library into your Rails 5 project

I am currently attempting to integrate noty.js, a notification library found at , into my Rails application. After installing it using npm: npm install noty I can see the library under node_modules. When I try to implement it in a JavaScript file like t ...

Accessing the Parent Variable from a Function in JavaScript: A Guide

How can you properly retrieve the value of x? let x = 5 const f = (n:number) => { let x = "Welcome"; return x * n // Referring to the first x, not the second one } Also, what is the accurate technical term for this action? ...

Tips for customizing the Bootstrap 4 grid system for a seamless mobile viewing experience

My form consists of 3 steps, each row containing multiple inputs. You can see an example of my form here. Below is a sample of the grid system I have used: <div class="row"> <div class="col"> <input asp-for="First_N ...

Modify the background color of checkboxes without including any text labels

I am looking to customize my checkbox. The common method I usually see for customization is as follows: input[type=checkbox] { display: none; } .my_label { display: inline-block; cursor: pointer; font-size: 13px; margin-right: 15px; ...

Using the useState hook will help avoid any crashes when running on IE11

I recently added a new repository to my GitHub account. The file dist/index.htm is causing Internet Explorer 11 to crash, displaying the error message: "unable to get property 'root' of undefined or null reference." This issue arises when I u ...

Connect with Friends - Using Express, MongoDB, and EJS for a Seamless Friend Connection

I have been working on creating a social network that allows users to send and interact with friend requests. Currently, I have completed the registration, log-in, and "search for other users" functions. Once I find and select another user, I am able to d ...

Ways to focus on a specific div using JavaScript

I am attempting to create a 'snow' effect on the background of a particular div with a red border. Here is the code, but you can also view it here: <!-- language: lang-js --> var width = getWidth(); var height = getH ...

How do the JavaScript thread and the Silverlight UI thread interact with each other?

While JavaScript operates on a single thread, Silverlight does not follow the same restrictions. However, when it comes to the interaction between JavaScript and Silverlight, it is crucial that communication occurs on the Silverlight UI thread. The relati ...

jQuery does not pass data to bootstrap modal

I am currently working with the full calendar feature. Within this framework, I have implemented a modal that allows users to insert new events: <div id="fullCalModal_add_appointment" class="modal fade"> <div class="modal-dialog"> ...

The malfunction of the selenium emulation feature in the node environment

Issue I am trying to implement mobile emulation in Chrome using Selenium with Express. However, despite setting the capabilities for mobile emulation with an Apple iPhone 6 device, Chrome does not open in emulation mode and no errors are displayed. c ...