Vue paginated select with dynamic data loading

My API has a endpoint that provides a list of countries. The endpoint accepts the following query parameters:

searchQuery // optional search string
startFrom // index to start from
count // number of options to return

For example, a request with searchQuery as '', startFrom as 0, and count as 10 will return first 10 countries.

Another request with searchQuery as Can, startFrom as 5, and count as 3 will return the fifth to eighth country containing the string Can.

I am looking to modify the pagination example from vue-select to utilize this REST API for fetching countries dynamically instead of using a static list as shown in the demo.

The Vue-select documentation also offers an ajax based example.

As a beginner in Vue, I'm struggling to integrate both methods as I desire.

Could a knowledgeable Vue expert provide guidance on how to achieve this?

Currently, my paginated example uses countries defined as a static array:

countries: ['Afghanistan', 'Albania', 'Algeria', ...]

Template:

<v-select :options="paginated" @search="query => search = query" :filterable="false">
    <li slot="list-footer" class="pagination">
      <button @click="offset -= 10" :disabled="!hasPrevPage">Prev</button>
      <button @click="offset += 10" :disabled="!hasNextPage">Next</button>
    </li>
</v-select>

Data:

data() {
    return {
        countries: // static array mentioned above
        search: '',
        offset: 0,
        limit: 10,
    }
}

Computed:

filtered() {
  return this.countries.filter(country => country.includes(this.search))
},
paginated() {
  return this.filtered.slice(this.offset, this.limit + this.offset)
},
hasNextPage() {
  const nextOffset = this.offset + 10
  return Boolean(this.filtered.slice(nextOffset, this.limit + nextOffset).length)
},
hasPrevPage() {
  const prevOffset = this.offset - 10
  return Boolean(this.filtered.slice(prevOffset, this.limit + prevOffset).length)
},

What steps should I take to fetch countries from my REST API instead?

Answer №1

The outcome will vary based on the specific tasks being carried out within the fetchOptions function. Essentially, you must ensure that pagination data is passed to the function and utilized as query parameters in an AJAX request. Feel free to share additional code so I can offer guidance on how to address this issue effectively.

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

The Carousel feature functions properly with 3 or more slides, but malfunctions when there are only 2 slides present

After implementing a minimal carousel, I discovered that it functions perfectly with 3 or more slides. However, as soon as I remove some slides, issues start to arise. Some of the problems I encountered include: The sliding animation is removed on ' ...

How to establish a session in CodeIgniter with the help of JavaScript

Currently, I am trying to save the session in jQuery. Specifically, I am attempting to store all the IDs that are checked via checkboxes in the export_type variable and then save them in the session. However, when running the following code snippet, I en ...

Customizing your buttons in Wordpress with extra CSS styles upon clicking

I am in the process of developing a Wordpress website and I require a button for switching between mobile and desktop versions manually. Within my .css file, I have included: @media (pointer:coarse) {} This adds modifications for the mobile version ...

Leverage ng2-charts along with a loading component during the AfterViewInit lifecycle hook

Currently, I am working on a web page that contains various charts. My focus right now is on developing a simple loader as shown below: <div *ngIf="loading === true; else elseBlock" class="container"> <div class="grid-pulse la-3x"> </di ...

Revamping the hyperlinks in my collapsible menu extension

Is there a way to modify the links in this accordion drop menu so that they lead to external HTML pages instead of specific areas on the same page? I've tried various methods but can't seem to achieve it without affecting the styles. Currently, i ...

Guide on integrating Troisjs into a Nuxt 3 project

I am interested in incorporating TroisJS (a wrapper for three.js designed for Vue) into my project using Nuxt.js. The TroisJS documentation at suggests adding it to the project like this: import { TroisJSVuePlugin } from 'troisjs'; app.use(Trois ...

As I attempt to connect with the bitcoin average server, I encounter a 403 status code error in the communication

const express = require("express"); const bodyParser = require("body-parser"); const request = require("request"); const app = express(); app.use(bodyParser.urlencoded({extended: true})); app.get("/", function(req, res){ res.sendFile(__dirname + "/inde ...

Using ReactJS to display a collection of images

Currently diving into ReactJS and experimenting with the Spotify API, everything is running smoothly except for a hurdle I encountered while attempting to display an array of images retrieved from the API. I initially tried to render the images inside the ...

Navigating on Vue with server-side rendering and Laravel

Currently, I am delving into Vue and facing a challenge setting it up as the complete front-end with Laravel. In my current setup, I have created a personal blog for testing purposes using Laravel with Blade engine and incorporating some Vue components whi ...

Having trouble interpreting the Json response from a jQuery Ajax POST request

When I receive a JSON from a REST API via Ajax, if I print the result to the console and then save it into a string variable (jsonInput) `JSON.parse` works without any issues. However, when I directly use the data from the Ajax call, it returns as undefi ...

Guidance on incorporating CSS into ES6 template literals within a react framework

I am trying to implement the Material UI stepper in my React application. The step content is set up as a string literal. My goal is to include CSS styling for paragraphs within the step content. Many online resources suggest using \n to add a line ...

Unable to dismiss message in Django

I'm a beginner in Django and I recently followed a tutorial to add message alerts to my code. The alerts are displaying correctly, but unfortunately, I am having trouble closing them using the 'x' button. https://i.stack.imgur.com/BQS1S.png ...

Issue with OnChange Not Triggering

Why isn't the onchange property firing when typing in my textbox? Check out the code below: VB.NET Dim textBoxUrlYoutube As New TextBox divUrlTextBoxContainer.Controls.Add(textBoxUrlYoutube) textBoxUrlYoutube.CssClass = "textboxyo ...

Finding the Modular Reciprocal with JavaScript

I am attempting to find the value of d by solving the equation ed ≡ 1 mod((p-1)(q-1)), similar to the RSA algorithm. Given e = 5 and (p-1)*(q-1) = 249996 I have experimented with various Javascript code snippets, such as: function calculateModInverse( ...

communication flow in two directions between server and client

Looking for recommendations on a javascript/nodejs solution that enables bi-directional communication between server and client. The application flow involves the client sending a discovery service to the server, which responds with a challenge. The client ...

Update the data table after a new file has been uploaded

As a novice web developer, I embarked on my first project using Vue. In this project, I created a form for file uploads in Vue 2 and Laravel. If you want to take a look at the full code: View: https://pastebin.com/QFrBfF74 Data table user file: https:/ ...

"What is the best way to retrieve the value of a div element with jquery

Attempting to retrieve the value of a div using jQuery in Chrome console from this HTML: <div class="col-md-3"> <div class="vou-col" style="cursor: pointer"> <h4>Free Large Bucket</h4> <span class="sku-info">Voucher ...

Mastering Angular2: Leveraging TypeScript's Power to Unleash JavaScript ES6 Syntax with

I am having trouble implementing a translation feature using the ng2-translate pipe in my Angular2/Ionic2 app, which is entirely written in JavaScript ES6. However, I have encountered an issue during the setup phase. The code snippets I have found on the ...

Vertically animating an image using jQuery

I have been experimenting with trying to make an image appear as if it is floating by using jQuery to animate it vertically. After some research, I stumbled upon this thread: Animating a div up and down repeatedly which seems to have the solution I need. H ...

What is the method to provide function parameters without executing the function?

I'm searching for a solution to obtain a function that requires a parameter without actually invoking the function. Example of current malfunctioning code: const _validations = { userID: (req) => check('userID').isString().isUUID(&apo ...