Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set data to empty string on vuetify text field clearable?

I have a v-data-table bound to a search prop, and a v-text-field with clearable set. When I clear the text field with the clearable icon-button, v-text-field sets the search prop to null, causing my computed prop to error out with:

Cannot read property toLowerCase() of null

How can I set the search prop back to an empty string instead of null when the clearable icon is clicked?

MyComponent.vue:

<template>
  <div>
    <v-text-field solo hide-details single-line v-model="search" clearable>
    </v-text-field>

    <v-data-table :search="search" class="mt-2" :items="myArray" hide-actions hide-headers elevation-1>
      <template v-slot:items="props">
        <td>{{props.item.myItems}}</td>
      </template>    
    </v-data-table>
  </div>
</template>

<script>
export default {
  props: ['parameter'],
  data() {
    return {
      search: ''
    }
  },
  computed: {
    myArray() {
      let myArray = []
      if(this.parameter) {
        myArray = this.parameter.filter((download) => {                
          return download.barcode.includes(this.search.toLowerCase());
        })
      }
      return myArray;
    }
  }
}
</script>
like image 817
Somethingwhatever Avatar asked Sep 09 '19 21:09

Somethingwhatever


2 Answers

Try to check the search data property as a condition inside the return statement :

  if(this.parameter) {
      myArray = this.parameter.filter((download) => {                
            return !this.search || download.barcode.includes(this.search.toLowerCase());
        })
    }
like image 62
Boussadjra Brahim Avatar answered Sep 19 '22 02:09

Boussadjra Brahim


Try using @click:clear="clearFunction()" as mentioned here.

Codepen reference: https://codepen.io/anon/pen/ePmLOg?editors=1010

like image 20
Kousika Ganesan Avatar answered Sep 21 '22 02:09

Kousika Ganesan