Create multiple instances of a component in a dropdown menu using different datasets in Angular 5

Outlined below is the structure of my drop-down list:

Companies > Depots

Each company has multiple depots. I have developed a component for companies, and upon clicking on a company (menu item), an HTTP request is made to bring all companies which are then looped through in the company component. The same process is followed for depots (menu item inside a company) where a list of depots is retrieved and displayed within the depot component.

However, I am facing an issue. When I retrieve the list of depots upon clicking on depots (menu item) and loop through them, all depots of all companies show the same list. This means the depot list of all depot components available on the page are being bound together.

I simply want to display the list of depots specific to that particular company.

Any assistance on this matter would be greatly appreciated.

The code snippet is as follows:

The Hierarchy is => Company->Depot

<a href="javascript:void(0)" (click)="onClickCompanies()">company</a>
<!-- code of company component starts here -->
<ul>
<li *ngFor="let company of companies" class="childul levels">
    <a href="javascript:void(0)" >{{company.CompanyName}}</a>   
    <ul class="nested-menu-items parentsul">
        <li class="childul levels">
            <a href="javascript:void(0)" (click)="onClickDepot(company.CompanyID)">Depots</a>
<!-- code of depot component starts here -->
            <ul class="nested-menu-items parentsul">
                <li class="childul levels" *ngFor="let companyDepot of companyDepots">
                    <a href="javascript:void(0)">{{companyDepot.DepotName}}</a>
                </li>
            </ul>
<!-- code of depot component ends here -->
        </li>
    </ul>
</li></ul><!-- code of company component ends here -->

This is the application view :

The image shows the outcome after clicking on the depot of New Company 2, which also gets appended to New Company 1 and subsequently all others too.

Answer №1

Every business should have a list of locations where they operate from! The structure for companies should look something like this:

interface company{
  CompanyID:number;
  CompanyName:string;
  depots:depots[];
}

interface depots{
  DepotName:string;
}

companies=company[];

onClickDepot(id)
{
  let depotsById = getDepots(id);//this function retrieves the depots
  let company = this.companies.find((x)=>x.CompanyID==id);
  company.depots=depotsById;
}

in html:
<!-- code of company component starts here -->
<ul>
<a href="javascript:void(0)" (click)="onClickCompanies()">company</a>

<li *ngFor="let company of companies" class="childul levels">
    <a href="javascript:void(0)" >{{company.CompanyName}}</a>   
    <ul class="nested-menu-items parentsul">
        <li class="childul levels">
            <a href="javascript:void(0)" (click)="onClickDepot(company.CompanyID)">Depots</a>
<!-- code of depot component starts here -->
            <ul class="nested-menu-items parentsul">
                <li class="childul levels" *ngFor="let companyDepot of company.depots ">
                    <a href="javascript:void(0)">{{companyDepot.DepotName}}</a>
                </li>
            </ul>
<!-- code of depot component ends here -->
        </li>
    </ul>
</li></ul><!-- code of company component ends here -->

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Typescript in React is throwing an error that says you cannot destructure the property 'colored' from the 'boxShadows' object because it is undefined

After downloading the material dashboard react theme from an open source GitHub project, I tried to convert the project into Typescript (React + Typescript). However, I encountered the following error (See Attached Image) https://i.stack.imgur.com/YZKpK.p ...

experimenting with the checked attribute on a radio button with jasmine testing

Currently using Angular 8 with Jasmine testing. I have a simple loop of radio buttons and want to test a function that sets the checked attribute on the (x)th radio button within the loop based on this.startingCarType. I am encountering false and null tes ...

How can I determine the data type of an Array element contained within an Interface member?

Is there a way to extract the type of key3 in MyInterface2 and use it in key3Value, similar to key2Value? interface MyInterface { key1: { key2: string } } const key2Value: MyInterface['key1']['key2'] = 'Hi' / ...

Definition for the type react-navigation-v6 <Stack.Group>

I'm having trouble figuring out the proper type definition for a Stack group that includes screens (refer to TestStack.Group and the nested TestStack.Screen). The navigation stack: const TestStack = createNativeStackNavigator<TestStackParamList> ...

