HTML

  1. <div ng-app="app">
  2.  
  3. <div class="l-header">
  4. <div class="l-cart">
  5. <div class="l-cart-count" ng-click="showCart = !showCart">{{ calculateTotalProducts() }}</div>
  6. <div class="l-cart-items" ng-show="showCart">
  7. <div ng-show="cart.length">
  8. <ul>
  9. <li ng-repeat="item in cart">
  10. <div class="l-cart-item-name">{{ item.product.name }}</div>
  11. <div class="l-cart-item-quantity">
  12. <input type="number" ng-model="item.quantity" ng-change="removeIfZero(item)" />
  13. </div>
  14.  
  15. <div class="l-cart-item-subtotal">{{ item.quantity * item.product.price | currency }}</div>
  16. <div class="remove-item">
  17. <img src="https://cdn3.iconfinder.com/data/icons/cleaning-icons/512/Trash_Can-512.png" ng-click="removeFromCart(item)">
  18. </div>
  19. </li>
  20. </ul>
  21. <div class="l-cart-total">total <b>{{ calculateTotalPrice() | currency }}</b></div>
  22. </div>
  23. <div class="l-cart-empty" ng-hide="cart.length">tu carrito está vacío</div>
  24. </div>
  25. </div>
  26. </div>
  27.  
  28. <ul class="l-stock">
  29. <li class="l-product" ng-repeat="product in stock" ng-click="addToCart(product)" ng-class="{'is-on-cart': isProductOnCart(product)}">
  30. <div class="l-product-name">{{ product.name }}</div>
  31. <div class="l-product-price">{{ product.price | currency }}</div>
  32. </li>
  33. </ul>
  34.  
  35. </div>

CSS:

  1. body
  2. color #333
  3. padding 60px 10px 10px 10px
  4. font-family Arial, Helvetica, sans-serif
  5. user-select none
  6.  
  7. .is-red
  8. color red !important
  9.  
  10. .l-header
  11. display flex
  12. justify-content flex-end
  13. align-items center
  14. position fixed
  15. top 0
  16. right 0
  17. left 0
  18. height 30px
  19. padding 10px
  20. background-color #2c3e50
  21.  
  22. .l-cart
  23. position relative
  24.  
  25. .l-cart-count
  26. font-size 12px
  27. font-weight 700
  28. width 30px
  29. line-height 30px
  30. text-align center
  31. color #ecf0f1
  32. background-color #27ae60
  33. border-radius 50%
  34. cursor pointer
  35.  
  36. .l-cart-items
  37. position absolute
  38. top 100%
  39. right 0
  40. width 270px
  41. margin 10px -10px 0 0
  42. padding 10px
  43. font-size 12px
  44. background-color #ecf0f1
  45.  
  46. &:before
  47. content ""
  48. position absolute
  49. bottom 100%
  50. right 15px
  51. margin 0 0 -2px 0
  52. border 10px solid transparent
  53. border-bottom-color #ecf0f1
  54.  
  55. li
  56. display flex
  57. align-items center
  58. padding-bottom 10px
  59. margin-bottom 10px
  60.  
  61. .l-cart-item-name
  62. flex 1
  63. overflow hidden
  64. white-space nowrap
  65. text-overflow ellipsis
  66.  
  67. .l-cart-item-quantity
  68. width 30px
  69. margin 0 10px
  70.  
  71. input
  72. display block
  73. border none
  74. padding 5px
  75. margin 0
  76. width 100%
  77. text-align right
  78. appearance none
  79.  
  80. &:focus
  81. outline none
  82. background-color #ffc
  83.  
  84. &::-webkit-outer-spin-button,
  85. &::-webkit-inner-spin-button
  86. -webkit-appearance none
  87. margin 0
  88.  
  89. .l-cart-item-subtotal
  90. color #000
  91. width 70px
  92. text-align right
  93.  
  94. .remove-item img
  95. width:30px
  96. height:30px
  97. margin 0 10px
  98. text-align center
  99.  
  100. .l-cart-total
  101. margin-top 10
  102. padding-top 10px
  103. text-align right
  104. border-top 1px solid #bdc3c7
  105.  
  106. b
  107. font-weight 700
  108. font-size 1.4em
  109.  
  110. .l-cart-empty
  111. text-align center
  112. font-size 1.4em
  113. color #95a5a6
  114.  
  115. .l-stock
  116.  
  117. & > li
  118. float left
  119. margin 0 10px 10px 0
  120.  
  121. &:after
  122. content ""
  123. clear both
  124.  
  125. .l-product
  126. display flex
  127. color #fff
  128. cursor pointer
  129.  
  130. & > div
  131. padding 10px
  132.  
  133. .l-product-name
  134. background-color #2980b9
  135.  
  136. .l-product-price
  137. background-color #3498db
  138.  
  139. .is-on-cart
  140.  
  141. .l-product-name
  142. background-color #27ae60
  143.  
  144. .l-product-price
  145. background-color #2ecc71

