"Discovering the method to showcase content based on page number or when the 'previous' or 'next'

Here is how my Vue component looks like:

<template>
    ...
        <b-col v-for="item in items"
               v-bind:key="item.id"
               cols="2">
            ...
            <strong slot="header" class="text-dark" :title="item.tittle" >{{item.tittle}}</strong><br/>
            ...
            <strong class="bg-warning text-dark"><i class="fa fa-money"></i> <b>{{item.price}}</b></strong><br/>
            ...
        </b-col>
    ...
        <b-pagination size="sm" :total-rows="itemsPagination.total" :per-page="itemsPagination.per_page" v-model="itemsPagination.current_page" prev-text="Prev" next-text="Next" hide-goto-end-buttons/>
    ...
</template>
<script>
    export default {
        ...
        data () {
          return{
              items: '',
              itemsPagination: ''
          }
        },
        mounted: function () {
            this.getItems()
        },
        methods: {
            getItems() {
                let params = []
                ...
                let request = new Request(ApiUrls.items + '?' + params.join('&'), {
                    method: 'GET',
                    headers: new Headers({
                        'Authorization': 'bearer ' + this.$session.get(SessionKeys.ApiTokens),
                        'Content-Type': 'text/plain'
                    })
                })
                fetch(request).then(r=> r.json())
                    .then(r=> {
                        this.items = r.data
                        this.itemsPagination = r
                    })
                    .catch(e=>console.log(e))
            }
        }
    }
</script>

Upon

console.log(this.itemsPagination)
, the output in browser console appears as shown below:

https://i.sstatic.net/PqOnb.png

This is how the pagination looks on my application interface:

https://i.sstatic.net/QtoKA.png

After executing the script, the item content on page 1 displays correctly. However, switching to subsequent pages does not update the item content. I'm struggling to resolve this issue and make it function properly.

Any suggestions on how to address this problem?

Update

I am using CoreUI

https://github.com/coreui/coreui-free-vue-admin-template/blob/master/src/views/base/Paginations.vue

Answer №1

Attempting to use

v-model="itemsPagination.current_page"
, however, the initialization of itemsPagination is an empty string:

    data () {
      return{
          items: '',
          itemsPagination: ''
      }
    }

Vue lacks the ability to detect property additions or deletions, causing no reactions. It is crucial to initialize itemsPagination as an object that includes (at minimum) current_page:

    data () {
      return{
          items: '',
          itemsPagination: {
            current_page: 1
          }
      }
    }

Update: You have the option to modify the example here. Simply double-click on the upper right corner to make changes and paste this code:

<template>
  <div>
    <h6>Default</h6>
    <b-pagination size="md" :total-rows="100" v-model="itemsPagination.current_page" :per-page="10">
    </b-pagination>
    <br>

    <h6>Small</h6>
    <b-pagination size="sm" :total-rows="100" v-model="itemsPagination.current_page" :per-page="10">
    </b-pagination>
    <br>

    <h6>Large</h6>
    <b-pagination size="lg" :total-rows="100" v-model="itemsPagination.current_page" :per-page="10">
    </b-pagination>
    <br>

    <div>currentPage: {{itemsPagination.current_page}}</div>
  </div>    
</template>

<script>
export default {
  data () {
    return {
      itemsPagination: {
        current_page: 1
      }
    }
  }
}
</script>

<!-- pagination-1.vue -->

Answer №3

I think it's important to ensure that the 'items' are not updated reactively.

Have you considered trying this approach:

data () {
          return{
              container:{ 
              // In my own project, I had to use nested fields for the data object to make Vue.set work
                  items: '',
                  itemsPagination: ''
              }
          }
        },

Then, in the fetch method:

Vue.set(this.container, 'items', r.data); 
// Alternatively, you can use this.$set if arrow functions are not used here and Vue is not accessible

Refer to https://v2.vuejs.org/v2/guide/reactivity.html for more information.

If this doesn't solve the issue, try checking for console.log(this) within your fetch method to see if it belongs to your component.

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

Transforming arrays into nested objects using JavaScript