Angular 4: Utilizing reactive forms for dynamic addition and removal of elements in a string array

I am looking for a way to modify a reactive form so that it can add and delete fields to a string array dynamically. Currently, I am using a FormArray but it adds the new items as objects rather than just simple strings in the array. Here is an example of ...

What is the equivalent of getElementById in .ts when working with tags in .js?

Looking to incorporate Electron, Preload, Renderer with ReactJS and TypeScript into my project. <index.html> <body> <div id="root" /> <script src='./renderer.js'/> </body> <index.ts> const root = Re ...

Angular log out function to automatically close pop-up windows

Within my application, there is a page where users can open a popup window. When the user clicks on logout, it should close the popup window. To achieve this, I have used a static variable to store the popup window reference in the Global.ts class. public ...

Is there a more effective method to return a response apart from using a redundant function?

function unnecessaryFunction(){ let details: SignInDetails = { user: user, account: account, company: company }; return details; } I am being told that the details value is unnecessary. Is there ...

Tips for correctly decorating constructors in TypeScript

When a class is wrapped with a decorator, the superclasses lose access to that classes' properties. But why does this happen? I've got some code that demonstrates the issue: First, a decorator is created which replaces the constructor of a cla ...

Transform the IO type to an array of Either in functional programming with the fp-ts

Looking for assistance with implementing this in fp-ts. Can someone provide guidance? const $ = cheerio.load('some text'); const tests = $('table tr').get() .map(row => $(row).find('a')) .map(link => link.attr(&apos ...

Is there a method to improve type inference in vscode?

Recently, I created a component with a click handler that looks like this: onClick={(e) => { e.stopPropagation(); }} It seems clear to me, but then the Typescript compiler complains that it's not a valid signature for onClick, which actually a ...

How can you selectively export a single function from a JavaScript file?

Within my project, I have two separate modules - one written in ts and the other in js. There is a utility within the js module that needs to be accessed by the ts module. The utility service.js looks like this: module.exports = { helloFriends: functi ...

Display and conceal table columns dynamically in Vue by utilizing the Vuetify data table functionality

Looking for an example: How to show hide columns of vuetify data table using v-select list I have created something similar, but I'm facing an issue where the table doesn't refresh when changing the header data: https://codepen.io/Meff1/pen/vY ...

Encountered an HttpErrorResponse while trying to access the API endpoint

I am encountering an issue when trying to update and insert data with a single post request. Below is my API code: Here is the service code: This section includes the function calling code: Additionally, this is the model being used The API C# model c ...

What is the rationale behind TypeScript's decision to permit omission of "this" in a method?

The TypeScript code below compiles without errors: class Something { name: string; constructor() { name = "test"; } } Although this code compiles successfully, it mistakenly assumes that the `name` variable exists. However, when co ...

What could be causing my TSC to constantly crash whenever I try to utilize MUI props?

Currently in the process of converting a JavaScript project using Next.js and Material UI to TypeScript. This is a snippet of code from one of my components. Whenever I include Props as an intersection type along with MUI's BoxProps, the TypeScript c ...

Issue found in the file assert.d.ts located in the node_modules directory: Expected '{' or ';' at line 3, character 68. Error code: TS1144

When attempting to start the angular application with ng serve, I encountered an error. Below are the project details: Angular CLI: 8.2.0 Node: 14.19.1 OS: darwin x64 Angular: 8.2.0 ... animations, cli, common, compiler, compiler-cli, core, forms ... platf ...

Set values to the inner property of the object

In my configuration file, I have set up nested properties as shown below export class Config { public msalConfig: { auth: { authority: string; clientId: string; validateAuthority: boolean; redirectUri: ...

Tips for effectively handling projects that call for varying versions of TypeScript within Visual Studio

In some cases, developers have had to downgrade their TypeScript version in order for it to work with a specific npm package version. Is it possible to do this with Visual Studio? I recently obtained a sample solution that utilized the angular2 npm packag ...

Using multiple MaterialUI components in a JavaScript page with React can pose challenges

Incorporating multiple MaterialUI Cards in my project, I encountered an issue where all the cards would expand or the select values would change simultaneously. Despite using unique key values and mapped components with distinct keys, the problem persisted ...