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

Refresh the page with cleared cache using window.location.reload

Is there a way to reload a page using JavaScript and still clear the cache, ensuring that the refreshed page shows the latest content from the server? It seems that other browsers are not displaying the most up-to-date versions of the content. Anyone have ...

Encountering an unrecoverable SyntaxError while trying to deploy a website on Netlify

When using commands like npm start, npm run build, and pm2 start server.js, everything runs smoothly without any errors. However, I encounter an issue when trying to deploy my project on Netlify. The Chrome console displays the error: Uncaught SyntaxError: ...

Utilize an Ajax function to call the BOT listening URL within a Web method

My recently developed web application features a submit button that triggers an Ajax call to communicate with the BOT. The code snippet used for the Ajax function is as follows: <script> $(document).ready(function () { $("#btnSend& ...

The occurrence of an unhandled promise rejection is triggered by the return statement within the fs

In my code, I have a simple fs.readFile function that reads data from a JSON file, retrieves one of its properties (an array), and then checks if that array contains every single element from an array generated by the user. Here is the snippet of code: co ...

Leveraging SASS with Vuetify styles

I am trying to incorporate some Vuetify classes into my own custom sass file, but I am encountering difficulties in doing so. I am working on a Vue-CLI3 project with the latest versions of Vue/Vuetify and have the following code snippet: main.sass @import ...

The initialization process of Vue.js router is compatible with router.map, but not with the Router constructor

I'm experiencing an issue in my app where routes work fine when I use router.map({}) with the vue-router, but they fail to work when I pass them directly in the constructor. Any insight into why this might be happening? // Routes that work: const rou ...

How to position an absolute element beneath a fixed element

My website is experiencing a problem where the fixed header is overlapping an absolute paragraph on this page. Does anyone know how to resolve this issue? ...

Error Encountered: Yii2 Mishap with Ajax Calls

Retrieve the URL creation code $ajaxSaveTestGrid = Yii::$app->urlManager->createUrl('testgrid/ajaxsave'); Here is my AJAX view code function saveRow(id, index) { if(id == 'tbl_testGrid') { save(i ...

Angular: Enhancing URL links

Recently, I discovered a function in my code that allows me to cycle through different pictures and change the URL accordingly. The initial URL is obtained using angular routes, where the "domain" parameter consists of the domain.id and the domain.category ...

Tips for avoiding duplicate Id tags while using Ajax in MVC

Hello everyone, I am curious to learn about the precautions and techniques you employ when working with Ajax or MVC functions such as @Html.Action in order to effectively manage and prevent duplicate Ids for Html tags. In addition, how does the page handl ...

Generate barcodes using Angular 2

Can anyone point me in the direction of an Angular 2 Barcode Generator capable of creating code 128 barcodes? I've been searching but haven't had any luck finding one. If you have any suggestions or can offer assistance, it would be greatly appr ...

"The issue seems to stem from a snippet of compact JavaScript

I am facing an issue with a list item that contains both an image and text. The text is overlapping the image, but when I hover over the text, it disappears and only the picture is visible. Here is the HTML code snippet: <ul> <li><img ...

What is the best approach for managing multiple socket rooms?

I have been working on developing a chat application that supports multiple chat rooms. After watching several tutorials, I have devised a way to handle this. const io = require("socket.io")(http); io.on("connection", (socket) => { ...

Using MUI Select with Array of Objects for values - Learn how to deselect a predefined state

I have a list of objects structured like this: [ { _id: "6311c197ec3dc8c083d6b632", name: "Safety" }, ........ ]; These objects are loaded as Menu Items options for my Select component: {categoryData && c ...

Creating valuable properties in TypeScript is a skill that requires knowledge and practice

In TypeScript, there is a unique feature available for defining properties with values using the `value` keyword. class Test { constructor(private value: number = 123) { } public MyValueProperty: number = 5; } Here is how you can define such ...

Is there a way to automatically close one menu when another is opened by clicking?

When all other search results have failed to solve my issue, I resort to posting my own question. My problem involves opening hidden submenus in a website's main menu. However, when I open multiple submenus, they just stack above each other. Ideally, ...

Updating the content with HTML and JavaScript

Hello everyone, I am currently working on a project to change the content of a div using JavaScript for educational purposes. Here is what I have done so far - <div id="navbar"> ... <ul> <li> <text onclick="getWordProcessing() ...

Increase the value of $index within the ng-repeat loop

Is there a way to increment the value of $index in ng-repeat by a specific amount? For example, if I want to display two values at a time, how can I ensure that the next iteration starts with the third value instead of the second value? <div ng-contr ...

Extract reference value from an HTML element

Is there a way to access the ref prop from an HTML element using Testing Library React? My current code snippet is as follows: it('element container ref should be null if prop noSwipe is passed', () => { const onCloseMock = jest.fn() ...

Prevent WordPress themes from running in order to integrate a personalized Vue.js front-end application

I'm currently working on a basic website that will utilize vuejs as the front-end framework and wordpress for generating dynamic content. To achieve this, I've placed wordpress inside the public directory of my vue project in a folder named app. ...