My array consists of objects structured like this: var exampleArray = [{ name: 'name1', subname: 'subname1', symbolname: 'symbol1' }, { name: 'name1', subname: 'subname11', symbolname: 'sy ...

Tips for accelerating the loading of data retrieved through ajax requests

At present, data is being fetched in array form from a PHP script. I have noticed that when retrieving 40 sets of data, it takes approximately 20 seconds for the data to load. This delay may be due to ajax having to wait until all the results are gathered. ...

Guide on using JavaScript to automatically scroll a HTML page to the top on any mobile browser

Can JavaScript be utilized to smoothly scroll an HTML page to the top? I am looking to achieve this with a stylish animation that functions correctly on all mobile browsers. jQuery is the library I am using on this particular page. Thank you, ...

Using jQuery to target the element before

Is there a way to determine the width of elements located before an element when it is hovered over? I attempted to achieve this using the following code: $('ul li').hover(function() { $(this).prevAll().each(function() { var margin = $(this ...

"An error occurs when attempting to clear Rad Grid data with javascript: htmlfile: Invalid argument thrown

Hello, I am currently developing an ASP.NET application. I am facing an issue where I need to clear the data in a Rad grid upon button click using JavaScript. The code snippet that I have attempted for this is as follows: document.getElementById(&a ...

Creating an AngularJS directive specifically for a certain <div> tag

Recently, I began learning angularjs and came across a script to change the font size. However, this script ended up changing all <p> tags on the entire webpage. Is there a way to modify the font size of <p> tags only within the <div class=" ...

What is the reason for hasChildNodes returning true when dealing with an Empty Node?

When the user clicks on purchase and the cart is empty, there should be an alert. However, it seems that no response is triggered. Upon running the program, an error message appears stating that it cannot read the property of addEventListener which is unde ...

The functionality of JQuery's .hover() is disabled once an HTML onClick() event is activated

I am currently working on a webpage and attempting to incorporate JQuery into it for the first time. However, I seem to be encountering some issues that I believe might have simple solutions. Background Information My JQuery code only contains one event l ...

Mastering the Art of Tab Selection with Jquery

I am trying to implement a tabs control using jQuery. Here is the HTML code I have: <div id="tabs" class="news1"> <ul> <li><a href="#tabs-1">Track</a></li> <li><a href="#tabs-2">History&l ...

Top method for developing a cohesive single-page application

Many websites are incorporating JSON data in string format within their page responses, along with HTML: For example, take a look at The benefit of rendering JSON in string format within the page response is that it allows for the efficient loading of da ...

Slider with Dual Images: A Visual Comparison

I'm currently working on a webpage that features before and after images using a slider based on mouse movement to display both pictures. I am trying to incorporate multiple sliders on the same page but have been facing difficulties in getting them to ...

Obtain document via Angular 2

Is it possible to use TypeScript to download an image that is already loaded? <div *ngIf="DisplayAttachmentImage" class="fixed-window-wrapper_dark"> <button class="btn btn-close-window" (wslClick)="HideAttachmentImage()"> & ...

Assistance with selecting elements using jQuery

I'm facing a challenge with a code that I cannot change. My goal is to introduce a selector that can choose (upon clicking) all elements below it until the next occurrence of the main element. The catch is that they are not nested, just stacked on top ...

Toggle display of divID using Javascript - Conceal when new heading is unveiled

At the moment, I have implemented a feature where clicking on a title reveals its corresponding information. If another title is clicked, it opens either above or below the previously opened title. And if a title is clicked again, it becomes hidden. But ...

I am seeing the object in req.body displayed in reverse order within my Node.js and Express backend application

I encountered an issue while developing a To-do list web application using the MERN stack with Redux. The problem arises when I try to send post data to the backend. In my form, the textfield area is named 'task' (e.g., ) and I input 'jonas& ...

Rotating Images in 3D with CSS

I am looking for guidance on how to create a rotating image effect on a webpage using CSS/JS. The image should rotate into the plane of the page, similar to the example shown in this post. Can you assist me with this? To better illustrate what I'm tr ...

Using JavaScript to convert an image URL to a File Object

Looking to obtain an image file from a URL entered into an input box, leading to the transformation of an image URL into a file object. To illustrate, when selecting a random image on Google Images, you can either copy the Image or its URL. In this scena ...

Struggling to Align NAV buttons to the Right in React Framework

My current Mobile Header view can be seen here: https://i.stack.imgur.com/CcspN.png I am looking to achieve a layout similar to this, https://i.stack.imgur.com/pH15p.png. So far, I have only been able to accomplish this by setting specific left margins, ...

Issues with utilizing Jquery datepicker within a Vue.js component's v-for loop in Laravel

I am facing an issue with the Jquery date picker inside a v-for loop in my vue.js component. The date picker works fine outside of the loop, but inside the loop it does not behave as expected. Here is a snippet of code where the date picker is working cor ...

Tips on including starting information into an angularjs application from the displayed HTML

I'm currently working on a complex AngularJs application that requires User Login. Once the user is authenticated, a page will be displayed to them and additional data will be loaded later. There are two methods for achieving this: XHR Fetch af ...