收银最麻烦的 就是 它的 字段设置了 ,数量 ,总数,价格 ,总价,显示在右侧统计区等等吧,今天正好用到 就把他做一下 简单的 整理 方便以后不用写轮子了
我的列表呢是一个这样的 二维数组 第一层是分类 ,分类下面带着各类的食品名称。
所以首先得思路 是 把在api接口获取到的数据进行foreach 循环一下 ,之后再进行 num字段的添加,最后向data里面记性赋值。
async getlist() {
let arr = {
food_time_type: 1,
restaurant_id: this.$store.getters['user/restaurant_id'],
}
const {
data: { list },
} = await lists(arr)
list.forEach((item) => {
item.food_list.forEach((tem) => {
tem.num = '0'
})
})
this.list = list
},
还有一个就是 element ui 里面的 input-number v-model 和 @Change 方法
v-model 跟item.num 进行绑定 这样数据就可以双向改变了 ,后面 在 @Change里面 传入item 后进行增加 或 减少 以及删除。
handleChange(value) {
if (value.num != 0) {
if (this.cart.includes(value)) {
for (let i in this.cart.value) {
this.cart[i].num++
}
} else {
this.cart.push(value)
console.log(this.cart)
}
} else {
let index = this.cart.indexOf(value)
this.cart.splice(index, 1)
}
},
返回总价的话 就在computed 里面直接返回 this.cart 里面 的num总价即可,this.cart 就是用来存储 信息使用的 。
toprice() {
var price_total = 0
this.cart.forEach((item) => {
price_total += item.num * item.price
})
return price_total.toFixed(2)
},