Implementing dual pagination components in Vue JS for dynamic updates on click

Having two pagination components on a single page is convenient for users to navigate without scrolling too much. One component is placed at the top and the other at the bottom.

The issue arises when I switch to page 2 using the top component, as the bottom component still shows the current page as 1. The same inconsistency occurs when using the bottom component. Changes made in one component are not reflected in the other simultaneously.

I aim to synchronize both components so that selecting page 2 on the top component automatically updates the bottom component to display page 2 as well, and vice versa.

Could you please provide guidance on how to resolve this inconsistency?

Here is a snippet of my code with a live preview available here: https://jsfiddle.net/c9wp2k63/107/

The pagination component utilized is: https://github.com/matfish2/vue-pagination-2

<div id="app">
  <h2>Vue Pagination 2</h2>
  <p>Selected page: {{page}}</p>
  <h3>Top Pagination</h3>
  <pagination :records="10000" :per-page="100" @paginate="setPage">
 </pagination>

 <h3>Bottom Pagination</h3>
 <pagination :records="10000" :per-page="100" @paginate="setPage">
 </pagination>
</div>



    new Vue({
    el: "#app",
    components: {
        Pagination
    },
    data: {
        page: 1
    },
    methods: {
        setPage: function(page) {
            this.page = page;
        }
    }
});

Answer №1

Upon examining the library's code ([1], [2]), it seems that the recommended approach is to utilize refs to access the components, followed by invoking Pagination.setPage(page) on them.

However, there is a caveat to be mindful of - when calling Pagination.setPage(page), it triggers the paginate event once again, necessitating the inclusion of an if statement to prevent a potential stack overflow error.

For an updated version, check out this revised JSFiddle.

Steps breakdown:

Firstly, add ref="topPagination" and ref="bottomPagination" to the template:

<div id="app">
  <h2>Vue Pagination 2</h2>
  <p>Selected page: {{page}}</p>
  <h3>
  Top Pagination
  </h3>
  <pagination :records="10000" :per-page="100" @paginate="setPage" ref="topPagination">
  </pagination>

  <h3>
  Bottom Pagination
  </h3>
   <pagination :records="10000" :per-page="100" @paginate="setPage" ref="bottomPagination">
  </pagination>
</div>

Then, include the necessary if condition and $refs...setPage():

new Vue({
  el: "#app",
  components: {
    Pagination
  },
  data: {
    page: 1
  },
  methods: {
    setPage: function(page) {
      if (page === this.page) { /* avoid stack overflow */ return; }
      this.page = page;
      this.$refs.bottomPagination.setPage(page);
      this.$refs.topPagination.setPage(page);
    }
  }
});

Answer №2

I managed to make it work, although I'm not entirely certain if this is the most optimal approach :) https://jsfiddle.net/c9wp2k63/144/

new Vue({
  el: "#app",
  components: {
    Pagination
  },
  data: {
    page: 1,
  },
  methods: {
    setPageFirst: function(page) {
        const isNewPage =  this.page != page
      this.page = page;

      if (isNewPage){
                this.$refs.secondPagination.setPage(page)
      }
    },
    setPageSecond: function(page) {
        const isNewPage =  this.page != page

      this.page = page;
      if (isNewPage){
         this.$refs.firstPagination.setPage(page)
       }
    }
  }
});

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

Passing the value of the selected calendar date to the controller

How can I pass the value generated by this calendar_date_select to the controller? Any suggestions on how to modify the onchange code? <%= calendar_date_select_tag "meeting_date_1", @time, :embedded => true, :time => true, :minut ...

A step-by-step guide on utilizing moment.js to format the data provided by a vuetify v-text-field (with the type: time)

Currently, I have set up this element in the code: <v-text-field label="Choose a time" type="time" mask="time" step="1800" prepend-inner-icon="access_time" v-model="expiryTime" :rules="[v => !!v || 'Time is required']" requ ...

Ways to block past dates on bootstrap date picker starting from the current date and how to prevent dates beyond 90 days in the future from being selected on the

I am currently facing an issue with disabling previous dates from the current date in my code. While the functionality works well for the "From Date" date picker, I am having trouble implementing the same restriction for the "To Date" date picker after 90 ...

Creating a JSON schema in JavaScript using an already established JSON framework

