Bug found in Vue.js app: search result pagination not working as expected

I've been developing a small frontend application that showcases a list of users in an HTML5 table using Bootstrap 3, Axios, and Vue.js 2. The user data is fetched from a JSON API and displayed in a paginated manner.

While implementing a search/filter functionality to the application, I encountered an issue with pagination not working properly for both the entire dataset and the search results. Here's a snippet of the code:

// Your Vue.js script goes here
// Your custom CSS styles go here
// Your HTML template goes here

After adding the search input field and updating the v-for loop to iterate over either displayedUsers or searchResults, I implemented the filtering logic but faced challenges in integrating pagination with search functionality. The resulting application can be accessed HERE.

If you have any advice on how to resolve this issue and make the pagination work seamlessly with search functionality, I'd appreciate your input.

Answer №1

To ensure the pagination functions properly alongside the filtering, it is necessary to integrate the pagination logic with the searchResults in the displayedUsers method.

displayedUsers() {
    return this.paginate(this.searchResults);
},

Subsequently, you must utilize displayedUsers throughout where a combined result is desired, such as in your template:

<tr v-for="(user, index) in displayedUsers">

Another aspect that requires correction in your code is that the page count currently always reflects the original user count, which should be updated to reflect the "current" user count:

setPages(users) {
    this.pages.length = 0; //we need to clear the previously set pages
    var numberOfPages = Math.ceil(users.length / this.perPage);
    for (var index = 1; index <= numberOfPages; index++) {
       this.pages.push(index);
    }
},

Additionally, update the pages whenever the displayedUsers are modified:

watch: {
    displayedUsers() {
        this.setPages(this.searchResults);
    }
},

If you also wish to reset the page when conducting a search, simply define the page within the searchResults method:

searchResults() {
    this.page = 1;
    return this.users.filter((user) => {
        return user.name.first.match(this.search);
      });
}

See the working example on JSFiddle.

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

An issue with repetitive looping in eventHandler in Javascript

Within an HTML page, I've implemented Javascript code that displays elements on a form when a user clicks the + button and removes elements when the - button is pressed. However, I encountered an error "TypeError: docs[n] is undefined" after following ...

The process of integrating Tailwind elements into NextJs version 13

Can anyone help me integrate Tailwind elements into my NextJs project using JavaScript instead of TypeScript? I tried following the documentation, but the navbar component's expand button doesn't work. It seems like all components are having some ...

What are the semantics behind implementing Basic Authentication in AngularJS and Spring Boot?

Currently going through a tutorial that involves AngularJS and Spring Security. The AngularJS navigation controller triggers an authenticate function upon page load: var authenticate = function(credentials, callback) { var headers = credentials ? { ...

Extract particular information from the JSON reply

When working with a JSON response in JavaScript, I use the following code to convert it to a string: var myObject = JSON.stringify(data); Although this code successfully prints out the results, I am having trouble extracting specific data such as myObjec ...

Utilize Dinero.js to implement currency formatting for input fields in React applications

I am currently working on a form in React that requires certain input fields to be formatted as money currency. These inputs should be prefixed with the dollar sign ($), include commas for thousands separation, and have exactly two decimal points. During ...

Axios promise handler in Vue Js is limited to processing only a single statement at a time

I'm currently utilizing Vue Js in my Laravel project to execute a POST request. Everything seems to be functioning properly, but I'm encountering some issues while dealing with the promise: axios.post('/customer/update',this.custo ...

Is there a reason why this MD Bootstrap Snippet isn't functioning properly?

When zooming out to 25% or beyond, I noticed that the toolbar unexpectedly pops open and refuses to close. How can I prevent this from happening? I asked this question yesterday but unfortunately received only downvotes :( Appreciate any help provided ...

What is the best way to have one modal trigger the opening of another modal in Angular 4+

On my main webpage, there is a modalA (form) that appears when clicked. Within this modalA, there is a button that triggers the opening of another modalB (form). If I decide to cancel the modalB, I want it to return to modalA instead of going back to the ...

Trouble with disabling default actions and transferring text

When the user clicks on loginAccount, the intention is to extract the text from the element with the id input-1 and assign it to username. This same process should occur for password, followed by form submission. However, despite using e.preventDefault() ...

Using Angular 2 to access information from the OpenWeather API

Trying to integrate weather data from an openweather API has presented a challenge. The object received contains an array of 40 objects representing the weather forecast for the next 5 days with a 3-hour interval. The issue lies in displaying this 5-day fo ...

Use jQuery to dynamically capture and store the value of data-rowindex into an array

Is there a way to dynamically store the data-rowindex value into an array? <tr class="ewTableRow" data-rowindex="1" id="r1_assessment_training" data-rowtype="2"> Below is the code I've attempted. Not entirely sure if it is the correct approach ...

AngularJS Currency Converter - Converting Currencies with Ease

I have a question regarding the most efficient way to handle currency conversion on a webpage. Currently, I have multiple input fields displaying different currencies. When a user clicks on the currency conversion button, a modal popup appears. After the ...

Is it possible to create a dynamic <hr /> using Flexbox?

In the midst of working on my application, I managed to achieve a simple layout. However, there is one peculiar requirement that has been eluding me for the past 2 days and now I find myself in need of assistance. Here is the current HTML code I am workin ...

vue utilize filtering to search through a nested array of objects within a parent array of objects

After making an API call, I receive JSON-formatted data with a specific structure like this: data = [ { name: 'John', school:[ { school_name: 'Harvard', date_attended: '2017-05-23' }, { schoo ...

Updating another component when an input value changes in React

I am currently learning React and I am facing a challenge in updating a component based on an input value. Previously, I had successfully done this using HTML and vanilla JavaScript. Now, I am trying to achieve the same functionality in React but encounter ...

Error in setting cookies using Javascript document.cookie on iOS with cordova-plugin-ionic-webview

Backend-sent cookies are successfully stored, but the app itself cannot set cookies. When running the code snippet below: document.cookie = "notified=1; path=/; expires=Tue, 19 Jan 2038 03:14:07 GMT"; console.log(document.cookie); An empty strin ...

Obtain a socket object from the Vuex store

I'm currently using vue socket io to receive data from a socket. To get the data, I utilize a query like this: // ioinstance import io from 'socket.io-client' const restaurantId = localStorage.getItem('restaurant-id') const socket ...

Matching a string with a variable in JavaScript: A simple guide

Can you assist me? I am trying to have activeLayers = [bus,stops,slot1] instead of activeLayers = ["bus","stops","slot1"]. How can I achieve this? Essentially, I want to convert the string into the object it references. Unfortunately, my current approach ...

jQuery Mobile is experiencing page height inaccuracies

My web application, built with jQuery Mobile 1.2.0, is encountering an issue with page height calculations on Windows Phone. While iOS and Android display correctly, there is a gap at the bottom of the page on Windows Phone. Is there a CSS-only solution t ...

The Next.js app's API router has the ability to parse the incoming request body for post requests, however, it does not have the

In the process of developing an API using the next.js app router, I encountered an issue. Specifically, I was successful in parsing the data with const res = await request.json() when the HTTP request type was set to post. However, I am facing difficulties ...