如上所示,这篇我们将商品分类菜单显示数量的提示完善,是软件更加易于使用。
好先让我回顾一下上节课的内容,Goods组件,数量提示功能最终需要在Goods组件内显示。
<template> <div> <div ref="menuScroll"> <ul> <!--专场--> <li :class="{'current':currentIndex===0}" @click="selectMenu(0)"> <p> <img :src="container.tag_icon" v-if="container.tag_icon"> {{container.tag_name}} </p> </li> <li v-for="(item,index) in goods" :class="{'current':currentIndex===index+1}" @click="selectMenu(index+1)" > <p> <img :src="item.icon" v-if="item.icon"> {{item.name}} </p> <i v-show="calculateCount(item.spus)">{{calculateCount(item.spus)}}</i>//通过i标签展示数量 </li> </ul> </div> <!-- 右侧商品列表 --> <div ref="foodScroll"> <!--专场--> <ul> <li> <div v-for="item in container.operation_source_list"> <img :src="item.pic_url"> </div> </li> <!-- 具体分类--> <li v-for="item in goods"> <h3>{{item.name}}</h3> <!--具体商品列表--> <ul> <li v-for="food in item.spus"> <div :style="head_bg(food.picture)"></div> <div> <h3>{{food.name}}</h3> <p v-if="food.description">{{food.description}}</p> <div> <span>{{food.month_saled_content}}</span> <span>{{food.praise_content}}</span> </div> <img :src="food.product_label_picture" v-show="food.product_label_picture" > <p> <span>¥{{food.min_price}}</span> <span>/{{food.unit}}</span> </p> </div> <div> <Cartcontrol :food="food"></Cartcontrol> </div> </li> </ul> </li> </ul> </div> <Shopcart :poiInfo="poiInfo" :selectFoods="selectFoods"></Shopcart> </div> </template>
<script> import BScroll from "better-scroll"; import Shopcart from "components/Shopcart/Shopcart"; import Cartcontrol from "components/Cartcontrol/Cartcontrol"; export default { data() { return { container: {}, goods: [], poiInfo: {}, listHeight: [], menuScroll: {}, foodScroll: {}, scrollY: 0 }; }, components: { BScroll, Shopcart, Cartcontrol }, created() { this.$axios .get("api/goods") .then(response => { let json = response.data; if (json.code === 0) { // 重点 this.container = json.data.container_operation_source; this.goods = json.data.food_spu_tags; console.log(this.goods) this.poiInfo = json.data.poi_info; this.$nextTick(function() { this.initScroll(); // 左右联动 // 右->左 // 计算区间 this.caculateHeight(); }); } }) .catch(function(error) { // handle error console.log(error); }); }, computed: { // 根据右侧判断左侧index currentIndex() { for (let i = 0; i < this.listHeight.length; i++) { let start = this.listHeight[i]; let end = this.listHeight[i + 1]; if (this.scrollY >= start && this.scrollY < end) { return i; } } return 0; }, selectFoods() { let foods = []; this.goods.forEach(good => { good.spus.forEach(food => { if (food.count > 0) { foods.push(food); } }); }); return foods; } }, methods: { head_bg(imgName) { return "background-image: url(" + imgName + ");"; }, initScroll() { this.menuScroll = new BScroll(this.$refs.menuScroll, { click: true }); this.foodScroll = new BScroll(this.$refs.foodScroll, { probeType: 3, click: true }); this.foodScroll.on("scroll", pos => { this.scrollY = Math.abs(Math.round(pos.y)); }); }, caculateHeight() { let foodList = this.$refs.foodScroll.getElementsByClassName( "food-list-hook" ); let height = 0; this.listHeight.push(height); for (let i = 0; i < foodList.length; i++) { height += foodList[i].clientHeight; this.listHeight.push(height); } // [0, 215, 1343, 2425, 3483, 4330, 5823, 7237, 8022, 8788, 9443] }, selectMenu(index) { // console.log(index); let foodlist = this.$refs.foodScroll.getElementsByClassName( "food-list-hook" ); // 根据下标,滚动到相对应的元素 let el = foodlist[index]; // 滚动到对应元素的位置 this.foodScroll.scrollToElement(el, 100); }, calculateCount(spus) { console.log(spus) let count = 0; spus.forEach(food => { if (food.count > 0) { count += food.count; } }); return count; }, } }; </script>
注意methods方法中的calculateCount函数实现计算个数,数量来自于增减组件内Cartcontrol。
calculateCount(spus) { console.log(spus) let count = 0; spus.forEach(food => { if (food.count > 0) { count += food.count; } }); return count; },
以上是spus数据
Cartcontrol组件控制商品增减
通过组件Cartcontrol接受了来自父组件的传值,并且我们在组件内添加商品的增减功能。