Guest User

Untitled

a guest
Oct 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. import { Component, Input, OnInit } from '@angular/core';
  2. import { ProposalModel, ProductModel } from './../../../shared/models/';
  3.  
  4. @Component({
  5. selector: 'mj-proposal-edit',
  6. templateUrl: './proposal-edit.component.html',
  7. styleUrls: ['./proposal-edit.component.scss']
  8. })
  9. export class ProposalEditComponent implements OnInit {
  10.  
  11. @Input() entity: ProposalModel;
  12. @Input() products: ProductModel[];
  13.  
  14. productSelectionData: any;
  15. selectedProducts: any;
  16.  
  17. constructor() { }
  18.  
  19. ngOnInit() {
  20.  
  21. // Load all products and items
  22. this.loadProductData();
  23. this.updateProductSelectionData();
  24. this.filterProductsSelected();
  25.  
  26. }
  27.  
  28. loadProductData() {
  29. this.productSelectionData = [];
  30.  
  31. this.products.forEach(product => {
  32. this.productSelectionData.push(
  33. { productTitle: product.productTitle, items: product.items })
  34. });
  35. console.log('Product Selection, after load: ', this.productSelectionData);
  36. debugger;
  37. }
  38.  
  39. updateProductSelectionData() {
  40. // Update Product Selection Object with previously selected data
  41.  
  42. // 1. Check if there is previously saved data
  43. if (this.entity.products !== undefined) {
  44. // 2. Update productSelectionData with values saved in entity object
  45. this.productSelectionData.forEach(product => {
  46. if (this.entity.products !== undefined) {
  47. this.entity.products.forEach(entityProduct => {
  48. if (product.productTitle === entityProduct.productTitle) {
  49. if (product.items !== undefined) {
  50. product.items.forEach(item => {
  51. if (entityProduct.items !== undefined) {
  52. entityProduct.items.forEach(entityItem => {
  53. if (item.code === entityItem.code) {
  54. item.selected = true;
  55. item.quantity = entityItem.quantity;
  56. item.discount = entityItem.discount;
  57. }
  58. });
  59. }
  60. });
  61. }
  62. }
  63. });
  64. }
  65. });
  66. console.log('Product Selection, after update: ', this.productSelectionData);
  67. debugger;
  68. }
  69. }
  70.  
  71. filterProductsSelected() {
  72. this.selectedProducts = [];
  73. this.productSelectionData.forEach(product => {
  74. this.selectedProducts.push(product)
  75. });
  76. this.selectedProducts.forEach(selectedProduct => {
  77. selectedProduct.items.forEach(item => {
  78. const itemIndex = selectedProduct.items.indexOf(item);
  79. if (item.selected === false) {
  80. selectedProduct.items.splice(itemIndex, 1);
  81. }
  82. if (item.selected === undefined) {
  83. selectedProduct.items.splice(itemIndex, 1);
  84. }
  85. });
  86. });
  87. console.log('Selected Products, after filter: ', this.selectedProducts);
  88. console.log('Product Selection, after filter: ', this.productSelectionData);
  89. debugger;
  90. }
  91.  
  92. }
Add Comment
Please, Sign In to add comment