<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#shopTable {
width: 500px;
text-align: center;
}
</style>
</head>
<body>
<div id="app">
<table :id="ID" :border="2" :cellspacing="0">
<tr>
<th>序号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品数量</th>
<th>总价</th>
<th>编辑</th>
</tr>
<tr v-for="(goods,index) in shopArr" :key="index">
<td v-text="goods.id"></td>
<td v-text="goods.type"></td>
<td v-text="goods.price"></td>
<!-- 数量 增加 减少 -->
<td>
<button @click = "goods.count = goods.count-- <=0 ? 0:goods.count">-</button>
<span>{{goods.count}}</span>
<button @click = "goods.count++">+</button>
</td>
<!-- 总价 -->
<td v-text="(goods.count*goods.price).toFixed(2)"></td>
<td>
<button @click = "del(index)" :disabled="goods.count>0">删除</button>
</td>
</tr>
<!-- <template>
<tr>
<th>显示</th>
</tr>
</template> -->
</table>
<p>所有商品总价为:{{totalPrice}}元</p>
</div>
<script src="../vue.js"></script>
<script>
var shopArr = [
{
id: 1,
type: "篮球鞋",
price: 1999,
count: 1
},
{
id: 2,
type: "篮球鞋1",
price: 1999,
count: 1
},
{
id: 3,
type: "篮球鞋2",
price: 1999,
count: 1
},
{
id: 4,
type: "篮球鞋3",
price: 1999,
count: 1
},
{
id: 5,
type: "篮球鞋4",
price: 1999,
count: 1
},
]
new Vue({
el: "#app",
data: {
ID: "shopTable",
shopArr
},
methods: {
del(index){
// 指定删除
this.shopArr.splice(index,1)
}
},
computed:{
// 计算总价
totalPrice(){
var allPrice = 0;
for(let i = 0; i < this.shopArr.length; i++){
//单价*数量
allPrice += this.shopArr[i].price*this.shopArr[i].count
}
return allPrice.toFixed(2)
}
}
})
</script>
</body>
</html>
相关文章
- 02-06【实战】简单的API接口FUZZ小案例
- 02-06[转]Vue中Vuex的详解与使用(简洁易懂的入门小实例)
- 02-06vue快速入门的三个小实例
- 02-06Vue(小案例_vue+axios仿手机app)_Vuex优化购物车功能
- 02-06Vue高级用法之mixin的使用【实用案例讲解】
- 02-06Vue(小案例_vue+axios仿手机app)_图片列表操作
- 02-06使用Android Studio写一个发短信的小案例
- 02-06Vue中计算属性(computed)和监听属性函数watch的比较
- 02-06vue---watch、computed和methods之间的区别
- 02-06Vue表单操作的小tips