import { Component, OnInit } from '@angular/core'; import { DynamicDialogRef } from 'primeng/api'; import { DynamicDialogConfig } from 'primeng/api'; import { TreeModule } from 'primeng/tree'; import { TreeNode } from 'primeng/api'; import { ProductsService } from '../../../services/products/products.service'; @Component({ selector: 'app-add-categories', templateUrl: './add-categories.component.html', styleUrls: ['./add-categories.component.scss'] }) export class AddCategoriesComponent implements OnInit { filesTree0: any = [{}]; companyId: number = 17; ProductCategories: any = [{}]; selectedNode: any = {} selectedCategories: any = []; productId: any; selectedFile: any =[]; constructor(public ref: DynamicDialogRef, public config: DynamicDialogConfig, private productService: ProductsService) { } ngOnInit() { this.productId = this.config.data.id; this.getProductCategories(); } // Get product categories getProductCategories() { this.productService.GetAllProductCategories(this.companyId) .subscribe(res => { this.ProductCategories[0].label = "All products"; this.ProductCategories[0].data = "All products"; this.ProductCategories[0].children = res; for (var a = 0; a < this.ProductCategories[0].children.length; a++) { this.ProductCategories[0].children[a].label = this.ProductCategories[0].children[a].ProductCategoryName; this.ProductCategories[0].children[a].children = this.ProductCategories[0].children[a].SubCategories; for (var b = 0; b < this.ProductCategories[0].children[a].children.length; b++) { this.ProductCategories[0].children[a].children[b].label = this.ProductCategories[0].children[a].children[b].ProductSubCategoryName } } }, error => console.log('Error', error)) } // Select node from tree selectNode(e) { this.selectedNode = e.node; console.log('Selected file', this.selectedFile) if (e.node.ProductCategoryName === undefined) { const selectedCategories = { ProductId: this.productId, ProductCategoryId: this.selectedNode.ProductCategoryId, ProductSubCategoryId: this.selectedNode.Id } this.selectedCategories.push(selectedCategories) } else { const selectedCategories = { ProductId: this.productId, ProductCategoryId: this.selectedNode.Id, ProductSubCategoryId: null } this.selectedCategories.push(selectedCategories) } } addCategory() { this.ref.close(this.selectedCategories); } }