Create a Vue.js dropdown menu that dynamically changes options based on the capacity of different trips

Imagine a scenario where a user has planned a trip with a specific capacity to accommodate a certain number of individuals. For instance, the trip may be designed for 1 person or up to 6 people. To streamline the process of calculating the total trip price based on the number of participants, I utilized Vue JS to create a component with a dropdown feature.

The goal is to iterate through the dropdown menu and display the available options corresponding to the trip's capacity. For example, if the trip's capacity is set at 4 people, the dropdown should only show options ranging from 1 to 4.

Below is a snippet of my Vue JS component:

<template>
    <div>
        <label><small>Select amount of people going on this trip</small></label>   
        <select class="form-control" name="people">
            <option v-for="i in trip" :value="i+1">{{ i+1 }}</option>
        </select>
    </div>
</template>

<script>
    export default{
        props: ['trip'],
        methods() {

        },
        data() {
            return {

            }
        }
    }
</script>

Upon implementation, the component displays the trip capacity, which is indicated by the number of available options in the dropdown menu:

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

For example, if the trip capacity is set at 4 people, the dropdown should dynamically adjust to include only 4 option values.

How can I effectively utilize a for loop within the option section of Vue JS to set the number of available options equal to the trip's capacity?

Answer №1

Utilize a range v-for loop.

<select class="form-control" name="people">
  <option v-for="n in trip.people" :value="n">{{n}}</option>
</select>

Answer №2

To limit the options shown to only 4 out of 6 data entries, you can follow this HTML code:

Your HTML:

<select class="form-control" name="people">
  <option v-for="people in displayOptions" :value="people">{{people}}</option>
</select>

Your Computed:

// Example data
const people=["Benjamin", "Nathan", "Bane","Natasha", "X", "Y"]
computed: {
  displayOptions(){ 
    return people.slice(0,4)
  }
}

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

Execute some jQuery code whenever a user selects an option within a group of options

Is there a way to trigger jQuery when any option within an option group is selected? Specifically, I am looking to display or hide certain inputs based on the selection of any option within one of two option groups. HTML <select> <option> ...

Using nested ternary operations in React can cause issues with accessing local variables

Note: I encountered an issue where the extra curly braces around the first ternary result did not solve my initial problem. I replaced them with parentheses. Josep's suggestion to use getTime required me to equate the dates. The Date().setHours(0, 0, ...

Difficulty Communicating Recapcha 2 with the PHP Script

I am currently testing the installation of reCaptcha 2 with server-side verification using a form with one input field. My process involves sending the reCaptcha response via Ajax to a PHP page for verification. While I am able to capture the information p ...

How to dismiss a jQueryMobile dialog without triggering a page refresh

I've encountered a question similar to this before, but there wasn't any solution provided. The issue I'm facing is that I have a form and when a user clicks on a checkbox, I want to open a popup/dialog for them to enter some data. However, ...

Increasing the nth-child Selector in Jquery

Referring to this I am looking to cycle through the nth-child selector, which involves: var i = 1; var tmp = $(this).find('td:nth-child(i+1)'); // I wonder how this can be achieved i++; I have rows with dynamically generated columns i ...

A guide on retrieving bytecode from a specific PDF using Angular

Can anyone help me with extracting the bytecode from a selected PDF file to save it in my database? I keep encountering an error stating that my byte is undefined. Could someone please review my code and identify what might be causing this issue? I attemp ...

What is the best way to incrementally add elements and automatically update them in the browser using JavaScript?

Below is a function that triggers when a button is clicked, adding 30 div elements to the document with specific attributes. var bar_counter = 0; bar_lengths = new Array(); function createBar(btnObj) { while(bar_lengths.length < 30){ var siz ...

Is there a way to arrange an array based on the initial value?

In my array, I have a mixture of strings and arrays. Each string corresponds to the array next to it. For example, the string 789 is associated with the array ['111A', '222B', '333C']. My goal is to sort based on the strings w ...

Calculate the total amount from the selected items on the list, depending on the clicked ('active') element

My main objective is to achieve the following: Before any clicks || After the user selects the desired item After conducting some research, I successfully implemented this using vue.js https://jsfiddle.net/Hanstopz/Lcnxtg51/10/ However, I encountered ...

Did not retrieve the expected value

Why does it seem like getters.getEventById(id) doesn't function the same as state.events.find(event => event.id === id) Check both console.logs at the start of the fetchEvent action. fetchEvent({ commit }, id) { var event = getters.getEve ...

Encountering a lack of data in the request body when posting in Express.js

Here are my two attached files. I am encountering an empty response from req.body. Index.html file: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQ ...

If the <option> "anyTableName" </option> is chosen, then display the column names of the selected table (PHP, MySQL)

Hey there, I'm a newbie on stackoverflow so feel free to correct me if I'm off base ;) Here's my current dilemma: I have a text.php file that contains 2 <select> elements. The first one allows me to choose a table (like "accounts", "c ...

What is causing my array of objects to constantly accumulate undefined elements?

My quick sort function implementation for the object users_total_likes is behaving unexpectedly. When compiled and run in the terminal or browser, it adds undefined values causing a TypeError: if(users[i][key] >= users[hi][key] && users[j][key] ...

Contrasting bracket notation property access with Pick utility in TypeScript

I have a layout similar to this export type CameraProps = Omit<React.HTMLProps<HTMLVideoElement>, "ref"> & { audio?: boolean; audioConstraints?: MediaStreamConstraints["audio"]; mirrored?: boolean; screenshotFormat?: "i ...

Challenge with delayed function execution in AngularJS

In my Angular application, I am encountering a problem in the following scenario. There are three crucial files: MainCtrl.js Upon loading the application, the init() function in MainCtrl.js is triggered, which then calls the flowService as shown below: ...

Automatically compute and convert currency formats using JavaScript

May I ask again? Hopefully you understand. I am looking to automatically calculate with a money format. Here is a demo: https://jsfiddle.net/xp4ky2gg/ This is my code... HTML code <table style="width:100%"> ...

What is preventing me from integrating angular-cookies into my application?

I'm struggling to resolve this issue where I can't seem to make it work. My aim is to integrate NgCookies (angular-cookies) into my application, but all I'm encountering are errors. This is what I currently have: JS files being included: ...

The Angular 2 application functions perfectly when running locally, but encounters issues when running on an ec2 instance

When trying to upload an Angular 2 application to an AWS EC2 t2.small instance, it is not working as expected, even though it runs successfully in a local server. Node version: v7.0.0 NPM version: 3.10.8 There has been an EXCEPTION: Uncaught (in prom ...

Show several fresh windows

Greetings everyone. Let me explain my current situation: I have a search page where users can select a product from a drop-down list, and upon clicking a button, a gridview is displayed showing the specifications of the selected product. What I'm a ...

What is the best way to utilize SCSS variables alongside data-attributes within Vue JS and Bootstrap Vue?

I'm currently working on implementing a Dark Theme button for my application that can change the entire theme with just one click. Although the functionality is already in place, I am exploring ways to make this process simpler and more efficient. So ...