What is the best way to show the chosen option from a dropdown list in Vuejs?

thicknessexpanded: false,
method(){
    thicknessOnClick: function (event) {
      this.thicknessexpanded = !this.thicknessexpanded;
    },
    }
.dropdown-check-list3 {
  padding-left: 4px;
  padding-bottom: 25px;
  display: inline-block;
}
.dropdown-check-list3 .anchor3 {
  margin-left: 20px;
  width: 255px;
  color: #70737a;
  font-size: 14px;
  font-weight: 500;
  background-color: #ffff;
  position: relative;
  cursor: pointer;
  display: inline-block;
  padding: 5px 140px 5px 10px;
  border: 2px solid #ccc;
}
.dropdown-check-list3 .anchor3:after {
  position: absolute;
  content: "";
  border-left: 2px solid black;
  border-top: 2px solid black;
  padding: 5px;
  right: 10px;
  top: 35%;
  transform: rotate(-135deg);
}
.dropdown-check-list3 .anchor3:active:after {
  right: 8px;
  top: 21%;
}
.dropdown-check-list3 ul.items3 {
  width: 254px;
  background-color: #ffff;
  padding: 2px;
  display: none;
  margin: 0;
  border: 1px solid #ccc;
  border-top: none;
}
.dropdown-check-list3 ul.items3 li {
  background-color: #ffff;
  list-style: none;
}
.dropdown-check-list3.visible .anchor3 {
  background-color: #ffff;
}
.dropdown-check-list3.visible > span.anchor3:after {
  transform: rotate(45deg);
}
 <div
              class="dropdown-check-list3"
              :class="{ visible: thicknessexpanded }"
              tabindex="100"
            >
              <span class="anchor3" @click="thicknessOnClick">Select (mm)</span>
              <ul class="items3">
                <div class="checkbox-alignment-application-filter">
                  <li
                    id="thickness-filter1"
                    class="vh-product"
                  />
                  <label class="productlist-specific" for="thickness-filter1"
                    >2.0mm</label
                  >
                </div>
                <div class="checkbox-alignment-application-filter">
                  <li
                    id="thickness-filter2"
                    class="vh-product"
                  />
                  <label class="productlist-specific" for="thickness-filter2"
                    >22.0mm</label
                  >
                </div>
                <div class="checkbox-alignment-application-filter">
                  <li
                    id="thickness-filter3"
                    class="vh-product"
                  />
                  <label class="productlist-specific" for="thickness-filter3"
                    >3.0mm</label
                  >
                </div>
                <div class="checkbox-alignment-application-filter">
                  <li
                    id="thickness-filter4"
                    class="vh-product"
                  />
                  <label class="productlist-specific" for="thickness-filter4"
                    >4.0mm</label
                  >
                </div>
              </ul>
            </div>

To make a selection from the dropdown menu and display the chosen value, I have implemented some functionality in the code snippet provided above. However, I am facing issues when trying to bind the selected value in the dropdown. Can someone offer guidance on how to achieve this properly?

If anyone could point out any mistakes in the code or suggest a starting point on displaying the selected value accordingly, it would be greatly appreciated.

Answer №1

When using vue.js, consider implementing a dropdown component where you pass in the items to select and use the v-model directive to bind the selected item in your vue data as well as the dropdown itself.

This approach is commonly used in many dropdown components within vue.

You can implement it like this:

Form Component

<template>
<CustomDropDown :items="itemsList" v-model="chosenSize" />
</template>