JS

  1. /**
  2. * Esta función genera productos aleatoriamente
  3. * (http://marak.com/faker.js/)
  4. **/
  5. function fetchStock () {
  6.  
  7. var i = 0,
  8. stock = [],
  9. total = faker.random.number({min: 10, max: 30});
  10.  
  11. for (i = 0; i < total; i++) {
  12.  
  13. stock.push({
  14. name : faker.commerce.productName(),
  15. price: faker.random.number({min: 1, max: 500})
  16. });
  17. }
  18.  
  19. return stock;
  20. };
  21.  
  22. /**
  23. * Aquí empieza nuestro código Angular...
  24. **/
  25.  
  26. var app = angular.module('app', []);
  27.  
  28. app.run(function ($rootScope) {
  29.  
  30. var cart = [],
  31. stock = fetchStock();
  32.  
  33. var addToCart = function (product) {
  34.  
  35. var item = cart.find(function (item) {
  36.  
  37. return item.product === product;
  38. });
  39.  
  40. if (item) {
  41.  
  42. item.quantity++;
  43.  
  44. } else {
  45.  
  46. cart.push({
  47. product : product,
  48. quantity: 1
  49. });
  50. }
  51. };
  52.  
  53. var removeFromCart = function (item) {
  54.  
  55. var index = cart.indexOf(item);
  56.  
  57. cart.splice(index, 1);
  58. };
  59.  
  60. var removeIfZero = function (item) {
  61.  
  62. if (item.quantity < 1) {
  63.  
  64. removeFromCart(item);
  65. }
  66. };
  67.  
  68. var calculateTotalPrice = function () {
  69.  
  70. return cart.reduce(function (sum, item) {
  71.  
  72. return sum + item.quantity * item.product.price;
  73.  
  74. }, 0);
  75. };
  76.  
  77. var calculateTotalProducts = function () {
  78.  
  79. return cart.reduce(function (sum, item) {
  80.  
  81. return sum + item.quantity;
  82.  
  83. }, 0);
  84. };
  85.  
  86. var isProductOnCart = function (product) {
  87.  
  88. return cart.some(function (item) {
  89.  
  90. return item.product === product;
  91. });
  92. };
  93.  
  94. angular.extend($rootScope, {
  95. stock: stock,
  96. cart: cart,
  97. addToCart: addToCart,
  98. removeFromCart: removeFromCart,
  99. removeIfZero: removeIfZero,
  100. calculateTotalPrice: calculateTotalPrice,
  101. calculateTotalProducts: calculateTotalProducts,
  102. isProductOnCart: isProductOnCart
  103. });
  104. });

