简体   繁体   English

Vue 过滤器 - 根据搜索输入返回对象

[英]Vue filter - Return object depending on search input

I have this search input:我有这个搜索输入:

<input type="text" v-model="searchHero" placeholder="Search Hero">

And this "output" area:而这个“输出”区域:

<div class="item wrapper" v-for="hero in filterSearch">
  {{ hero.image }} <br>
  {{ hero.name }} <br>
  {{ hero.bookmark }} <br>
</div>

The idea is that the user searches a hero, and the output should show only the hero he is searching for.这个想法是用户搜索一个英雄,输出应该只显示他正在搜索的英雄。 For example, if he starts typing Ir , the output area should show Iron Man and Iron Man2例如,如果他开始输入Ir ,则输出区域应显示Iron ManIron Man2

This is the full vue js code:这是完整的 vue js 代码:

new Vue({
  el: '#app',
  data: {
    searchHero: '',
    heroes: [
      { name: 'Spiderman', image: true, bookmark: 'Bookmarkhere' },
      { name: 'Hulk', image: true, bookmark: 'Bookmarkhere' },
      { name: 'Iron Man', image: true, bookmark: 'Bookmarkhere' },
      { name: 'Captain America', image: true, bookmark: 'Bookmarkhere' },
      { name: 'Iron Man2', image: true, bookmark: 'Bookmarkhere' }
    ]
  },
  computed: {
    filterSearch() {
      return this.heroes.filter( heroes => {
        return heroes.name.toLowerCase().includes(this.search.toLowerCase())
      })
    }
  }
});

I get the following error in the console:我在控制台中收到以下错误:

[Vue warn]: Error in render: "TypeError: Cannot read property 'toLowerCase' of undefined" (found in ) [Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性 'toLowerCase'”(在 中找到)

I believe that the problem is with the filderSearch(), but I cannot see what.我相信问题出在 filderSearch() 上,但我看不出是什么。

Here is the entire jsfiddle: https://jsfiddle.net/prozik/no2uLgrd/这是整个 jsfiddle: https ://jsfiddle.net/prozik/no2uLgrd/

Note: Replace <div class="item wrapper" v-for="hero in filterSearch"> to注意:将<div class="item wrapper" v-for="hero in filterSearch">

<div class="item wrapper" v-for="hero in heroes"> if you want to see all the heroes. <div class="item wrapper" v-for="hero in heroes">如果您想查看所有英雄。

Your filter expression is a little off, try this你的过滤表达式有点不对,试试这个

computed: {
  filterSearch() {
    return this.heroes.filter( hero => {
      return !this.searchHero ||
        hero.name.toLowerCase().indexOf(this.searchHero.toLowerCase()) > -1
  })
}

} }

Note that javascript array filter passes in each item individually, one at a time (you have coded as if the whole list was passed in).请注意,javascript 数组过滤器单独传入每个项目,一次一个(您已编码为好像传入了整个列表)。

Also added !searchHero so that all are displayed when search is empty.还添加了!searchHero以便在搜索为空时显示所有内容。

Here's the Fiddle这是小提琴

Parameter name heros needed to be hero.参数名称 heros 需要是 hero。

filterSearch () {
  return this.heroes.filter(hero => {
    return hero.name.toLowerCase().includes(this.searchHero.toLowerCase())
  })
}

It's also advisable to add :key="hero.id" to your v-for loop in the template.还建议将:key="hero.id"添加到模板中的 v-for 循环中。 Vue Cheat Sheet Vue 备忘单

I would do this.我会这样做。

much cleaner and readable code.更干净和可读的代码。

filterSearch() {
  return this.heroes.filter( hero => {
    return hero.name.toLowerCase().match(this.searchHero.toLowerCase())
  })
}

Let me know how it works.让我知道它是如何工作的。 Good Luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM