article

Thursday, January 13, 2022

AngularJs Simple Shopping Cart

AngularJs Simple Shopping Cart

Bootstrap 5.1 Version
https://getbootstrap.com/docs/5.1/getting-started/introduction/

angularjs CDN
https://angularjs.org/ Version 1.8.2 https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//index.html
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8" name = "viewport" content = "width=device-width, initial-scale=1" />
<title>AngularJs Shopping Cart</title>
<link rel = "stylesheet" type = "text/css" href = "https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"/>
</head>
<body ng-app="shoppingcart" ng-controller="shoppingCTR">
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
      <div class="container-fluid">
        <a class="navbar-brand" href="#">Cairocoders</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
          <ul class="navbar-nav">
            <li class="nav-item">
              <a class="nav-link active" aria-current="page" href="#">Home</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>
     
    <div class="container">
    <div class="row">
        <h3 class = "text-primary">AngularJs Shopping Cart</h3>
        <hr style = "border-top:1px dotted #ccc;"/>
        <div id = "bg-background" class="col-8">
            <h4>PRODUCTS<h4>
            <hr style = "border-top:1px groovy #ccc;">
            <div id = "product">
                <div id = "p_list" ng-repeat = "product in products ">
                    <span style="font-size:16px;">{{product.p_name}}</span>
                    <center><img ng-src = "{{product.p_image}}"/></center>
                    <div><label style="font-size:14px;">Price: ₱{{product.p_price}}</label></div>
                    <center><button type = "button" class="btn btn-primary" ng-click = "add_cart(product)"> Add to cart</button></center>
                </div>
            </div>
        </div>
        <div class="col-4">
            <div class = "panel panel-primary">
                <div class = "panel-heading">
                    <h5>MY CART</h5>
                </div>
                <div class = "panel-body table-responsive">
                    <table class = "table">
                        <thead>
                            <tr>
                                <th>Product</th>
                                <th>Price</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr ng-repeat = "cart in carts" ng-init = "setTotals(cart)">
                                <td>{{cart.p_name}}</td>
                                <td>₱{{cart.p_price}}</td>
                                <td><button type = "button" ng-click = "remove_cart(cart)" class = "btn btn-danger"> X</button></td>
                            </tr>
                            <tr>
                                <td align = "right">Total</td>
                                <td>₱{{total}}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        </div>
    </div>
<script>
var app = angular.module("shoppingcart", [])
.controller("shoppingCTR", function($scope){
    $scope.carts=[];
    $scope.products = [
        {p_id: "1", p_name: "Dell Inspiron 15 3501", p_image: "images/1.jpg", p_price: 5900},
        {p_id: "2", p_name: "Asus X515 11th Gen intel", p_image: "images/2.jpg", p_price: 6800},
        {p_id: "3", p_name: "Lenovo IdealPad 3", p_image: "images/3.jpg", p_price: 4800},
    ];
                 
    $scope.add_cart = function(product){
        if(product){
            $scope.carts.push({p_id: product.p_id, p_name: product.p_name, p_price: product.p_price});
        }  
    }
                         
    $scope.total = 0;
                 
    $scope.setTotals = function(cart){
        if(cart){
            $scope.total += cart.p_price;
        }
    }
                 
    $scope.remove_cart = function(cart){
        if(cart){
            $scope.carts.splice($scope.carts.indexOf(cart), 1);
            $scope.total -= cart.p_price;
        }
    }
});
</script>
<style>
#bg-background{
    background-color:#fff;
    padding:10px;
    border-radius:5px;
}
#product{
    padding:5px;
    clear:both;
}
#p_list{
    width:200px;
    max-width:200px;
    height:260px;
    padding:5px;
    border:1px solid #000;
    float:left;
    margin:15px;
}
#p_list img{
    height:150px;
    width:150px;
}
</style>
</body>  
</html>

Related Post