6

I am using Vue and am trying to make live search. But on updating the content of search, it doesn't get updated.

Data do get update in array, when checked in dev tools. But DOM don't get updated.

template

<div class="dropdown">
    <input type="text" v-model="input" placeholder="Search" @keyup="searching" data-toggle="dropdown">
    <span class="caret"></span>
    <ul class="dropdown-menu">
      <li v-for="(data,index) in availSearchData" :key="index">
        <a href="#">{{data.name}}</a>
      </li>
    </ul>
  </div>

method

searching() {
  if (this.input) {
    let url = this.domain + "search";
    axios
      .get(url, {
        params: {
          table: this.table,
          data: this.input
        }
      })
      .then(res => {
        this.availSearchData = [];
        res.data.forEach(doc => {
          this.availSearchData.push(doc);
        });
      });
  }
}

I don't know where I am doing wrong. Please help out if possible.

3
  • 1
    can you show me your response? Commented Mar 13, 2019 at 15:41
  • In UI, I don't get anything.. In dev tools, after update wihen i give input 'salt': availSearchData:Array[1] 0:Object coid:4 name:"SALTS AND SUGAR" subcatid:"1" Commented Mar 13, 2019 at 15:53
  • just console.log(res) and show the output here Commented Mar 13, 2019 at 16:16

4 Answers 4

4

To add an item to the back of an array and get it to be reactive in Vue, below is what worked for me:

this.$set(this.items, 
          this.items.length, 
          JSON.parse(JSON.stringify(this.item))
         );

The this.$set is Vue's inbuilt array manipulation function that guarantees reactivity. The this.items is the array, this.items.length (NOTE: it is items.length NOT items.length - 1) is to push a new index to the back of the array and finally, JSON.parse(JSON.stringify(this.item)) is to clone the this.item into a new object before pushing into the array. The cloning part may not be applicable to you and I used this in variables because all the variables are declared in my data() function.

Sign up to request clarification or add additional context in comments.

Comments

0

Use a computed property in your component and use that for parsing the template like this

<li v-for="(data,index) in availSearch" :key="index">
  <a href="#">{{data.name}}</a>
</li>

and computed property will be then

availSearch() {
  return this.availSearchData;
},

so this computed property always return the array if it is updated.

Also if your response is the array that you want to use exactly, try this

searching() {
  if (this.input) {
    let url = this.domain + "search";
    axios
    .get(url, {
      params: {
        table: this.table,
        data: this.input
      }
    })
    .then(res => {
      this.availSearchData = [];
      Vue.set(this, 'availSearchData', res.data);
    });
  }
}

9 Comments

This worked amazingly, but i get this error Computed property "availSearch" was assigned to but it has no setter.
Do not set the values using "availSearch". This is just for reading the values.
I did not set using "availSearch"
Then probably please provide your code so that I can check how exactly you are using this
and when there's only one element in array. It shows nothing in UI
|
0

Possible explanations for this might be:

  • You don't declare the property in the component and thus normal reactivity doesn't work.
  • You are using index as the key in your array. This might confuse the reactivity system, so it does not necessarily know if the item
    changed. Try using the name of the item as the key instead.

Comments

0

Try calling your function from mounted hook. I think the problem is that you are trying to show data when the DOM is not rendered yet. By calling your function in mounted you get data back after DOM has been rendered.

mounted() {
    this.searching();
}

from Vue website "mounted: Called after the instance has been mounted, where el is replaced by the newly created vm.$el. If the root instance is mounted to an in-document element, vm.$el will also be in-document when mounted is called."

3 Comments

This worked, It did showed data.. but when there's only one element in array. It shows nothing
change the :key="name". If mounted worked then the problem might be using the index as key
I can't actually, it contains duplicate data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.