Implementing Pagination in Vue: How to Make it Work with Response Data

I am looking to integrate pagination from the response data into my existing code, while also incorporating filters.

JavaScript

var entriesList = new Vue({

    el: "#post-list-template",

    data: {
        posts: [],
        categories: [],
        currentEntries: ''
    },

    created: function () {
        this.fetchData();
    },

    watch: {
        currentEntries: 'fetchData' 
    },

    methods: {
        fetchData: function () {

            var self = this;

            var apiKey = 'my-key';

            axios.get('/wp-json/frm/v2/views/16', {
                    headers: {
                        Authorization: 'Basic '+ btoa( apiKey +':x' )
                    },
                    params: {
                        phouse: self.currentEntries
                    }
                })
                .then((response) => {
                    this.posts = response.data.renderedHtml;
                })
                .catch((e) => {
                    console.error(e)
                })

            //fetch all category data

            axios.get('/wp-json/wp/v2/categories/')
                .then(response => this.categories = response.data);
        }
    } 

})

The pagination html structure in the response is as follows:

<ul class="frm_pagination">
    <li class="active">
        <a href="?frm-page-16=1">1</a>
    </li>
    <li class="">
        <a href="?frm-page-16=2">2</a>
    </li>
    <li class="">
        <a href="?frm-page-16=3">3</a>
    </li> 
    <li class="dots disabled">...</li> 
    <li class="">
        <a href="?frm-page-16=40">40</a>
    </li>
    <li class="">
        <a href="?frm-page-16=2" class="next">&gt;</a>
    </li>
</ul>

To implement functionality, I need to intercept the default behavior of the paginated links, extract the page number upon clicking, and utilize it in my API call like so: /wp-json/frm/v2/views/16/?page=2

Answer №1

To identify a click on a link, you can utilize the mounted hook in this manner:

mounted(){

    this.$el.addEventListener('click', ev => {

        // checking if a pagination link is clicked
        if( !ev.target.matches('.frm_pagination a') ) return;

        ev.preventDefault();

        let page = ev.target.href.match(/rm-page-16=(\d+)/)[1];

        // utilizing the page value;
    })
}

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

Ways to update a nested object by utilizing the setState method

I have a situation where I need to modify the key/value pairs of an object in my state. Specifically, I need to add a key/value pair to the object with an ID of 13. Please refer to the attached photo for reference. Although I know how to use setState, I ...

Construct a new array within a JavaScript constructor function

I have been working on setting up a constructor and trying to initialize an array inside the object that will be created. This specific array is meant to hold multiple objects. function Cluster3DObject(name){ this.name = name; ...

when reloading the browser, clear the console with webpack vue cli

Is there a way to configure the vue-cli-service, which utilizes webpack, to automatically clear the browser's console upon hot reload? Having old and irrelevant messages cluttering the console log after each save can be inefficient. It would be helpf ...

The Angular JS Factory fails to send data back to the controller

When I call the method getPopularMovies in my factory using the controller, it returns undefined. I'm not sure what mistake I've made here. Please help me figure it out. My Factory angular.module('ngMovies').factory('moviesFactor ...

Unable to submit form upon clicking radio button in .NET Core

Trying to submit a form by clicking on a radio button. The JQuery code used for submitting the form: Form: @for (var item = 0; item < Model.Count(); item++) { <form id="myform" action="xx" controller="xxx" method="post"> ...

The arrival of chat featuring Ajax, long-polling, and support for multiple users has finally

Imagine a site with three modules: "links", "home", and "chat". The "links" and "home" modules are static pages that do not require long polling. However, in the "chat" module, new messages may arrive at any time from other users, requiring an immediate up ...

instructions for selecting div id within the same "table td" element using jQuery

Here is the code snippet that I am working with: <td> <div id="div<%# Eval("Id") %>" class="Display"><%# Eval("Display") %></div> <div class="Actions"> </div> <div class="Comment"> <span>Comm ...

Sending form data from a third-party website to Django

Currently, I am running a Django website that holds user information. My goal is to host forms on external websites, such as a newsletter signup form. I want to be able to extract data from a queryset in the URL and send it back to my Django site. At the m ...

I'm having trouble getting Three.js to properly display a scene imported from an OBJ file

I need help troubleshooting why my Three.js scene is not displaying on the HTML canvas. I have successfully imported an OBJ file with all the correct details, such as Meshes and materials. Here is the code snippet that I am using: export function TestSc ...

why doesn't the promise return the record after it has been updated

After completing the form, I proceed to send it as an object to the API NodeJS. The following is the execution function: .post('/create/card', function (req, res) { var rb = req.body, obj = { query: { $set ...

Ajax polling ceases the polling process when an internet connection is lost

My current setup involves a continuous ajax polling cycle where messages are pulled, processed, a wait period occurs, and then the process is repeated. Everything runs smoothly until a user disconnects from the internet, whether it be due to a simple actio ...

Enable contenteditable on table by pressing the tab key

I developed a table entry tool and I would like to be able to input items by pressing the tab key instead of having to manually click on each element. I haven't been able to figure out how to make this function work yet. $('table').on(&apos ...

Distinguish between datalist selection and text input in BootstrapVue Autocomplete

When looking at the code snippet provided below, I'm trying to figure out the appropriate event/method to determine whether the value entered in the input field was manually typed or selected from the <datalist>. SAMPLE CODE: <div> &l ...

Vue combined with modules for namespace management

I seem to be facing a problem that I thought I had solved before by using namespaces, but unfortunately nothing seems to be working. Here's my module: const Algo1Module = { namespaced: true, state: { questions: { question1: "test&qu ...

Losing value in Angular service when the view is changed

I am currently working on a project to create a basic Angular application that retrieves data from Instagram. The concept involves the user inputting a hashtag on the main page, which then redirects them to another page where posts related to that hashtag ...

Using Javascript to extract and organize JSON arrays from various sets of checkboxes

Is there a way to automatically create an array of multiple groups of arrays like this: arr = {arr1:{index:value, index2:value2}, arr2:{index,value, index2:value2}}; The order is based on groups of checkboxes in HTML (see example below): <div class=& ...

Hydration has finished, but there are some discrepancies - Utilizing Ascii art within a vue component

I encountered an issue with displaying ascii art in the {{ name }} section of my component. While developing, a Vue warning popped up: Hydration text content mismatch in <pre> Followed by an error message: Hydration completed but contains mismatch ...

Troubles arising while using ng serve in Angular 2

I'm currently facing an issue during the installation process of an existing Angular application. When trying to run the application using the ng serve command, I encounter the following error message: The "@angular/compiler-cli" package was not prope ...

Refreshing a data object that is shared among Vue components

I've recently started diving into Vue, and I've found myself responsible for tweaking an existing codebase. There's this data.js file that caught my attention, containing a handful of objects holding city information, like: export default { ...

Showing code snippets with possible markup or malicious content as plain text strings

I am facing a challenge with a table on my webpage where values are added through a textbox in a popup modal. However, when I try to add the string ); to the textbox, it triggers a popup instead of being displayed innocuously in its original format on the ...