javascript - vuejs 2.0 fliter 搜索过滤怎么做啊---新手

查看:116
本文介绍了javascript - vuejs 2.0 fliter 搜索过滤怎么做啊---新手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>vue自定义过滤器</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>

</head>
<body>
    <div id="app">
        <input v-model='search'/>

        <ul v-for="item in products ">
            <li>{{item.name}},价格:¥{{item.price}}</li>

        </ul>
    </div>
    <script>
  Vue.filter('searchFilter', function(value) {
    ...
  })

var vm = new Vue({
  el:'#app',
  data: {
    products: [
            {name: '苹果',price: 25,category: "水果"}, 
            {name: '香蕉',price: 15,category: "水果"}, 
            {name: '雪梨',price: 65,category: "水果"}, 
            {name: '宝马',price: 2500,category: "汽车"},
            {name: '奔驰',price: 10025,category: "汽车"}, 
            {name: '柑橘',price: 15,category: "水果"}, 
            {name: '奥迪',price: 25,category: "汽车"}
        ]
  }
})
</script>
</body>
</html>

解决方案

这是官方的例子
https://cn.vuejs.org/v2/examp...

这是根据你的数据在官方基础上修改的
https://jsfiddle.net/ycloud/b...

html

<div id="app">
  <input v-model='search' />
  <ul v-for="item in searchData ">
    <li>{{item.name}},价格:¥{{item.price}}</li>
  </ul>
</div>

script

var vm = new Vue({
  el: '#app',
  data: {
    search: '',
    products: [{
      name: '苹果',
      price: 25,
      category: "水果"
    }, {
      name: '香蕉',
      price: 15,
      category: "水果"
    }, {
      name: '雪梨',
      price: 65,
      category: "水果"
    }, {
      name: '宝马',
      price: 2500,
      category: "汽车"
    }, {
      name: '奔驰',
      price: 10025,
      category: "汽车"
    }, {
      name: '柑橘',
      price: 15,
      category: "水果"
    }, {
      name: '奥迪',
      price: 25,
      category: "汽车"
    }]
  },
  computed: {
    searchData: function() {
      var search = this.search;

      if (search) {
        return this.products.filter(function(product) {
          return Object.keys(product).some(function(key) {
            return String(product[key]).toLowerCase().indexOf(search) > -1
          })
        })
      }

      return this.products;
    }
  }
})

这篇关于javascript - vuejs 2.0 fliter 搜索过滤怎么做啊---新手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