how to do filters in vue.js

2019-09-14 00:36发布

问题:

i am new to vue.js.so i want to apply a filter thing in my project.i tried to do this from past 2 to 3 days..but i couldn't..can any one help me how to do this..

I have 3 input boxes one is experience,expected_ctc,and profile_role depending on these 3 i want to display the results..

Here is my js page:

const app = new Vue({
  el: '#app',
  data() {
    return {
      page: 0,
      itemsPerPage: 3,
    }
 },
 components: { Candidate },
  methods: {
  //custom method bound to page buttons
  setPage(page) {
    this.page = page-1;
    this.paginedCandidates = this.paginate()
  },
  paginate() {
    return this.filteredCandidates.slice(this.page * this.itemsPerPage, this.page * this.itemsPerPage + this.itemsPerPage)
  },
 },


 computed: {
  //compute number of pages, we always round up (ceil)
  numPages() { 
    console.log(this.filteredCandidates) 
    return Math.ceil(this.filteredCandidates.length/this.itemsPerPage); 
  },
  filteredCandidates() {
    //filter out all candidates that have experience less than 10
    const filtered = window.data.candidates.filter((candidate) => {
      if(candidate.experience === 5) {
        return false;
      }

      return true;
    })
    console.log(filtered);
    return filtered;
  },
  paginedCandidates() {
    return this.paginate()
  }
 }
});

here is my buttons from view page:

<div class="container">

<b>Experience:</b><input type="text" v-model="experience" placeholder="enter experience">

<b>Expected CTC:</b><input type="text" v-model="expected_ctc" placeholder="enter expected ctc">

<b>Profile Role:</b><input type="text" v-model="profile_role_id" placeholder="enter role">


<input type="button" name="search" value="search" >

</div>

Can anyone help me..

Thanks in advance..

回答1:

Ok let's start with a smaller example. Lets say you have "Candidates" one one candidate might look like

{ 
  name: 'John Doe',
  expected_ctc: 'A',
  experience: 'B',
  profile_role_id: 1
}

From your current state I'd say you have all candidates in an array returned by laravel. Let's say we're in your current template where you have your vue app

<div id="app">
   <!-- lets start with one filter (to keept it clean) -->
   <input type="text" v-model="experienceSearch"/>

   <ul class="result-list">
     <li v-for="result in candidatelist">{{ result.name }}</li>
   </ul>
</div>

And now to the script

// always init your v-model bound props in data
// usually you wouldn't take the candidates from the window prop. However, to keep it easy for you here I will stick to this
data: function() {
  return {
     experienceSearch: '',
     candidates: window.data.candidates
  }
},

// the list that is displayed can be defined as computed property. It will re-compute everytime your input changes
computed: {
  candidatelist: function() {
     // now define how your list is filtered
     return this.candidates.filter(function(oCandidate) {
        var matches = true;

        if (this.experienceSearch) {
           if (oCandidate.experience != this.experienceSearch) {
              matches = false;
           }
        }

        // here you can define conditions for your other matchers

        return matches;
     }.bind(this));
  }
}

General steps:

  • all candidates are in data -> candidates
  • relevant (filtered) candidates are represented by the computed prop candidatelist
  • inputs are bound with v-model to EXISTING data prop definitions

Fiddle https://jsfiddle.net/sLLk4u2a/

(Search is only exact and Case Sensitive)