Tips for implementing a cascading dropdown feature in Angular 7 Reactive Formarray: Ensuring saved data loads correctly in the UI form

I recently found a helpful guide on stackoverflow related to creating cascading dropdowns in an Angular reactive FormArray, you can check it out here

After following the instructions, I managed to achieve my desired outcome. However, I now face a new challenge. I need assistance in dynamically loading the 'wheres[]' data for each row of the FormArray when retrieving saved information from the database for editing purposes. Although I've made progress, I am encountering issues with the second dropdown. The key lies in loading the 'wheres[]' values correctly based on the stored JSON data and iterating through them.

Here is some context: 'options: string - the name of the variable in DB where formarray is stored'

private retrieveSavedControls(options: string): FormArray{
console.log(classes);
this.profileForm= this.fb.group({
  optionGroups: this.fb.array([])
})
const control = <FormArray>this.profileForm.controls['optionGroups'];

this.units = JSON.parse(options).optionGroups;
console.log('parsed json: ',this.units);
let index =0;
this.units.forEach(unit => {

  control.push(this.fb.group({
    selectInput: ['', Validators.required],
    whereInput: [[], Validators.required]
  }));
  
  // This section should have loaded the list for each of the 2nd dropdown but seems to be failing.
  this.wheres[unit.selectInput] = this.getWhere().filter((item)=> item.selectid == unit.selectInput); 

   (<FormArray>this.profileForm.controls['optionGroups']).controls[index]
    .patchValue({ selectInput: unit.selectInput, whereInput: unit.whereInput});
  index++
})
console.log('control: ',control)
return control;

}

Answer №1

The problem turned out to be in my code where the list was unexpectedly empty. After addressing this issue, the selected values loaded correctly for each list.

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 add an html element to an existing html document

I'm experimenting with creating a dynamic HTML element using TypeScript to display on a webpage. In my .ts file, my code looks like this: const displayContent = document.getElementById("display-content") as HTMLOutputElement; var displayVariable = " ...

Angular release 6: A guide on truncating text by words rather than characters

I'm currently enhancing a truncate pipe in Angular that is supposed to cut off text after 35 words, but instead it's trimming down to 35 characters... Here is the HTML code: <p *ngIf="item.description.length > 0"><span class="body-1 ...

What are the Dependencies for an Angular 2 package.json file?

I am embarking on Angular2 development for the first time. Upon researching various examples online, I have noticed a discrepancy in the package.json file under the "dependencies" section. "@angular/common": "2.0.0-rc.1", "@angular/core": "2.0.0-rc.1", S ...

Ways to implement pointer event styling within a span element

Hi, I'm having trouble with styling and I can't seem to figure out how to resolve it. The style pointer-events: none doesn't seem to be working for me. Here is an example of my code: The style snippet: .noclick { cursor: default; ...

Error: Unable to access the 'visitStatement' property of an undefined object within Angular 2

Help! I encountered an error in my angular2 application. The error message says: TypeError: Cannot read property ‘visitStatement’ of undefined ...

Guide to executing various datasets as parameters for test cases in Cypress using Typescript

I am encountering an issue while trying to run a testcase with multiple data fixtures constructed using an object array in Cypress. The error message states: TS2345: Argument of type '(fixture: { name: string; sValue: string; eValue: string}) => vo ...

Encountered an issue in React and Typescript where the argument type is not compatible with the parameter type 'EventListenerOrEventListenerObject'

One challenge I am facing is integrating Typescript into my React project: componentDidMount() { document.addEventListener('mousemove', this.handleMouseMove); } private handleMouseMove = (e: React.MouseEvent<HTMLElement>) => { appS ...

Using HTTP POST to subscribe and implementing a try-catch block

When using Angular2's http.post, I encountered an issue where the headers were not sent to the CORS server. To address this, I attempted to create a loop that would retry the request until it succeeds. However, this resulted in an endless loop. How ca ...

Prevent redirection when submitting and show an error message instead

I implemented a login system where, upon entering the correct username and password, a token is stored in local storage. If there's an error with the credentials, an "Login Unsuccessful" message is displayed. Everything was functioning correctly until ...

Struggling to integrate D3.js with React using the useRef hook. Any suggestions on the proper approach?

I'm currently working on creating a line chart using d3.js and integrating it into React as a functional component with hooks. My approach involved utilizing useRef to initialize the elements as null and then setting them in the JSX. However, I encou ...

Error encountered while rendering content in an Angular template

I'm currently integrating ngx-carousel into my application. Interestingly, the carousel works perfectly when I manually input the data. However, when trying to fetch the data from the server, it fails to work as expected. Take a look at my code snip ...

Guide to starting a Tizen Web App project using Angular

Starting out: I have experience with Angular and am now looking to delve into Tizen for the first time. I want to create a Tizen Web Application using Angular (7.x.x) for Samsung TV. After installing Tizen Studio and its extensions, I've set up a st ...

Creating a service in AngularJS 1.8 with ES6 modules that acts as a bridge to a class based API interface

As I continue to enhance a codebase that originally consisted of a mix of different versions of AngularJs and some unstructured code utilizing various versions of a software API, I encounter an interesting quirk. It appears that this API is accessible thro ...

What is the best way to create a custom type guard for a type discriminator in TypeScript?

Suppose there are objects with a property called _type_ used to store runtime type information. interface Foo { _type_: '<foo>'; thing1: string; } interface Bar { _type_: '<bar>' thing2: number; } function helpme(i ...

Issue with Socket.io: Data not received by the last user who connected

I have developed a Node.js and Express application with real-time drawing functionality on canvas using socket.io version 1.7.2. Users are divided into separate socket rooms to enable multiple teams to draw independently. However, there is an issue where t ...

Accessing Github REST API with Next-auth token

Looking to enable user login and retrieve specific user data using the Github REST API, such as repositories and issues. After successfully implementing the login feature, I encountered an obstacle with the Github REST API that requires a token for endpoi ...

The error occurred in Angular with the [dateclass] class specifically within the mat-calendar

I'm currently working on creating a calendar that allows for multiple date selections. I came across a helpful resource on StackBlitz Here is the code snippet for my AppComponent: isSelected = (event: any) => { const date ...

The reason behind encountering the "UNMET PEER DEPENDENCY" error within Angular 2

Is there a way to properly set up angular2-flash-messages in my angular-src directory instead of the root? I attempted to use npm install angular2-flash-messages, but encountered the following error: ├── UNMET PEER DEPENDENCY @angular/<a href="/c ...

Uncovered requirement for the selectSignal in an isolated module

While trying to utilize selecctSignal in a standalone component, I encountered a runtime error: ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(Standalone[AllTablesHeadComponent])[signal -> signal -> signal -> signal]: N ...

Tips for maintaining knowledge after redirecting to a new page

Building an app using Ionic 4 where I need to display vouchers from a database as images. Each image should act as a link to a details page showing more information about that specific voucher. However, I am struggling to figure out how to keep track of th ...