Export Class Product && ManageProduct(ok)
C:\Users\Administrator\Desktop\ts\Product.ts
export class Product {
private _id: number;
private _name: string;
private _price: number;
private _description: string;
private _state: boolean;
private _image: string;
constructor(id: number, name: string, price: number, description: string, state: boolean, image: string) {
this._id = id;
this._name = name;
this._price = price;
this._description = description;
this._state = state;
this._image = image;
}
public get id(): number {
return this._id;
}
public set id(variable: number) {
this._id = variable;
}
public get name(): string {
return this._name;
}
public set name(variable: string) {
this._name = variable;
}
public get price(): number {
return this._price;
}
public set price(variable: number) {
this._price = variable;
}
public get description(): string {
return this._description;
}
public set description(variable: string) {
this._description = variable;
}
public get state(): boolean {
return this._state;
}
public set state(variable: boolean) {
this._state = variable;
}
public get image(): string {
return this._image;
}
public set image(variable: string) {
this._image = variable;
}
}
C:\Users\Administrator\Desktop\ts\ManageProduct.ts
import {Product} from './Product';
export class ManageProduct {
private _products : Product[] = [];
constructor() {
let product1 = new Product(1,'Product Name 1', 1000,'Description Product Name 1', true, "1.jpg");
let product2 = new Product(2,'Product Name 2', 2000,'Description Product Name 2', false, "2.jpg");
let product3 = new Product(3,'Product Name 3', 3000,'Description Product Name 3', true, "3.jpg");
let product4 = new Product(4,'Product Name 4', 4000,'Description Product Name 4', false, "2.jpg");
this.addProductToCart(product1);
this.addProductToCart(product2);
this.addProductToCart(product3);
this.addProductToCart(product4);
}
addProductToCart(product: Product): void {
this._products.push(product);
}
getProduct(): string {
let products: Product[] = this._products;
let length = products.length;
var html:string = "";
for (let i:number = 0; i < length; ++i) {
html += `
<div class="col-md-3">
<div class="card mb-4 shadow-sm">
<img src="./images/${products[i].image}" alt="${products[i].image}">
<div class="card-body">
<p class="text-danger text-center">${products[i].price}</p>
<p class="text-success text-center">${products[i].name}</p>
<p class="card-text">${products[i].description}</p>
<div class="d-flex justify-content-center align-items-center">
<button type="button" class=" btn btn-primary btn-sm buynow" data-idsp="${products[i].id}">Mua hàng</button>
</div>
</div>
</div>
</div>
`;
}
return html;
}
getProductById(id: number): Product {
let products: Product[] = this._products;
let length = products.length;
for (let i:number = 0; i <= length; ++i) {
if(i == (id - 1)) {
return products[i];
}
}
}
}
Last updated