Vue+Element+computed实现购物车
本人写的小程序,功能还在完善中,欢迎扫一扫提出宝贵意见!
该购物车效果采用的是Element-ui库实现的。
采用了computed计算属性来实现逻辑操作。
功能分析:
- 全选/全不选/单选;
- 数量增加、数量减少;
- 总金额 = 数量 * 单价;
- 商品总价:每一项的总金额相加;
- 新增、编辑功能;
- 删除功能。
废话不多说,复制粘贴看效果:
1 <template> 2 <div class="cart"> 3 <el-row class="addBox"> 4 <el-button type="primary" @click="addChange">新增</el-button> 5 </el-row> 6 <div class="content"> 7 <el-table :data="tableData" @selection-change="handleSelectionChange"> 8 <el-table-column type="selection" width="55" align="center" /> 9 <el-table-column prop="name" label="名称" width="180" align="center" /> 10 <el-table-column prop="price" label="单价" width="180" align="center" /> 11 <el-table-column prop="num" label="数量" align="center"> 12 <template slot-scope="scope"> 13 <el-input-number 14 v-model="scope.row.num" 15 :min="1" 16 :max="10" 17 label="描述文字" 18 /> 19 </template> 20 </el-table-column> 21 <el-table-column prop="total" label="总计" align="center"> 22 <template slot-scope="scope"> 23 {{ (scope.row.num * scope.row.price).toFixed(2) }} 24 </template> 25 </el-table-column> 26 <el-table-column fixed="right" label="操作" align="center"> 27 <template slot-scope="scope"> 28 <el-button type="text" size="small" @click="editRow(scope.row)" 29 >编辑</el-button 30 > 31 <el-button 32 @click="deleteRow(scope.$index, tableData)" 33 type="text" 34 size="small" 35 >删除</el-button 36 > 37 </template> 38 </el-table-column> 39 </el-table> 40 <div class="footerBox"> 41 <ul> 42 <li>已选{{ multipleSelection.length }}件商品</li> 43 <li> 44 总价:<span class="totalPrice">¥{{ totalPrice }}</span> 45 </li> 46 </ul> 47 </div> 48 </div> 49 <!-- 新增/编辑 --> 50 <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%"> 51 <el-form 52 :model="formData" 53 label-width="100px" 54 :rules="rules" 55 ref="formData" 56 > 57 <el-form-item label="名称:" prop="name"> 58 <el-input 59 v-model="formData.name" 60 autocomplete="off" 61 placeholder="请输入名称" 62 ></el-input> 63 </el-form-item> 64 <el-form-item label="单价:" prop="price"> 65 <el-input 66 v-model="formData.price" 67 autocomplete="off" 68 placeholder="请输入单价" 69 ></el-input> 70 </el-form-item> 71 <el-form-item label="数量:" prop="num"> 72 <el-input 73 v-model="formData.num" 74 autocomplete="off" 75 placeholder="请输入数量" 76 ></el-input> 77 </el-form-item> 78 </el-form> 79 <div slot="footer" class="dialog-footer"> 80 <el-button @click="dialogVisible = false" size="medium" 81 >取 消</el-button 82 > 83 <el-button type="primary" @click="submitForm('formData')" size="medium" 84 >确 定</el-button 85 > 86 </div> 87 </el-dialog> 88 </div> 89 </template> 90 91 <script> 92 export default { 93 data() { 94 return { 95 dialogTitle: "", 96 rules: { 97 name: [{ required: true, message: "请输入名称", trigger: "blur" }], 98 price: [{ required: true, message: "请输入单价", trigger: "blur" }], 99 num: [{ required: true, message: "请输入数量", trigger: "blur" }], 100 }, 101 dialogVisible: false, 102 formData: { 103 price: "", 104 name: "", 105 num: 0, 106 }, 107 tableData: [ 108 { 109 price: "3.00", 110 name: "西瓜", 111 num: "1", 112 }, 113 { 114 price: "4.00", 115 name: "苹果", 116 num: "1", 117 }, 118 { 119 price: "5.00", 120 name: "香蕉", 121 num: "1", 122 }, 123 { 124 price: "6.00", 125 name: "石榴", 126 num: "1", 127 }, 128 ], 129 multipleSelection: [], 130 }; 131 }, 132 computed: { 133 totalPrice() { 134 var price_total = 0; 135 for (var i = 0; i < this.multipleSelection.length; i++) { 136 price_total += 137 this.multipleSelection[i].num * this.multipleSelection[i].price; 138 } 139 return price_total.toFixed(2); 140 }, 141 }, 142 methods: { 143 //编辑按钮 144 editRow(val) { 145 this.dialogTitle = "编辑"; 146 this.dialogVisible = true; 147 this.formData = val; 148 }, 149 //新增按钮 150 addChange() { 151 this.dialogTitle = "新增"; 152 this.dialogVisible = true; 153 }, 154 //弹窗确定 155 submitForm(formName) { 156 this.$refs[formName].validate((valid) => { 157 if (valid) { 158 this.dialogVisible = false; 159 if (this.dialogTitle == "新增") { 160 this.tableData.push(this.formData); 161 } 162 } else { 163 return false; 164 } 165 }); 166 }, 167 handleSelectionChange(val) { 168 this.multipleSelection = val; 169 }, 170 deleteRow(index, rows) { 171 this.$alert("你确定要删除吗?", "提示", { 172 confirmButtonText: "确定", 173 }) 174 .then(() => { 175 rows.splice(index, 1); 176 }) 177 .catch(() => {}); 178 }, 179 }, 180 }; 181 </script> 182 <style scoped> 183 .cart { 184 width: 80%; 185 margin: 0 auto; 186 } 187 .addBox { 188 padding: 10px; 189 text-align: left; 190 } 191 .content { 192 margin: 10px; 193 border: 1px solid #dedede; 194 } 195 .footerBox { 196 background: #f5f5f5; 197 padding: 14px 14px; 198 color: #646464; 199 } 200 .footerBox ul { 201 display: flex; 202 align-items: center; 203 list-style: none; 204 justify-content: space-between; 205 } 206 .totalPrice { 207 color: red; 208 font-size: 20px; 209 } 210 </style>
若有不明白请加群号:复制 659182980 ,也可扫码,希望能帮助到大家。