【问题标题】:Vue.js filtering on arrayVue.js 过滤数组
【发布时间】:2017-10-02 00:41:58
【问题描述】:

我正在尝试使用 vue.js 中的计算属性过滤数组。我想搜索多个字段、名称、状态、标签等。

我的数据:

events: [
  {
    id: 1,
    name: 'Name of event',
    url: '#',
    datetime: '2017-05-10T00:00:00Z',
    description: 'The full text of the event',
    state: 'VIC',
    tags: [
      'ordinary',
      'advanced'
    ]
  },
  {
    id: 2,
    name: 'Another event',
    url: '#',
    datetime: '2017-05-12T00:00:00Z',
    description: 'The full text of the event',
    state: 'VIC',
    tags: [
      'beginner'
    ]
  },
  {
    id: 3,
    name: 'Great event',
    url: '#',
    datetime: '2017-05-18T00:00:00Z',
    description: 'The full text of the event',
    state: 'NSW',
    tags: [
      'beginner'
    ]
  }
]

},

以下功能按预期工作,但我不知道如何让它搜索“标签”中的项目(已注释掉)。

searchevents: function(){
  let result = this.events
  if (this.filterValue){
    result = result.filter(event =>
      event.name.toLowerCase().includes(this.filterValue.toLowerCase()) ||
      event.state.toLowerCase().includes(this.filterValue.toLowerCase())
      // event.tags.toLowerCase().values().includes(this.filterValue.toLowerCase())
    )
  }
  return result
}

以下返回一个空白数组,当我在 angular 中完成但在 vue 中没有完成时,此方法可以正常工作。

searchevents2: function(){
  var searchRegex = new RegExp(this.filterValue,'i')
  this.events.filter(function(event){
    return !self.filterValue || searchRegex.test(event.name) || searchRegex.test(event.state)
  })
}

理想情况下,我希望能够列出要过滤的数组项,或者只过滤整个数组。

感谢任何帮助,首先在这里发帖,所以要温柔。我在 Python 方面的经验比 Javascript 多得多,所以我有时也可能使用不正确的术语。

【问题讨论】:

  • 我建议你缩短你的问题和代码。
  • 我认为你的问题很好。我要补充的唯一真正有帮助的是一个可以修改以解决您的问题的工作示例。

标签: arrays vue.js filtering


【解决方案1】:

const app = new Vue ({
  el: '#app',
  data: {
    search: '',
    userList: [
      {
        id: 1,
        name: "Prem"
      },
      {
        id: 1,
        name: "Chandu"
      },
      {
        id: 1,
        name: "Shravya"
      }
    ]
  },
  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())
     }).sort(compare)
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.js"></script>
<div id="app">
  <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>

【讨论】:

    【解决方案2】:

    你离得不远了。

    对于您的searchEvents 过滤器,您只需添加标签过滤器。以下是您可以这样做的方法。

    searchevents: function(){
        let result = this.events
        if (!this.filterValue)
          return result
    
        const filterValue = this.filterValue.toLowerCase()
    
        const filter = event => 
            event.name.toLowerCase().includes(filterValue) ||
            event.state.toLowerCase().includes(filterValue) ||
            event.tags.some(tag => tag.toLowerCase().includes(filterValue))
    
        return result.filter(filter)
    }
    

    Array.some() 是一种标准数组方法,如果数组中的任何元素通过您的测试,则返回 true。

    searchevents2: function(){
        const searchRegex = new RegExp(this.filterValue,'i')
        return this.events.filter(event => 
          !this.filterValue || searchRegex.test(event.name) || searchRegex.test(event.state))
    }
    

    有了searchEvents2,你真的只留下了一个错误的self。要么你需要在执行过滤器之前设置self,要么就像我在这里所做的那样,把它变成一个箭头函数。

    Example.

    【讨论】:

    • 非常感谢。完美的解释,效果很好。我修改了第二个函数并为 event.tags 添加了一个测试,我认为它运行良好且代码更简洁。
    • @KyleHadland 很高兴为您提供帮助:)
    猜你喜欢
    • 2020-11-25
    • 2018-05-01
    • 2021-04-13
    • 2020-01-21
    • 1970-01-01
    • 2020-07-11
    • 2020-11-07
    • 2021-08-16
    • 2020-02-01
    相关资源
    最近更新 更多