最详细Vue实现筛选功能案例

一.html

	    <template>
	  <div>
	     <button v-for="item in tabList" :key="item.type" @click="filterGoods(item.type)">{{item.name}}</button>
	<ul>
	    <li v-for ="item in goods" :key="item.id">
	   <p>name:{{item.name}}</p>
	   <p>belong:{{item.belong}}</p>
	   <p>price:{{item.price}}</p>
	   <p>nums:{{item.nums}}</p>
	   <p>promotion:{{item.promotion}}</p>
	  </li>
	</ul>
	  </div>
	</template>

二.css

    li{
  margin-top: 20px
}
input{
  background: red
}

三.js

     data() {
  return {
    goodList:[
      {id: 1,name:'苹果',belong:1,price:10,nums:'100kg',promotion:'促销'},
        {id: 2,name:'香蕉',belong:1,price:50,nums:'200kg',promotion:'促销'},
          {id: 3,name:'葡萄',belong:1,price:100,nums:'300kg',promotion:'促销'},
            {id: 5,name:'西红柿',belong:2,price:120,nums:'1200kg',promotion:'促销'},
              {id: 6,name:'苦瓜',belong:2,price:140,nums:'1020kg',promotion:'促销'}
    ],
     tabList:[
        {type:0,name:'全部'},
        {type:1,name:'水果'},
        {type:2,name:'蔬菜'},
      ],
      goods:[],
    // fruit:''
  }
},
 methods: {
  filterGoods(type){
    if(type ==0){
      // this.goods =this.goodList
      this.goods.splice(0);
      this.goods.push(...this.goodList)
    }else{
      let res =this.goodList.filter(item=>{
        return item.belong ==type
      })
      this.goods.splice(0);
      this.goods.push(...res)
    }
  }
 } ,
 mounted() {
   this.filterGoods(0)
 },
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值