博主信息
博文 16
粉丝 0
评论 0
访问量 13912
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
vue购物车练习
坨坨
原创
588人浏览过
  1. <template>
  2. <div>
  3. <h1>购物车</h1>
  4. <table border="1px" cellspacing="0">
  5. <tr>
  6. <th></th>
  7. <th>编号</th>
  8. <th>商品名称</th>
  9. <th>价格</th>
  10. <th>购买数量</th>
  11. <th>操作</th>
  12. </tr>
  13. <tr v-for="(item,index) in cartList" :key="item.id">
  14. <td><input type="checkbox" v-model="item.checked"></td>
  15. <td>{{item.id}}</td>
  16. <td>{{item.name}}</td>
  17. <td>¥{{item.price}}</td>
  18. <td>
  19. <button @click="item.count--" :disabled="item.count<=0">-</button>
  20. {{item.count}}
  21. <button @click="item.count++">+</button>
  22. </td>
  23. <td><a href="#" @click.prevent="del(index)" >删除</a></td>
  24. </tr>
  25. <tr>
  26. <td colspan="3">总计:</td>
  27. <td colspan="3">¥{{totalPrice}}</td>
  28. </tr>
  29. </table>
  30. </div>
  31. </template>
  32. <script>
  33. export default {
  34. name: 'App',
  35. data(){
  36. return{
  37. cartList: [
  38. {id:1, checked:false, name:'《细说PHP》', price:10, count:1},
  39. {id:2, checked:false, name:'《细说网页制作》', price:20, count:1},
  40. {id:3, checked:false, name:'《细说JavaScript语言》', price:40, count:1},
  41. {id:4, checked:false, name:'《细说DOM和BOM》', price:50, count:1},
  42. {id:5, checked:false, name:'《细说Ajax与jQuery》', price:60, count:1},
  43. {id:6, checked:false, name:'《细说HTML5高级API》', price:70, count:1},
  44. ]
  45. }
  46. },
  47. methods:{
  48. del(id){
  49. this.cartList.splice(id,1)
  50. }
  51. },
  52. computed: {
  53. totalPrice(){
  54. let sum =0
  55. for(let item of this.cartList) {
  56. console.log(item)
  57. if(item.checked) {
  58. sum += item.price * item.count;
  59. }
  60. }
  61. return sum;
  62. }
  63. },
  64. }
  65. </script>
  66. <style>
  67. h1 {
  68. text-align: center;
  69. margin-bottom: 20px;
  70. }
  71. table{
  72. margin: 0 auto;
  73. }
  74. button{
  75. margin: 0 5px;
  76. }
  77. </style>
批改老师:autoloadautoload

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学