0

I need to implement a button that would drop the filters in my application. The application is written on Vue. The filters themselves were implemented, but I do not know how to implement their reset.

<template>
  <div id="app">
    <input type="text" v-model="search">
    <select name="sort" v-model="sort">
      <option v-for="option in options" :value="option.value" :label="option.label"></option>
    </select>
    <table>...</table>
  </div>
</template>

<script>
  import goodsList from './api/data';

  export default {
    name: 'app',
    data() {
      return {
        search: '',
        sort: '',
        options: [
          { label: 'Default', value: 'none' },
          { label: 'Brand', value: 'brand' },
          {label: 'Price', value: 'price'}
        ],
        goods: goodsList,
      }
    },
    computed: {
      filteredList() {
        let filteredGoods = this.goods.filter( item => {
          return item.name.toLowerCase().includes(this.search.toLowerCase());
        });
        switch (this.sort) {
          case 'brand':
            filteredGoods.sort((a, b) => a.brand.localeCompare(b.brand));
            break;
          case 'price':
            filteredGoods.sort((a, b) => parseInt(a.price - b.price));
            break;
        }
        return filteredGoods;
      }
    },
  }
</script>
1
  • 1
    store the unfiltered data to another variable and use that as rollback value while resetting. Commented Jul 9, 2019 at 10:56

1 Answer 1

2

You will need a reset function which will assign the default selected value eg: 'none' to the v-model 'sort'. Since it is a two way binding, changing the value of 'sort' variable will eventually reset the selected option.

Function to be added:

resetOptions: function () {  
  this.sort='none';

}

Link below https://jsfiddle.net/RohanPatil/68wced20/9/

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.