Having trouble displaying options in VueJS Component using datalist

Currently, I am harnessing the power of VueJS and its components to create an extensive array of datalists and selectors, each equipped with a submit button for validation upon form submission.

Initially, I successfully implemented a datalist within a component that rendered options and provided type completion - it worked flawlessly! However, when I attempted to convert this functionality into a VueJS Component and passed the dataarray as a property, my list of options ceased to render.

Comparing Two Datalist elements...

https://i.stack.imgur.com/gHsk2.png

The upper image represents the "raw" datalist which functions perfectly

https://i.stack.imgur.com/MSeO0.png

However, in the Vue.js component version, no options are displayed...

https://i.stack.imgur.com/SXEfr.png

The options are simply not visible when compared to the first image...

https://i.stack.imgur.com/va0DA.png

The VueJS Component for Datalist

<template>
    <div>
      <input type="text" v-model="item" list="data_input" v-on:input="selectionChanged">
        <datalist id="yourdatalist">
          <option v-for="item in data_input">{{item}}</option>
        </datalist>
    </div>
</template>

<script>
export default {
  name: 'Datalist',
  props: ['inputDataList'],
  data () {
    return {
      selection: '',
      item:'',
      data_input:this.inputDataList
    }
  },

methods: {
  selectionChanged: function(element) {
    console.log("selection = "+this.selection+", new value = " + element.target.value);
    var newSelection = element.target.value;

    if (newSelection != this.selection) { 
      // newSelection changes on every keystroke, so you must keep diffing it with your known data
      for (var i=0; i<this.data_input.length; i++) {
        if (this.data_input[i] == newSelection) {
          this.selection = newSelection
          console.log("selection = "+this.selection+" now");
          this.$emit('selectionChanged', this.selection);
        }
      }
    }
  },
},
}
</script>

The HTML code for the calling component

<p>Examples of Datalists</p>
<input type="text" v-model="film" list="films" v-on:input="filmChanged">
    <datalist id="films">
        <option v-for="film in films">{{film}}</option>
    </datalist>
<div v-if="focusedfilm">
  <h6>You have picked {{focusedfilm}}</h6>
</div>
<br/>
<p>Examples of Child Component Datalist</p>
<Datalist :inputDataList="films"/>

Answer №1

To assign the value of the 'id' attribute of the datalist to the 'list' attribute.

Modify

<datalist id="yourdatalist">
to <datalist id="data_input">

Sincerely

Answer №2

It is recommended to accept Alfredo Lanzetta's answer if he posts it first, as I am here to provide an explanation of why the solution works.

If you are looking to create a dropdown list for an input field in your code, consider the following:

<input type="text" v-model="item" list="data_input" v-on:input="selectionChanged">
<datalist id="yourdatalist">
  <option v-for="item in data_input">{{item}}</option>
</datalist>

To ensure that the datalist is correctly assigned to the input field, you need to link them using the input field's list property.

The key is to set the list property of the input field to match the id of the datalist. In this example, the datalist has the id

yourdatalist</code, while the input field's <code>list
property is set to data_input. This mismatch prevents the dropdown list from appearing because the input field is searching for a datalist with the id data_input.

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

Having trouble entering text into a textbox in Reactjs when the state is empty

I am currently working on integrating a newsletter submit form using Reactjs/nextjs. However, I am facing an issue where once I submit the form, I am unable to type anything in the textbox. I have tried making the "state" empty but it did not resolve the ...

Learn how to trigger a re-render in React to update a value stored in local storage

I need assistance in displaying a spinner with a percentage value during file uploads within my React application. To achieve this, I am utilizing Material UI's circular progress. My approach involves making a REST call using Axios to obtain the perce ...

Transforming Arrays of Objects into a Single Object (Using ES6 Syntax)

My attempt to create aesthetically pleasing Objects of arrays resulted in something unexpected. { "label": [ "Instagram" ], "value": [ "@username" ] } How can I transform it into the d ...

Exporting methods/functions in React Native

import { width as responsiveHeight } from "react-native-responsive-dimensions"; I am trying to export responsiveHeight with the name width. Can someone please guide me on the correct way to do this? The current approach is not yielding any results. ...

