Angular Lab5
Angular Lab5
Bài 1:
Xây dựng sản phẩm
Thiết kế giỏ hàng khi người dùng click mua sản phẩm như sau:
Hướng dẫn:
bai1.component.css
img {
width: 100%;
padding: 20px;
}
.input-qty {
width: 60px;
height: 25px;
float: right
}
.table td,
th {
padding: 0.3rem;
}
Th.S Nguyễn Đình Hoàng 1
Lab5 Angular 16
bai1.component.html
<div class="text-right mt-1">
<button class="btn btn-primary" data-toggle="modal" data-
target="#cartModal">Cart
({{totalItems()}})
</button>
</div>
</div>
<div class="modal-body p-0">
<table class="table table-bordered mb-0">
<tr *ngFor="let items of cartItems; let i=index">
<td>{{items.title}}</td>
<td style="width:120px">QTY:
<input [(ngModel)]="items.qtys" class="form-control
input-qty" type="number" min="1">
</td>
<td class="text-right">{{items.price | currency:
"VND"}}</td>
<td>
<button (click)="removeItem(i)"><span class="fa fa-
trash"></span></button>
</td>
</tr>
<tr *ngIf="cartItems.length == 0">
<td colspan="4" class="text-center">Cart is empty</td>
</tr>
<tr *ngIf="cartItems.length > 0">
<td></td>
<td class="text-right">Cart Total </td>
<td class="text-right text-danger font-weight-
bold">{{Total()| currency:"VND"}}</td>
<td></td>
</tr>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-
dismiss="modal">Close</button>
<div class="container">
<div class="row">
<div class="col-6 col-sm-3 text-center" *ngFor="let item of products">
<img [src]="item.image" alt="">
<h5>{{ item.title }}</h5>
<h6 class="text-danger">{{item.price | currency:'VND' }}</h6>
<p class="text-center"><input [(ngModel)]="item.qty" type="number"
class="form-control" placeholder="Qty"
min="1" /></p>
bai1.component.ts
products: any[] = [
{ id: 1, title: 'Macbook Pro', price: 25000000, qty: 1, image:
'./assets/images/1.jpg' },
{ id: 2, title: 'Asus ROG Gaming', price: 17000000, qty: 1, image:
'./assets/images/2.jpg' },
{ id: 3, title: 'Amazon Kindle', price: 15000000, qty: 1, image:
'./assets/images/3.jpg' },
{ id: 4, title: 'Another Product', price: 18000000, qty: 1, image:
'./assets/images/4.jpg' },
]
cartItems: any[] = []
addToCart(itemToAdd: any) {
// Add the item or increase qty
let itemInCart = this.cartItems.filter(item => item.id === itemToAdd.id);
let isItemInCart = itemInCart.length > 0;
if (isItemInCart === false) {
this.cartItems.push({
id: itemToAdd.id, title: itemToAdd.title,
price: itemToAdd.price, qtys: itemToAdd.qty, image: itemToAdd.image
})
} else {
itemInCart[0].qtys += itemToAdd.qty;
}
itemToAdd.qty = 1;
console.log(this.cartItems)
console.log(this.products)
}
Thiết kế giỏ hàng khi người dùng click mua sản phẩm như sau:
− Giỏ hàng được thay đổi số lượng, khi thay đổi số lượng thì tính lại tiền và tổng tiền
− Xóa sản phẩm thì tính lại tổng tiền
− Chức năng tìm kiếm, khách có thể tìm kiếm theo tên
Hướng dẫn:
bai2.component.css
img {
width: 100%;
height: 100%;
}
.input[type=text] {
.input[type=text]:focus {
width: 100%;
}
bai2.component.html
<div class="container mt-3">
<div class="row">
<div class="col-12 col-sm-4 col-md-4 col-lg-4 mt-2 ">
<input type="text" [(ngModel)]="searching" placeholder="Tìm sản
phẩm" class="input form-control mr-sm-2">
</div>
<div class="col-12 col-sm-4 col-md-4 col-lg-4 mt-2 form-inline my-2 my-
lg-0">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit"
data-toggle="modal"
data-target=".bd-example-modal-xl">Giỏ hàng của bạn ! <span
*ngIf="itemcount()==0">Bạn
chưa mua hàng</span>
<span *ngIf="itemcount()!=0">
Bạn đã mua {{itemcount()}} hàng !
</span>
</button>
</div>
</div>
<div class="row">
<!-- hiển thị sản phẩm dùng ngfor -->
<div class="col-12 col-sm-6 col-md-4 col-lg-3 mt-2 " *ngFor="let item of
filterName()">
<div class="card text-center border-primary" style="height: 100%;">
<div class="card-header bg-primary text-light ">
<!-- hiển thị tên-->
{{ (item.name)}}
</div>
<div class="card-body">
<!-- hiển thị hình -->
<img [src]="item.image" alt=''>
</div>
<div class="card-footer text-danger">
<!-- hiển thị giá -->
{{item.price | currency}}
<br>
<button class="btn btn-primary"
(click)="addCart(item)">Mua</button>
</div>
</div>
</div>
</div>
</td>
<td class="align-middle">{{item.price* item.incart |
currency: "VND"}}</td>
<td class="align-middle"><button class="btn btn-danger"
(click)="Delete(i)">Remove</button></td>
</tr>
<tr>
<th colspan="3">Tổng tiền</th>
<th>{{itemcount()| currency:"VND"}}</th>
<th>{{sumTotal() | currency:"VND"}}</th>
<th><button class="btn btn-danger" (click)="DeleteAll()">Xóa
hết</button>
</th>
</tr>
</table>
<ng-template #elseTemplate>
<div class="btn btn-danger"> Mời bạn chọn mua !</div>
</ng-template>
</div>
</div>
bai2.component.ts
products: any[] = [
{
id: 1,name: "Đồng hồ thụy sỹ",image: "assets/images/1001.jpg",
price: 1200,
incart: 1,
total: 0
},
{
id: 2,
name: "Dell Star X",
image: "assets/images/1003.jpg",
price: 700,
incart: 1,
total: 0
},
{
id: 3,
name: "Sony Vaio 2017",
image: "assets/images/1004.jpg",
price: 1700,
incart: 1,
total: 0
},
{
id: 4,
name: "Máy ảnh Canon",
image: "assets/images/1005.jpg",
price: 300,
incart: 1,
total: 0
},
{
id: 5,
name: "Vòng cưới France",
image: "assets/images/1009.jpg",
price: 7000,
incart: 1,
total: 0
},
{
id: 6,
name: "Motorola thế hệ 5",
image: "assets/images/1011.jpg",
price: 900,
incart: 1,
total: 0
},
{
id: 7,
Bài 3:
Hướng dẫn:
Copy project lab5 và đổi tên thành lab5_menu sau đó thực hiện tạo menu dùng chung cho
các component.
Th.S Nguyễn Đình Hoàng 9