I have a json structure stored in a variable called "data" that looks like this: { "SearchWithMasterDataDIdAndScandefinitionDAO": [ { "dateDm_id": 20120602, "issueValue": "ELTDIWKZ", "scanName": "Company Stored as Person (Give ...

Running a Vue.js application on your local machine and executing Cypress tests against it in sequence via an npm script'être

Recently, I have been faced with the challenge of setting up and running a local Vue.js app on localhost:3000 and then executing Cypress tests against it. Within my package.json file, I have included some npm scripts which are necessary for this process: ...

Getting the value of a lookup in an alert or console within a Material table in React

I am currently integrating a material table into my project and I have encountered an issue. Instead of getting the name of a city, I am receiving numbers like 63 or 32 in alerts or console logs. For reference, here is the link to the CodeSandbox: https:/ ...

Tips for adding content to a textarea with JavaScript without disrupting the editing history

I have a specific requirement where I want the user to be able to highlight text in a textarea, then press ctrl + b to automatically surround that selected text with stars. Here is what I envision happening: 1) The initial content of the textarea is: "he ...

Error: Firebase is not recognized as a valid function

I have been attempting to go through the firebase Node tutorial at this URL: Running my node.js app leads to a crash with a "TypeError: Firebase is not a function" error. Here is an excerpt from my index.js file: var Firebase = require("firebase"); var f ...

Incorporating a TypeScript module into a JavaScript module within a React application

I'm encountering an issue with my React app that was created using create-react-app. I recently added a Typescript module to the project, which is necessary for functionality reasons. Although it will remain in Typescript, I made sure to install all t ...

Searching for parameters wrongly triggering the id on a different route

Having recently delved into mongoose, I must apologize in advance for any misuse of terminology on my part. Below is the content of my routes file: const express = require('express'); const router = express.Router(); const passport = require(&a ...

Set up the configuration for express to access an external API through proxy.conf.json while in production mode

I am currently managing two applications on Heroku, one being myserverapi (built with Spring Boot) and the other being a client-side Angular app named client. The server is hosted at myserver.heroku.com while the client resides at myclient.heroku.com. At t ...

A guide on incorporating a React custom package within an Angular directive

Hey there, I have an Angular v1.x application and a React custom package. I'm looking to integrate the React custom package into my Angular directive, so that it can display a React component. Here's some code snippet: Angul ...

Mastering the usage of AngularJS Directive controllerAs syntax with scope is key to developing

I have included my code below: // HTML <body> <h1>{{foo.name}}</h1> <my-directive></my-directive> </body> // Scripts app.directive('myDirective', function() { return { restrict: 'E', ...

Constructing an interactive table from a JSON dataset using Node.js and Express

Just starting out with Node.JS here! I'm currently working on creating a page with multiple tables and information using NodeJS. However, I've hit a roadblock when trying to display the result of an SQL query in an HTML table. Right now, I'm ...

Utilizing Node.js to Retrieve Data from MySQL

Hi there, I'm new to NodeJS and I'm curious about how to fetch a MySQL query like we do in PHP. $query = mysql_query("SELECT * FROM accounts"); while($fetch = mysql_fetch_array($query)) { echo $fetch['Username']; } How would this be ...

Could you provide instructions on how to change mjs files into js format?

Is there a method to change .mjs files to .js files? Some hosting services do not yet support mjs files, so I am interested in converting them to js files. Context: Mozilla's PDFjs has incorporated JavaScript modules (mjs) into their code, but since ...

I'm looking to add a next and previous button within my jumbotron- can anyone offer guidance?

As I near the completion of my Full Stack Nanodegree final project and await assistance from my instructor, I've taken on the task of developing my portfolio. However, I'm facing a challenge with implementing next and previous buttons within my j ...

A guide on retrieving the values of all child elements within an HTML element using Puppeteer

I have been exploring the capabilities of puppeteer and am trying to extract the values from the column names of a table. <tbody> <tr class="GridHeader" align="center" style="background-color:Black;"> <td cl ...

AngularJS: Importing a parent directive within a child directive

Take a look at this Plunk example to understand better. I'm attempting to create a test scenario for accessing complex directives, but I encounter an error when trying to call a method from the parent directive: Parent Directive app.directive(&apos ...

Facing challenges in implementing a logic to showcase an intricate page on express.js (node.js) platform

Having trouble setting up a functional logic to display a detailed page showcasing the post title, image, and details. Currently, I've successfully created a list page that displays all the posts by the logged-in user with the title, image, and detail ...