Deactivate specific choices from a dynamically generated dropdown menu in Angular 8

I am working on a dynamic dropdown feature with multiple fields. https://i.sstatic.net/28iQJ.png

  • By pressing the + button, a new row is generated. Users can add any number of rows.
  • My challenge is to prevent users from selecting previously chosen values in the dropdown list.

I have maintained a list of selected items and my complete field list as shown below.

SelectedFieldList =["ProductCode","Name"];
FieldList=["ProductCode","Name","Code","Image","Category", etc..]

To disable the selected options, I attempted using the following code snippet:

 <select formControlName="FieldName">
                        <ng-container *ngFor="let FieldItem of FieldList">
                          <option *ngIf="!SelectedFieldList.includes(FieldItem)"  [value]="FieldItem">{{FieldItem}}</option>
                          <option [disabled]="true" *ngIf="SelectedFieldList.includes(FieldItem)"  [value]="FieldItem">{{FieldItem}}</option>
                        </ng-container>
                      </select>

While this solution works within the dropdown, it encounters an issue upon clicking the next button. The disabled item gets rearranged in the dropdowns, impacting the selection order.

In the screenshot above, "Product number" was initially selected. After clicking the + button, it correctly disables in the next dropdown but alters the value to "Product code" in the first dropdown (although "Product number" remains disabled).

https://i.sstatic.net/Qp6B2.png

If you have any suggestions or solutions, please share them. Thank you in advance!

Answer №1

Have you considered binding the condition to disable the input?

 <ng-container *ngFor="let FieldItem of FieldList">
    <option [disabled]="SelectedFieldList.includes(FieldItem)"  [value]="FieldItem">{{FieldItem}}</option>
 </ng-container>

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

Struggling with testing the checkbox when it changes inside the CardHeader Avatar={} component

I've recently implemented a feature similar to the example showcased on MaterialUI's TransferList. However, I'm encountering difficulties accessing a checkbox within the avatar={}. The project utilizes Jest and Enzyme for testing purposes. T ...

I'm eager to showcase live, incoming data on the chart. However, I'm feeling a bit lost on how to proceed. Can you help

I am currently utilizing the line chart feature of ng2-charts in my Angular project. However, I am unsure how to create a graph with real-time data. Here is an example of some sample data being used for the line chart: lineChartData: ChartDataSets[] = [ { ...

Ways to address memory leakage issues in Angular

Is there a way to manually optimize the Garbage Collector by forcefully pushing variables into it? For example, if we have a root or global level variable in an Angular component that is no longer needed when switching to another page, how can we ensure it ...

Problem with custom anchor component in React that needs to perform an action before being clicked

React Component (Custom Anchor) import React from 'react'; class Anchor extends React.Component { onClick = (event) => { // ----------------------------- // send Google Analytics request ...

``There seems to be an issue with retrieving and displaying data in Ionic2 when using nav

I am facing an issue with displaying the data received from NavParams. I have used console.log() to confirm that I am getting the correct data, but for some reason, I am unable to display it on the new page. I suspect that there might be an error in how I ...

Struggling to containerize an Angular application

Struggling to deploy my Angular application as a docker image on Azure, I have hit a roadblock. Following the steps outlined in a video tutorial at https://learn.microsoft.com/en-us/azure/developer/javascript/tutorial-vscode-docker-node-01?tabs=bash did no ...

Interpret information in Angular 2 using Typescript

Just starting with Angular (IONIC) and need help. How can I extract the userId or id from this code? his.response = data. //Looking for guidance on accessing Json keys Response : { "userId": 1, "id": 1, "title": "sunt aut facere repellat providen ...

Steps for reverting the version of angular cli from 1.0.1 to 1.0.0

Hey there, I'm struggling to deploy my app due to the version of angular-cli. After checking, I found that my angular-cli version is: @angular/common: 2.4.10 @angular/compiler: 2.4.10 @angular/core: 2.4.10 @angular/forms: 2.4.10 @angular/http: 2.4.1 ...

Sharing information between components in Angular 2 that are not directly related as parent-child relationships

Hey there, I'm just getting started with Angular 2 and ran into a bit of a roadblock. In my homepage component, I have a ul element where I display job descriptions fetched from a Firebase API call. The data is stored in an array called "jobs" and dis ...

In the past, my code would run smoothly without any issues, but now I am encountering a runtime error even though the code comp

Recently, I started learning TypeScript and encountered an issue while working with Classes. My code was functioning properly before but now it's displaying a runtime error. ...

Is there a way to attach a ref to a Box component in material-ui using Typescript and React?

What is the best way to attach a ref to a material-ui Box component in React, while using TypeScript? It's crucial for enabling animation libraries such as GreenSock / GSAP to animate elements. According to material-ui documentation, using the itemRef ...

Creating versatile list components that can accommodate various types of list items

As part of my project using Next.js, typescript, and type-graphql, I found myself creating Table components. These components are meant to display custom object types as rows within a table. While each piece of data has its own unique structure, they all ...

The name "Identifier" has already been declared before

I am currently working on a social network project to enhance my skills in nodejs and reactjs. While debugging the backend code for /signin using Postman, I encountered an error that prevents me from launching the node server. The error message displayed i ...

Error occurring with the gulp task runner

Encountering an error while using the task runner for my "gulp" file. My project involves Angular 2 and Asp.net Core. The "typings" folder is set up with a "typings.json" file that contains the following: { "resolution": "main", "tree": { "src": ...

Controlling CSS Styles in Angular using TypeScript

Currently, I am working on an Angular project that involves dynamically populating a calendar. In addition to this, I have a range of dates and the task at hand is to modify the background for specific days within this date range. To manage dates effective ...

Securing Angular CLI Assets: Implementing canActivate for Protection

I am facing an issue where anyone can access my website's assets using URLs like http://localhost:4200/assets/filename.pdf, even when the user is not logged in. How can I secure these assets by implementing a canActivate guard? An ideal solution woul ...

Clearing transformation offset in Angular Material Drag and Drop

In my web application, I am working with a <div> element that has the CSS property position: absolute, placed inside a container with position: relative. The left and top properties of this element are linked to data X and Y in the backend component. ...

Angular Karma Error - MatDialogRef Provider Not Found

While testing with Angular Karma, I encountered the following error... NullInjectorError: StaticInjectorError(DynamicTestModule)[ManageProblemsComponent -> MatDialogRef]: StaticInjectorError(Platform: core)[ManageProblemsComponent -> MatDialogRef]: ...

Cross-Origin Resource Sharing Problem - Angular version 8 with NodeJS and ExpressJS

I've attempted various solutions from different sources and have a feeling that I may be overlooking something minor here. In my setup, I have an AngularJS 8 application running on Node 10 with ExpressJS. The specific issue I'm encountering rela ...

What is the importance of having a reference path for compiling an AngularJS 2 project using gulp-typescript?

I wanted to modify the Angular Tour Of Heros project to utilize gulp from this Github Repository. This is the gulpfile.json file I came up with: const gulp = require('gulp'); const del = require('del'); const typescript = require(&apo ...