<script>
export default {
   data() {
      return {
         itemsList = ["2mm", ...],
         chosenSize = itemsList[0]
      }
   }
<script />

Dropdown Component

<template>
 ... dropdown code 
 <li @click="updateValue(item)" v-for="item, index in items" :key="index">item</li>
 ...
</template>

<script>
export default {
props: ["value", "items"],
data() {
  return {
    chosen: this.value
  }
},
methods: {
   updatedValue(item) {
      this.chosen = item;
      this.$emit("input", item);
   }
}
</script>

This approach allows for customization of the dropdown while keeping track of the selected item and having a v-model in the parent form component at all times.

I recommend checking out [vue js components][1] for more information.

Hope this information proves helpful! [1]: https://v2.vuejs.org/v2/guide/components.html

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

Exploring Ways to Retrieve Property Names in AngularJS JSON

I need help finding a way to use the property name as the table header in my code example below: <table> <th ng-repeat="auditorium in auditoriums"> {{ auditorium.NAME }} </th> <tbody ...

Best practice for validating a form using React: Why the state doesn't update immediately with useState and onSubmit

I'm currently working on implementing a custom form validation for my React project using Typescript. However, I've encountered an issue with the useState hook not updating the state containing errors immediately upon form submission. Let me illu ...

The effects of external CSS are not being displayed or are being overridden

I am currently working on a webpage design that resembles the style of Microsoft's website. I came across a tutorial on W3 Schools for creating an image slideshow, which can be found here: https://www.w3schools.com/howto/howto_js_slideshow.asp. The ex ...

Unable to render dynamic ID in Next.js version 13.4.6 due to an issue

https://i.sstatic.net/x8oF1.pnghttps://i.sstatic.net/OEIL5.png Currently diving into next-js! Previously, I utilized dynamic id rendering in my projects. However, encountering errors now with the current version (next js 13.4.6). Having trouble identifyin ...

How can you display an escaped string using lit element?

Check out this demonstration that first showcases HTML content within the fs-highlight element and stores the result in the _code property. The render function then displays the template with the highlighted HTML from _code inserted into the code element ...

Display Vue component using a string input

Is there a solution to make this non-functioning example work, or is its usage illegal? Vue.component('hello', { template: '<span>Hello world!</span>' }) Vue.component('foo', { data(){ return { ...

What are the steps to create a shadow effect on a bar chart in ChartJs?

Currently, I am utilizing version 3.9 of chartjs to construct a bar chart. My goal is to incorporate a shadow effect on the bars resembling the image in this link: https://i.sstatic.net/SGIrO.png Despite my efforts, I have not been able to locate instruc ...

Scrolling with a Dynamic Twist

Check out this link to see what I'm talking about. I'm struggling to make the background image behave the way I want it to. I need the background to automatically resize based on the width of the window, which it currently does correctly. When ...

Eliminate any unnecessary tags located before the text

I am facing a challenge with the following code snippet. The Variable contains a string that includes HTML tags such as <img>, <a>, or <br>. My goal is to eliminate the <img> tag, <a> tag, or <br> tag if they appear befo ...

Front end development in Laravel 6.2 not coming together as expected

I've recently started using Laravel 6.2 and Vuetify, but I'm encountering an issue with Vuetify not working properly. The design for Vue.js does not appear as expected and I can't figure out what went wrong. Can someone please assist me with ...

Steps for aligning an image and text within an icon next to each other

I'm looking to align a small PNG image next to some text within an icon. How can I achieve this? Currently, they are stacked vertically. Here is the current layout - I want the two elements side by side instead. The structure of the division is unique ...

Express.js session management -- Ensuring sessions are created exclusively for authenticated users

I've implemented the express-session Node module to handle secure session cookies. // To ensure certain requests require an active session, this code sets up a secure session cookie app.use(expressSession({ secret: "wouldn'tyouliketoknow", ...

showing the angular response data in a HTML table layout within a specific div element

I'm currently using the angular-fullstack generator and I've received data in response to a GET request in the form of integer values (1, 2 5, 6 134, 245 236, 567 415, 234 and so on). I'd like to display these values in an HTML t ...

Attempting to transform a brief JavaScript script into Java code

There's a code snippet in JavaScript that I'm not familiar with, but I need to pass this information to one of my Java methods. example.setChoices([{ value : 'One', label : 'Label One', disabled : true }], 'value', ...

Is it achievable to create a seamless fade in/out effect using jQuery?

I am experiencing a problem with smooth fading of image backgrounds on my website. Interestingly, this issue only occurs in Safari and Opera. In Firefox, Chrome, and IE7/8, you can see "frames" of the fade transition, almost like taking pictures. I am cu ...

The script functions smoothly on XAMPP, however, it encounters issues when deployed on

I've encountered an issue with a script that is designed to display posts and images from a user. While it works perfectly fine on Xampp, I'm facing an issue on the host server where only the posts are visible but not the images that the user has ...

Error Alert: UnhandledPromiseRejectionWarning in Async Series Causes Concern

I am currently working on a function in my project that uses the async series method to ensure specific tasks are executed in a certain order. In this case, function1 needs to be completed before function2, which must then be completed before function3. a ...

How can timestamps be set in MongoDB during an insertOne() operation without relying on new Date() as in PostgreSQL's NOW()?

timestamp: { $currentDate: { $type: "timestamp" } } }); The insert() method is not functioning properly Is there anyone who can assist me with this issue? ...

Guide to embedding a qr code within a pdf document

I've been working on creating a PDF file generation feature where users can download the PDF with a QR code embedded in it. While I've successfully implemented the PDF generation part, I'm facing an issue with adding the QR code to the file. ...

Removing an item from a numeric array in mongoose: Best practices

I received a document structured like this: { "_id" : "5339be1d9a703ab8708b45675339bed39aac7", "description" : "data", "name" : "data", "members" : [ { "user" : { "$ref" : "users", "$id" ...