I am creating an order entry form where we shall store both master and details data. Below I am showing you my entity details which wrote in typescript.
export interface IOrder {
id: number;
customerId: number;
orderDate: Date;
orderAmount: number;
orderDiscount: number;
finalAmount: number;
status: string;
orderDetail: IOrderDetail[];
}
export interface IOrderDetail {
id: number;
serviceName: string;
serviceDescription: string;
quantity: number;
price: number;
discount: number;
amountAfterDiscount: number;
}
Below is my Form Screenshot:
After clicking on the plus(+) sign it will add order item into the grid. The below mention function will do that the job.
addToGrid() {
this.itemCount=(this.order.orderDetail.length);
this.order.orderDetail[this.itemCount].id = 0;
this.order.orderDetail[this.itemCount].serviceDescription = this.serviceDescription.value;
this.order.orderDetail[this.itemCount].serviceName = this.serviceName.value;
this.order.orderDetail[this.itemCount].price = this.price.value;
this.order.orderDetail[this.itemCount].quantity = this.quantity.value;
this.order.orderDetail[this.itemCount].discount = this.discount.value;
this.order.orderDetail[this.itemCount].amountAfterDiscount = ((this.quantity.value * this.price.value) - this.discount.value);
console.log(this.order.orderDetail);
}
I am getting orderDetail undefined error. Please help me to short out my issue.
Below is my Component.ts file:
export class AddorderComponent implements OnInit {
dbops = DBOperation.create;
orderForm: FormGroup;
itemList: IServiceOffer[];
custList: ICustomer[];
id: number;
order: IOrder;
errorMessage: any;
title: string;
itemCount: number;
constructor(private _fb: FormBuilder, private calendar: NgbCalendar,
private _avRoute: ActivatedRoute,
private _orderService: OrderService,
private _router: Router) {
if (this._avRoute.snapshot.params["id"]) {
this.id = this._avRoute.snapshot.params["id"];
}
this.orderForm = this._fb.group({
id: ['', [Validators.required]],
customerId: ['', [Validators.required]],
orderDate: ['', [Validators.required]],
orderAmount: [ '' , [Validators.required]],
orderDiscount: [''],
finalAmount: [''],
status: [''],
serviceId: [''],
serviceName: ['', [Validators.required]],
serviceDescription: ['',[Validators.required]],
price: [''],
quantity: ['',[Validators.required, Validators.pattern("^[0-9]*$")]],
discount: [ '', [Validators.pattern("^[0-9]*$")]],
amountAfterDiscount: ['']
});
}
ngOnInit() {
this.getCustomerList();
this.getServiceList();
if (this.id > 0) {
this.editOrder(this.id);
// console.log(this.customer);
} else {
this.addOrder();
}
}
saveOrder() {
this.order = this.orderForm.value;
if (!this.orderForm.valid) {
return;
}
if (this.dbops == DBOperation.create) {
console.log(this.order);
this._orderService
.saveOrder(Global.BASE_API_ENDPOINT + "order/add", this.order)
.subscribe(
() => {
this._router.navigate(["order"]);
},
error => (this.errorMessage = error)
);
} else if (this.dbops == DBOperation.update) {
this._orderService
.updateOrder(
Global.BASE_API_ENDPOINT + "order/update",
this.order
)
.subscribe(
() => {
this._router.navigate(["order"]);
},
error => (this.errorMessage = error)
);
}
}
cancel() {
this._router.navigate(["order"]);
}
addOrder() {
this.dbops = DBOperation.create;
this.title = "Add New Order";
}
editOrder(id: number) {
this.dbops = DBOperation.update;
this.title = "Edit Order";
this._orderService
.getOrderById(Global.BASE_API_ENDPOINT + "order/details", id)
.subscribe(data => this.orderForm.setValue(data));
}
getCustomerList() {
this._orderService
.getCustomerList(Global.BASE_API_ENDPOINT + "customer/allcustomer")
.subscribe(data => (this.custList = data));
}
getServiceList() {
this._orderService
.getServiceList(Global.BASE_API_ENDPOINT + "serviceoffer/allservice")
.subscribe(data => (this.itemList = data));
}
addToGrid() {
this.itemCount=(this.order.orderDetail.length);
this.order.orderDetail[this.itemCount].id = 0;
this.order.orderDetail[this.itemCount].serviceDescription
= this.serviceDescription.value;
this.order.orderDetail[this.itemCount].serviceName
= this.serviceName.value;
this.order.orderDetail[this.itemCount].price = this.price.value;
this.order.orderDetail[this.itemCount].quantity = this.quantity.value;
this.order.orderDetail[this.itemCount].discount = this.discount.value;
this.order.orderDetail[this.itemCount].amountAfterDiscount
= ((this.quantity.value * this.price.value) - this.discount.value);
console.log(this.order.orderDetail);
}
getServicedetails(value: any) {
this._orderService
.getServiceDetails
(Global.BASE_API_ENDPOINT + "serviceoffer/details", value)
.subscribe(data => (
this.serviceDescription.setValue(data.serviceDescription),
this.serviceName.setValue(data.serviceName),
this.price.setValue(data.servicePrice)
));
}
get customerId() {
return this.orderForm.get("customerId");
}
get serviceId() {
return this.orderForm.get("serviceId");
}
get serviceName() {
return this.orderForm.get("serviceName");
}
get serviceDescription() {
return this.orderForm.get("serviceDescription");
}
get price() {
return this.orderForm.get("price");
}
get quantity() {
return this.orderForm.get("quantity");
}
get discount() {
return this.orderForm.get("discount");
}
get orderDate() {
return this.orderForm.get("orderDate");
}
get orderAmount() {
return this.orderForm.get("orderAmount");
}
get orderDiscount() {
return this.orderForm.get("orderDiscount");
}
get finalAmount() {
return this.orderForm.get("finalAmount");
}
get orderDetail() {
return this.orderForm.get("order.orderDetail[]");
}
get status() {
return this.orderForm.get("status");
}