IONIC之简易购物车的更多相关文章

  1. Session机制二(简易购物车案例)

    一:案例一(简易购物车) 1.目录结构 2.step1.jsp <%@ page language="java" contentType="text/html; c ...

  2. javaweb练手项目jsp+servlet简易购物车系统

    简易购物车项目 这是一个用intellij IDEA做的简易的javaweb项目,开发环境使用的jdk1.8和tomcat8以及mysql数据库. 1.项目开发准备: 创建github仓库 项目框架搭 ...

  3. angular初始用——简易购物车

    <html> <head> <meta charset="utf-8"> <script src="js/angular.js& ...

  4. Java servlet 实现的简易购物车

    首页 2.购买页 3.购物车页 1. 首页代码 发送一个post请求 <!DOCTYPE html><html lang="en"><head> ...

  5. [ Python -1 ] 简易购物车程序

    练习: 1. 要求用户输入总资产,例如:2000 2. 显示商品列表,让用户根据序号选择商品,加入购物车 3. 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功. goods = [{' ...

  6. ~~小练习:python的简易购物车~~

    进击のpython 1,用户先给自己的账户充钱:比如先充3000元. 2,有如下的一个格式: goods = [{"name": "电脑", "pri ...

  7. HttpSession之简易购物车

    创建一个简单的购物车模型,由三个 jsp 和两个 Servlet 组成: step1.jsp <%@ page language="java" contentType=&qu ...

  8. Newbe.Claptrap 框架入门,第一步 —— 创建项目,实现简易购物车

    让我们来实现一个简单的 “电商购物车” 需求来了解一下如何使用 Newbe.Claptrap 进行开发. 业务需求 实现一个简单的 “电商购物车” 需求,这里实现几个简单的业务: 获取当前购物车中的商 ...

  9. 简易购物车 --day2

    代码段 f =float(input('输入你的工资')) goods=['1.apple','2.mac','3.ph','4.python','5.php'] price=[35,26.5,14, ...

随机推荐

  1. HTML 5 在Web SQL 使用演示样本

    Web sql 这是一个模拟数据库浏览器.可以使用JS操作SQL完成数据读取和写入,但是这件事情并不多,现在支持的浏览器,而其W3C规格已经停止支持.外形似它的前景不是很亮. W3C 规范:http: ...

  2. 对Extjs中时间的多种处理

    1.类型为datetime的json数据处理 (字段类型为datetime) new Date(parseInt(yourTime.substring(6, yourTime.length - 2)) ...

  3. [转载]John Burkardt搜集的FORTRAN源代码

    Over the years, I have collected, modified, adapted, adopted or created a number of software package ...

  4. Net 4.0 之 Dynamic 动态类型

    Net 4.0 之 Dynamic 动态类型 本文主要旨在与网友分享.Net4.0的Dynamic 对Duck Type 的支持.     一..net4.0主要新特性 .Net4.0在.Net3.5 ...

  5. HOJ:2031 进制转换

    进制转换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. iOS基础 - Quartz 2D绘图的基本步骤

    一.使用Quartz 2D绘图的基本步骤 1) 获取上下文context(绘制图形的地方) 2) 设置路径(路径是用来描述形状的) 3)  将路径添加到上下文 4)  设置上下文属性(设置颜色,线宽, ...

  7. 实现WebService的调用与被调用

    之前一直用WCF来开发服务,可是从未用过WebService.对WebService有种很神奇的期待,都说WebService比较简单,但是从未用过就对我来说就是一种新的知识.起始让我来说WCF与We ...

  8. .net通用底层搭建

    .net通用底层搭建 之前写过几篇,有朋友说看不懂,有朋友说写的有点乱,自己看了下,的确是需要很认真的看才能看懂整套思路. 于是写下了这篇. 1.这个底层,使用的是ado.net,微软企业库 2.实体 ...

  9. ASP.NET交互Rest服务接口(Jquery的Get与Post方式)

    ASP.NET交互Rest服务接口(Jquery的Get与Post方式) 本文将通过一个简单的实例,介绍如何创建一个Rest服务接口,以及通过JQUERY去对它进行调用;主要采取两种方式分别为Get跟 ...

  10. c语言:最长对称子串(3种解决方案)

    问题描述: 输入一个字符串,输出该字符串中最大对称子串的长度.例如输入字符串:“avvbeeb”,该字符串中最长的子字符串是“beeb”,长度为4,因而输出为4. 解决方法:中序遍历 一,全遍历的方法 ...