2

I have a table in vue template, The search is not working, Don't know what i missed.

<input type="text" class="dv-header-input" v-model="query.search_input"
  @keyup.enter="fetchRecords()">

And the table :

<tr v-for="row in filteredRow">
    <td v-for="(value, key) in row">{{value}}</td>
</tr>

And the JS :

  export default {
    props: [
      'source', 
      'title',
    ],
    data() {
      return {
        model: { data: [] },
        columns: {},
        query: {
          search_input: ''
        },
      }
    },
    created() {
      this.fetchRecords()
    },
    methods: {
      fetchRecords() {
        var vm = this
        // Not original API, 
        axios.get(/get/Details)
          .then(function(response) {
            Vue.set(vm.$data, 'model', response.data.model)
            Vue.set(vm.$data, 'columns', response.data.columns)
          })
          .catch(function(response) {
            console.log(response)
          })
      }
    },
    computed: {
      filteredRow: function(){
        return this.model.data.filter((row) => {
        for(var key in row){
            return String(row[key]).indexOf(this.query.search_input);
          }
        });
      }
    }
  }

In filteredRow, I console.log(String(row[key]).indexOf(this.query.search_input))' which gives me 30 times 0.

What am i missing and whats the best approach.

2
  • 1
    that for loop is never going to get past the first key in row Commented Jan 12, 2018 at 13:09
  • @jeff (Finally someone), Then what should be done ? Commented Jan 12, 2018 at 13:10

1 Answer 1

3

That for loop is never going to get past the first key in row. You want to return true only if you find the string:

  filteredRow: function(){
    return this.model.data.filter((row) => {
      for(var key in row){
        if(String(row[key]).indexOf(this.query.search_input) !== -1){
          return true;
        }
      }
      return false;
    });
  }
Sign up to request clarification or add additional context in comments.

3 Comments

@Gammer no problem. indexOf returns -1 when it doesn't find the string, you can't use it as a boolean because it returns 0 if the string is found at the first character
Make sense, Perfect. filteredRow will work for conditional based filter ?, Means select a table column from drop down then select an operator from another dropdown like = and then type the number you want to search against that column ?
You would have to write some new code for that since this one is only a string filter

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.