<!DOCTYPE html><html><head> <meta charset=34;utf-8"> <title>hello world</title> <link rel="stylesheet" type="text/css" href="../css/demo.css"> <script src="../js/vue.js"></script></head><body> <div id="demo_table" v-cloak> <template v-if="list.length"> <table> <thead> <th>序号</th> <th>商品名称</th> <th>商品单价</th> <th>购买数量</th> <th>操作</th> <th>全选 <input type="checkbox" :checked="allCheckStatus" @click="allCheck()"></th> </thead> <tbody> <tr v-for="item,index in list"> <td>{{index + 1}}</td> <td>{{item.name}}</td> <td>{{item.price}}</td> <td> <button @click="handleReduce(index)" :disabled="item.count==1"> -</button> {{item.count}} <button @click="handleIncrease(index)">+</button> </td> <td> <button @click="handleDelete(index)">移除</button> </td> <td> <input type="checkbox" :checked="item.checked" @change="change(index)"> </td> </tr> </tbody> </table> <div> 商品总价:{{totalPrice}} </div> </template> <div v-else>列表为空</div> </div> <script src="../js/demo.js"></script></body></html>
var demo_table = new Vue({ el: "#demo_table", data: { //全选状态,与全选按钮绑定 allCheckStatus: true, list: [{ "name": "ja", "price": 1029, "count": 100, "checked": true }, { "name": "book2", "price": 1029, "count": 1, "checked": true }, { "name": "book3", "price": 1876, "count": 19, "checked": true }] }, methods: { handleReduce: function (index) { this.list[index].count--; }, handleIncrease: function (index) { this.list[index].count++; }, handleDelete: function (index) { this.list.splice(index, 1); }, //全选点击事宜 allCheck: function () { for (var i = 0; i < this.list.length; i++) { if (this.allCheckStatus) { this.list[i].checked = false; } else { this.list[i].checked = true; } } if (this.allCheckStatus) { this.allCheckStatus = false; } else { this.allCheckStatus = true; } }, //每行选中状态切换事宜 change : function(index){ if(this.list[index].checked){ this.list[index].checked = false; } else { this.list[index].checked = true; } } }, computed: { totalPrice: function () { var total = 0; for (var i = 0; i < this.list.length; i++) { if(this.list[i].checked){ total += this.list[i].price; } } return total; } }});