Solution 1 :

The <datalist> control doesn’t have it’s own events, you have to use the <input>. Both clicks and keyboard events will trigger the input’s keydown listener.

<input
  list="data"
  v-model.trim="searchBy"
  @keydown="getData"
  @keyup.enter="getAllDatas"
  type="text"
  placeholder="Find more data..."
/>

This will pass the event to the handler, so unless you can work around that, you might want to use a <select> instead, or a custom control.

Problem :

I have a problem with datalist with my input search in VueJS, why when i click with mouse or with enter on one of the option on my datalist, my function getData() which is called to @click and @keypress.enter is not launched?

here is my code:

template:

          <input
            list="data"
            v-model.trim="searchBy"
            @keyup.enter="getAllDatas"
            type="text"
            placeholder="Find more data..."
          />
          <br />
          <datalist id="data">
              <option v-for="r in repos.items" 
                      :key="r.id" 
                      :value="r.name"
                      @click="getData(r.id)"
                      @keypress.enter="getData(r.id)"  />
          </datalist>

script:

 methods: {
    getAllDatas() {
      fetch(`someURL`)
        .then(res => res.json())
        .then(
          res => {
            this.data = res
          }
        )
    },
    ...mapActions(["fetchData"]),
    async getData() {
       await this.fetchData();
  },

i totally have no idea what is wrong here, can someone tell me?

Comments

Comment posted by Amaarockz

I don’t see the getData() in your methods section

Comment posted by svelteDev

I cant do that because i need to provide an attribute to my

Comment posted by Dan

In that case, you can’t use a datalist. Your choice is a

Comment posted by svelteDev

Can you show example witch

Comment posted by Dan

Well, it’s basically what you have here but without the input. So you wouldn’t get the typing ability with it. If you need that, then you have to use a custom control. Is that what you want?

Comment posted by svelteDev

Thanks @Dan, it helps!

By