Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, Input, OnInit } from '@angular/core';
- import { ProposalModel, ProductModel } from './../../../shared/models/';
- @Component({
- selector: 'mj-proposal-edit',
- templateUrl: './proposal-edit.component.html',
- styleUrls: ['./proposal-edit.component.scss']
- })
- export class ProposalEditComponent implements OnInit {
- @Input() entity: ProposalModel;
- @Input() products: ProductModel[];
- productSelectionData: any;
- selectedProducts: any;
- constructor() { }
- ngOnInit() {
- // Load all products and items
- this.loadProductData();
- this.updateProductSelectionData();
- this.filterProductsSelected();
- }
- loadProductData() {
- this.productSelectionData = [];
- this.products.forEach(product => {
- this.productSelectionData.push(
- { productTitle: product.productTitle, items: product.items })
- });
- console.log('Product Selection, after load: ', this.productSelectionData);
- debugger;
- }
- updateProductSelectionData() {
- // Update Product Selection Object with previously selected data
- // 1. Check if there is previously saved data
- if (this.entity.products !== undefined) {
- // 2. Update productSelectionData with values saved in entity object
- this.productSelectionData.forEach(product => {
- if (this.entity.products !== undefined) {
- this.entity.products.forEach(entityProduct => {
- if (product.productTitle === entityProduct.productTitle) {
- if (product.items !== undefined) {
- product.items.forEach(item => {
- if (entityProduct.items !== undefined) {
- entityProduct.items.forEach(entityItem => {
- if (item.code === entityItem.code) {
- item.selected = true;
- item.quantity = entityItem.quantity;
- item.discount = entityItem.discount;
- }
- });
- }
- });
- }
- }
- });
- }
- });
- console.log('Product Selection, after update: ', this.productSelectionData);
- debugger;
- }
- }
- filterProductsSelected() {
- this.selectedProducts = [];
- this.productSelectionData.forEach(product => {
- this.selectedProducts.push(product)
- });
- this.selectedProducts.forEach(selectedProduct => {
- selectedProduct.items.forEach(item => {
- const itemIndex = selectedProduct.items.indexOf(item);
- if (item.selected === false) {
- selectedProduct.items.splice(itemIndex, 1);
- }
- if (item.selected === undefined) {
- selectedProduct.items.splice(itemIndex, 1);
- }
- });
- });
- console.log('Selected Products, after filter: ', this.selectedProducts);
- console.log('Product Selection, after filter: ', this.productSelectionData);
- debugger;
- }
- }
Add Comment
Please, Sign In to add comment