如何在vue.js中进行过滤

时间:2017-05-15 06:30:12

标签: vue.js laravel-5.4

我是vue.js.so的新手。我想在我的项目中应用过滤器。我试图在过去2到3天内完成此操作..但我无法帮助我。该怎么做..

我有3个输入框,一个是体验,expected_ctc和profile_role,具体取决于这3个我想显示结果..

这是我的js页面:

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()
  }
 }
});

这是我在视图页面中的按钮:

<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>

任何人都可以帮助我..

提前致谢..

1 个答案:

答案 0 :(得分:1)

好的,让我们从一个较小的例子开始。让我们说你有候选人&#34;一个候选人可能看起来像

<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>

从你目前的状态我说你有laravel返回的数组中的所有候选人。我们说我们现在的模板中有您的vue应用程序

// 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));
  }
}

现在到剧本

def fill_in_the_blanks():

一般步骤:

  • 所有候选人都在数据中 - &gt;候选
  • 相关(过滤的)候选者由计算的道具候选列表
  • 表示
  • 输入与v-model绑定到 EXISTING 数据支柱定义

小提琴https://jsfiddle.net/sLLk4u2a/

(搜索仅精确且区分大小写)