Troubleshooting: My jQuery script for fading in content upon document ready is not

I'm currently working on a website where I want everything to fade in when the user enters the site. Take a look at my code: var celyLogin = $(".container"); $(document).ready(function () { /*! Fades in page on load */ $("container") ...

What's preventing me from utilizing Leaflet Map on Next.js despite attempting Dynamic Import?

When using Leaflet, it requires the global window object which is not available on SSR (Server-Side Rendering) and can cause an error stating "window is not defined". I have tried extensively to find a solution, and the only method I found was to use dyna ...

Transforming .POST into corresponding .AJAX techniques

I'm currently attempting to convert a .post javascript call into an equivalent .ajax call in order to make it asynchronous. However, I'm running into some issues and can't seem to get it to work. The original working .post call looks like th ...

Having trouble sending a request in next.js with Docker during the build process?

When utilizing the getStaticProps function to send a request to my backend API from another Docker container, I am encountering an issue. Despite ensuring that the API URL is accurate, the static page fails to be created. This is due to the requirement for ...

Is it possible to disable a function by clicking on it?

I currently have a website that is utilizing the following plugin: This plugin enables an image to be zoomed in and out through CSS transforms, with default controls for zooming in and out. My goal is to incorporate a reset button that returns the image ...

Tips for adjusting the vue-intro.js styles in a nuxt.js project

I need help changing the appearance of the tooltip button in my web app using vue-intro. I am struggling to update its color and themes within vue-intro.js. https://i.stack.imgur.com/yP7qI.png Specifically, I am looking to modify the Next button color. ...

Pass information to CGI script and return using jQuery.ajax

Currently, I am utilizing jQuery.ajax() to transmit HTML form data from my frontend to a Perl script on the server and then receive some information back. The preferred format for this information is text or string. Additionally, I need to store it as a v ...

How to reach the Twitter share iframe with JavaScript/HTML

There seems to be an issue with the Twitter share button not displaying at its full width on certain pages. Upon investigation, I discovered that the iframe containing it is only set to a width of 24px, when it should actually be set to the correct width. ...

What is causing the CSS transition to fail in the updated DOM?

When attempting to apply a CSS transition as shown in the following code snippet: https://jsfiddle.net/sunyuu/e58sfeva/17/ var Main = { data () { return { isActive: false, childStyle: {} }; }, methods: { sho ...

Updating entire DOM in javascript

Implementing undo-redo functionality in my project has proven to be quite complex, as every change affects numerous elements. I believe that saving and restoring the entire page may be the most effective solution. However, I have encountered issues with mi ...

Custom CSS for the Google Maps Circle Object

Currently, I am utilizing the Google Maps Javascript API v3 Circle object to display circles on the map. I am interested in customizing the CSS of this circle by incorporating some CSS animations. Although I am aware that custom overlays can be used for t ...

Accessing store state in axios plugin with Nuxt.js

I've encountered a problem where I have a token stored, but I'm struggling to access it in my axios plugin while using Nuxt.js. In the past with just Vue, it was simple to import the store and access the token. However, I'm having difficulty ...

Is it possible for Angular to perform bidirectional data binding in reverse between two input fields?

I'm struggling to get my two input fields to update values when the opposite input is changed. My goal is to create a simple $dollar to Gold oz calculator with two input fields. You can see a sample preview here: http://embed.plnkr.co/dw6xL95zRqJC1p ...

I can't seem to retrieve any values from the API other than "chicken"

Is there a way to make the search bar in my recipe app look for recipes that I provide, rather than fetching data from useState? Any suggestions on how I can achieve this? import React, { useEffect, useState } from 'react'; import Recipe from &ap ...

Retrieving particular excerpts from a block of text that includes the keyword "javascript."

I have a chunk of string containing HTML source code. Currently, I am trying to read specific tags that I need using JavaScript. Since I am new to programming, I'm unsure how to proceed. Can anyone assist me with this? The issue lies in the following ...

Replacing npm package dependency

I came across this post: How can I change nested NPM dependency versions? Unfortunately, the solution provided did not work for me. I am attempting to modify a package to utilize a different version of a specific dependency instead of the one it currentl ...