javascript - 在 Vue.js 中动态过滤对象数组

标签 javascript vue.js

我有一个 Vue.js 应用程序。在此应用程序中,我尝试将过滤器值动态应用于对象的 Array。 Array 中的每个对象都有字段。我正在尝试按字段值过滤这些对象。每个字段都可以按多个值过滤。

此时,我一直没有弄清楚如何进行这种过滤。我试过使用 JavaScript 内置的 filter 函数。但是,这总是为我返回一个空的结果集。我把这个 Fiddle 放在一起了,其中包括以下代码:

new Vue({
  el: '#app',
  data: {
    currentFilterProperty: '',
    currentFilterValue: '',

    cols: [
      { title: 'Name', prop:'name' },
      { title: 'Age', prop:'age' },
      { title: 'Birthday', prop:'birthday' },      
    ],

    dataFilters: [],
    data: [
      { name:'Patricia Miller', age:69, birthday:'04-15-1948' },
      { name:'Bill Baggett', age:62, birthday:'05-07-1955' },      
      { name:'Maxine Thies', age:21, birthday:'11-28-1995' },      
      { name:'Alison Battle', age:65, birthday:'08-07-1952' },      
      { name:'Dick Triplett', age:25, birthday:'08-27-1982' } 
    ]
  },

  methods: {
    addFilter: function() {
      var f = this.dataFilters[this.currentFilterProperty];
      if (!f) {
        this.dataFilters = {};
        this.dataFilters[this.currentFilterProperty] = [ this.currentFilterValue ];
      } else {
        this.dataFilters[this.currentFilterProperty].push(this.currentFilterValue);
      }

      // How to apply filter?
    }
  }
})

我不确定如何将过滤器应用于 data 对象。

最佳答案

完整的解决方案。最佳测试:添加过滤器 Age 62,然后添加 Birthday 04-15-1948,然后在 Name Patricia 中添加“tri”。

new Vue({
  el: '#app',
  data: {
    filteredProperty: 'name',
    query: '',
    activeFilters: [],
    data: [
      {name: 'Patricia Miller', age: 62, birthday: '04-15-1948'},
      {name: 'Bill Baggett', age:62, birthday: '04-15-1948' },
      {name:'Maxine Thies', age:62, birthday:'11-28-1948'},
      {name:'Alison Battle', age:65, birthday:'08-07-1952'},      
      {name:'Dick Triplett', age:25, birthday:'08-27-1982'}
    ]
  },
  computed: {
    filtered () {
      var filtered = this.data
      this.activeFilters.forEach(filter => {
        filtered = filtered.filter(record => {
          return filter.name === 'name'
            ? new RegExp(filter.value, 'i').test(record[filter.name])
            : record[filter.name] == filter.value
        })
      })
      return filtered
    }
  },
  methods: {
    addFilter () {
      this.activeFilters.push({
        name: this.filteredProperty,
        value: this.query
      })
      this.query = ''
    },
    removeFilter (idx) {
      this.activeFilters.splice(idx, 1)
    }
  }
})
<div id="app">
  <div>
    <select v-model="filteredProperty">
      <option value="name">Name</option>
      <option value="age">Age</option>
      <option value="birthday">Birthdate</option>
    </select>
    <input placeholder="filter value" v-model="query">    
    <button @click="addFilter">add filter</button>
  </div>
  <hr>
  <table v-if="activeFilters.length">
    <tr style="width: 100px">
      <th colspan="3">Filters in use:</th>
    </tr>
    <tr v-for="(filter, index) in activeFilters" :key="index">
      <td>{{ _.capitalize(filter.name) }}:</td>
      <td>{{ filter.value }}</td>
      <td style="padding-left: 10px;">
        <a href="#" @click.prevented=removeFilter(index)>
          remove
        </a>
      </td>
    </tr>
  </table>
  <hr v-if="activeFilters.length">
  <table>
    <tbody>
      <tr v-for="(record, index) in filtered" :key="index">
        <td style="padding-right:18px;">{{ record.name }}</td>
        <td style="padding-right:18px;">{{ record.age }}</td>
        <td>{{ record.birthday }}</td>
      </tr>
    </tbody>
  </table>  
</div>

<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/lodash"></script>

关于javascript - 在 Vue.js 中动态过滤对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47354334/

相关文章:

javascript - 加载弹出页面时在 chrome 扩展弹出页面中调用 Angular 方法?

javascript - Bootstrap typeahead 使用 json 对象而不是字符串

javascript - Cloud Firestore 保存额外的用户数据

vue.js - 为什么在 Vue 中的 require() 方法之后需要默认值?

javascript - 使用 javascript 填充动态表单

javascript - 移除部分图片 src 属性

vue.js - Vue 应用程序仅在本地开发中正确渲染 API 数据,而不是在生产构建中(使用 vuex、axios 和 Parcel)

javascript - 如何在 vue.js 中的路由之间传递 Prop ?

vue.js - 通过参数传递的 id 在页面刷新时丢失

javascript - 如何使用 jquery 根据索引号在一系列元素上添加自定义属性