如何在Vuejs中使用多个条件筛选数组?

3

const app = new Vue({
  el: '#app',
  data: {
    search: '',
    itemsList: [],
    isLoaded: false,
    selectNum: status,
    userList: [{
        id: 1,
        name: "Prem",
        status: "ok"
      },
      {
        id: 2,
        name: "Chandu",
        status: "notok"
      },
      {
        id: 3,
        name: "Shravya",
        status: "ok"
      },
      {
        id: 4,
        name: "kirt",
        status: "notok"
      }
    ]
  },
  created() {
    vm.itemsList = [];
    vm.isLoaded = true;
  },
  computed: {
    filteredAndSorted() {
      // function to compare names  
      function compare(a, b) {
        if (a.name < b.name) return -1;
        if (a.name > b.name) return 1;
        return 0;
      }

      return this.userList.filter(user => {
        return user.name.toLowerCase().includes(this.search.toLowerCase()) &&
          user.status === this.selectNum
      }).sort(compare)
    }
  }
})
<div id="app">
  <div v-if="isLoaded">
    <select v-model="selectNum" name="text">
      <option :value="status">ok</option>
      <option :value="status">notok</option>
    </select>
  </div>
  <div class="search-wrapper">
    <input type="text" v-model="search" placeholder="Search title.." />
    <label>Search Users:</label>
  </div>
  <ul>
    <li v-for="user in filteredAndSorted">{{user.name}}</li>
  </ul>
</div>

我想实现的功能是,

初始情况下,数组将通过v-for加载。 在顶部我有两个选项:搜索栏和下拉框。通过它们的帮助,我试图过滤数组。

使用搜索栏,我想要过滤数组的值。 使用下拉框,我想要过滤每个数组中存在的状态。

2个回答

2
尝试使用以下代码片段:

const app = new Vue ({
  el: '#app',
  data() {
    return {
      search: '',
      itemsList: [],
      isLoaded: false,
      selectNum: '',
      userList: [
        {
          id: 1,
          name: "Prem",
          status:"ok"
        },
        {
          id: 2,
          name: "Chandu",
          status:"notok"
        },
        {
          id: 3,
          name: "Shravya",
          status:"ok"
        },
        {
          id: 4,
          name: "kirt",
          status:"notok"
        }
      ]
    }
  },
  created(){
    this.isLoaded = true;
  },
  computed: {
    filteredAndSorted(){
     function compare(a, b) {
       if (a.name < b.name) return -1;
       if (a.name > b.name) return 1;
       return 0;
     }
     const res = this.userList.filter(user => {
          return user.name.toLowerCase().includes(this.search.toLowerCase())
       }).sort(compare)
     if (this.selectNum) {
       return res.filter(user => user.status === this.selectNum )
     }
     return res
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div v-if="isLoaded">
    <select v-model="selectNum" name="text"> 
      <option value="" selected="selected">Select status</option>
      <option value="ok">ok</option>
      <option value="notok">notok</option>
    </select>
  </div>
  <div class="search-wrapper">  
    <input type="text" v-model="search" placeholder="Search title.."/>
        <label>Search Users:</label>
  </div>  
  <ul>
    <li v-for="user in filteredAndSorted">{{user.name}}</li>
  </ul>
</div>


1
代码在第25行出现了错误。
// ...
  created(){
    var vm = this;
    vm.itemsList = [];  // => j is undefined
    vm.isLoaded = true;
  },
// ...

是的,搜索栏运作良好。但是从下拉菜单中,我只能选择id选项。但是功能无法正常工作。对于基于id的下拉菜单,我需要显示用户。 - user17525310
如果您想使用下拉菜单限制结果,请访问以下链接: https://codepen.io/huudai09/pen/wvrPwoN?editors=1011 - hudy9x

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接