Dropdown menu displaying data in a hierarchical structure within a single table

I am currently working on implementing a cascading dropdown feature where selecting a state will display the areas within that state. The issue I'm facing is that all the data is stored in the same table in the database.

Here is an overview of the database structure:

States:

{id: 2526, code: 's_abuja', label: 'Abuja', descr: '', category: 'state'}
{id: 2527, code: 's_anambra', label: 'Anambra', descr: '', category: 'state'}
{id: 2528, code: 's_enugu', label: 'Enugu', descr: '', category: 'state'}
{id: 2529, code: 's_akwa_ibom', label: 'Akwa Ibom', descr: '', category: 'state'}

Areas:

{id: 1377, code: 's_abia_1', label: 'Aba North',descr:'',category:'local_government_area'}
{id: 1378, code: 's_abia_2', label: 'Aba South', descr: '', category: 'local_government_area'}
{id: 1394, code: 's_abuja_1', label: 'Abaji', descr: '', category: 'local_government_area'}
{id: 1395, code: 's_abuja_2', label: 'Bwari', descr: '', category: 'local_government_area'}

In my Angular application, I've incorporated this logic into the select tag:

<select
    class="form-control"
    id="field_civilState"
    name="civilState"
    formControlName="civilState"
    (change)="onChange($event.target.value)"
>

.ts file

import { IDictionary } from '/model/dictionary.model';
    
civilStates: IDictionary[];
lgas: IDictionary[];

ngOnInit(){
   this.lgas = this.civilStates.filter(x => x.category === 1)[0].category;
}

onChange(dataValue) {
   this.lgas = this.civilStates.filter(x => x.category === dataValue)[0];
}

I would appreciate any assistance in resolving this challenge.

Answer №1

.ts

 public tempLga: any[] = [];

 updateSelection(dataValue): void { 
   this.tempLga = [];
   this.tempLga = this.lgas.filter(e => e.code.includes(dataValue));
 }

.html

       <select
            class="form-control"
            id="field_civilState"
            name="civilState"
            formControlName="civilState"
            (change)="updateSelection($event.target.value)"
          >

       <select class="form-control">
                <option [ngValue]="null">Please Choose..</option>
                <option *ngFor="let selection of tempLga" [value]="selection.id"> 
                  {{selection.label }}</option>
              </select>

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

Exploring Angular Scope within a Typescript-based javascript function

My current setup involves Typescript with Angular combined with Breezejs. class CounterController { count: number = 0; static $inject = ['$scope']; constructor($scope) { $scope.vm = this; } setCount14(): void { ...

Advantages of optimizing NodeJS/TypeScript application by bundling it with webpack

Currently, I am working on a Node/Express application and I am interested in incorporating the most recent technologies. This includes using TypeScript with node/Express along with webpack. I have a question: What advantages come with utilizing webpack t ...

determine the total number of months within a specific duration

I need help with calculating the number of months between two dates, inclusive of both extremes. I have attempted the following code: var duration : moment.Duration; const contractStartMoment = moment(contractStart); //contractStart=01.05.2021 const ...

When calling `mongoose.model()`, the second argument must either be a schema or a plain old JavaScript object (POJO)

Trying to launch my application but unsure which file is causing the issue. Can someone point me in the right direction? Here is a snippet of my app.module.ts file: import { Module } from '@nestjs/common'; import { ConfigModule } from '@nes ...

Exploring the world of Typescript TSX with the power of generic

Introducing JSX syntax support in Typescript opens up new possibilities. I have an expression that functions smoothly with traditional *.ts files, but encounters issues with *.tsx files: const f = <T1>(arg1: T1) => <T2>(arg2: T2) => { ...

Arranging Alphanumeric Characters in Angular in Ascending Order

I am trying to sort a list of characters first, followed by alphanumeric values. Here is what I have: [Austria , Germany , 123aed , 234eds] This is my current sorting attempt: obj.sort((a,b) => { if ( (isNaN(a.text) && isNaN(b.text)) || ...

Optimal method for importing libraries in Angular 2 with TypeScript

Currently experimenting with Angular2, but I find myself in need of using jQuery. After downloading the d.ts file, I referenced the definitions in each file using the following syntax: /// <reference path="../../../../typings/jquery/jquery.d.ts" /> ...

Result of Mongodb aggregation operation

I've created a Property controller : //Retrieve Properties By Type const getPropertiesByType = async (req: Request, res: Response) => { const { cities, type } = req.query; const citiesArr = typeof cities === 'string' ? cities.spli ...

Utilizing files that do not have the extension '.ts' or '.tsx' within the 'ts_library' as dependencies

My current challenge involves importing a JSON file from TypeScript while utilizing the resolveJsonModule flag in my tsconfig. The problem lies in how I can provide this JSON file to ts_library since it seems unable to locate the file. This issue extends t ...

Is there a way to determine if individuals in my contact list have installed and are using my app? Using Ionic 2

I need the following feature: When a user clicks on the contacts button, they should be able to view all contacts from their device who are currently using the app. Additionally, they should see an "invite" option for contacts who are not yet using the app ...

The property 'label' is not found in the 'string' type in Typescript

Below is the code snippet I am using: interface State { resourceGroup: QuickPickItem | string; } setEvent(state.resourceGroup?.label).catch(err => console.error(err)); When executing this code, I encountered the following error messa ...

What is the best way to access DirectivesModule from a component that utilizes specific directives within it?

Updated: After thorough consideration, it has come to light that the efficient solution involves importing SharedModule into the parent component of the child component utilizing the DropDownDirective for proper functionality. Delving into an Angular 4 ...

Why does TypeORM DataSource initialization keep defaulting my host to localhost instead of using my public IP address?

I'm facing a particular challenge that has me scratching my head. I've been attempting to establish a connection to a remote SQL database hosted on Google Cloud using a locally running instance of my Node application, but so far, it's been u ...

Tips for effectively setting up your Angular project for success

Recently, I started working on a simple project: https://stackblitz.com/edit/angular-rktmgc-ktjk3n?file=index.html The main code resides in: /index.html <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <div ...

Implementing Angular CDK for a dynamic drag-and-drop list featuring both parent and child elements

I'm currently working on setting up a drag and drop list within Angular using the Angular CDK library. My goal is to create a list that includes both parent and child elements, with the ability to drag and drop both parent items as well as their respe ...

Tips for enhancing a TypeScript interface for a React component in (Material-UI) by utilizing styled-components

I've been struggling to find a solution for this overload issue with no luck so far. My stack includes Typescript, Styled-components, and Material-UI. I am utilizing styled(MUIButton) to extend the default Button from MUI. While my props are being pas ...

The data from the Subscribe API call is gradually loading within the ngOnInit() function

When using Angular 8, I am experiencing slow data retrieval when making API calls in the ngOnInit() function. The issue arises when trying to pass this data as @Input from one component module to another - it initially comes through as undefined for a minu ...

What is the best way to retrieve a class property within an arrow function that is passed to a constructor?

On a React login page, I created a class for rendering authentication methods. The class includes a default render function that I need to customize. class AuthMethod { constructor(initializer: Pick<AuthMethod, 'id'> & Partial<A ...

Angular Reactive Forms: Deleting a Validator from a Control

I'm currently tackling a challenge with Angular reactive forms. Validators are dynamically added to my controls in my specific scenario. Here's how it's done: const myControl = myFormGroup.get('myControl'); if (myControl.validat ...

Scouring the Active Directory with AngularJS

I am still fairly new to working with Angular, and I have a web application that requires users to input names and locations. One common issue raised by users is the inability to search for people using active directory. Is there a way for me to implemen ...