Utilizing ng-options within an ng-repeat to filter out previously chosen options

Facing a simple problem and seeking a solution.

I am using ng-repeat to dynamically create select boxes with ng-options displaying the same values.

The ng-repeat is used to iterate through model options.

<div data-ng-repeat="condition in model.conditions">
    <label>{{condition}}</label>
    <select data-ng-model="selectedValue"
            ng-options="item.name for item in optionList">
    </select>
</div>

This represents the condition model:

$scope.model = 
    {
        conditions:
        [
            {id:1,name:"CONDITION_ONE"},
            {id:2,name:"CONDITION_TWO"}
        ]
    }

This corresponds to the list of options:

$scope.optionList = 
    [
        {id:1, name:"OPTION_ONE"},
        {id:2, name:"OPTION_TWO"},
        {id:3, name:"OPTION_Three"}
    ];

Image for illustration purposes only:

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

I aim to enable the removal of selected items from one select box when chosen, in order to prevent duplicates across select boxes.

Is there a way to achieve this functionality using angularJS?

Answer №1

Utilize the ng-repeat filter

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
<div data-ng-repeat="condition in model.conditions">
    <label>Remove Id {{condition.id}} from  List </label>
<select ng-model="selectedName" ng-options="item.name for item in filter(condition)">
</select>

</div></div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.optionList = 
    [
        {id:1, name:"OPTION_ONE"},
        {id:2, name:"OPTION_TWO"},
        {id:3, name:"OPTION_Three"}
    ];
    
    $scope.model = 
    {
        videos:
        [
            {id:1,name:"CONDITION_ONE"},
            {id:2,name:"CONDITION_TWO"}
        ],
        conditions : [
        {id:1, name:"OPTION_ONE"},
        {id:2, name:"OPTION_TWO"},
        {id:3, name:"OPTION_Three"}
    ]
    };
    
    $scope.filter = (item)=>{
   return $scope.optionList.filter(list => list.id !== item.id);
    }
});
</script>

</body>
</html>

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

Successful Mongoose query in Node.js, however the array remains empty even after using forEach loop. Issue with MongoDB integration

After performing a forEach inside an asynchronous method, I am trying to return an array of names. The issue is that despite the forEach working correctly, the array always ends up empty. On the website side, here is my request: function retrieveCharity( ...

What's the reason for the double invocation of afterSelectionChange() in ng-grid?

The response to this specific inquiry did not sit well with me. There seems to be something off about the code. As a newcomer to Angular, I saw an opportunity to enhance my learning by creating a Plunk to assist the original poster. You can find my Plunk ...

What is the reason for jQuery's inability to recognize ":not(#menu *)" in a selector?

I have created a Javascript-based navigation bar that is triggered by clicks instead of hover for better mobile usability. I have made sure to keep the HTML, CSS, and JavaScript code as simple as possible. To ensure that clicking anywhere outside the menu ...

Declaring a variable outside of a function or object

It's been a challenge for me to assign a value to the variable result as it always stays undefined. if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } ...

Leveraging jQuery to extract a key-value pair and assign it to a new property by

I'm struggling with extracting a value from a key-value pair and inserting it into the rel property of an anchor tag. Even though I try to split the code and place the value correctly, it doesn't seem to work as expected. Instead of applying the ...

How do I store the result of an Ajax request as a variable in a different function within a React component?

One of the challenges I'm facing involves making an ajax call that retrieves a list of movies, and then running another function with a separate ajax call to fetch the genre names. Since the first call only provides the genre IDs, I need to match each ...

Utilize Redux Toolkit to efficiently share actions across different slices of state

How can I efficiently share common actions across multiple redux state slices? For instance, let's say I have an updateField action that I want to use in various slices other than just the profile slice. Should I import it from an external file for r ...

Duplicate entries being saved in the database due to Ajax calls

I am currently utilizing Fullcalendar jQuery with PHP for managing events. I am making an AJAX call to add events, and while it works fine for the initial event entry after a page refresh, it starts creating duplicate events for subsequent entries. I am un ...

Ensuring the accuracy of forms using third-party verification services

While working on an Angular form validation using an external service, I encountered a cannot read property of undefined error. The component contains a simple form setup: this.myForm = this.fb.group({ username: ['', [this.validator.username] ...

Save pictures in MongoDB using GridFS or BSON format

As a newcomer to MongoDB, I am seeking advice on the best way to store images in the database. Gridfs and BSON seem to be the most common options, but I'm unsure about their respective pros and cons. The main difference I'm aware of is the 16MB s ...

Retrieving selected options from a jQuery mobile multiselect for database storage

I'm currently working on extracting values from a select list in a jQuery mobile application. Here's the markup for my select element: <div data-role="fieldcontain"> <label for="stuff" class="select">Stuff:</label ...

Which is the better option for selecting DOM elements in a Vuejs 3 application: using raw js or jquery?

 I am currently working on developing an application using Node.js and Vue.js 3. One of the features I have implemented is a sidebar that dynamically fetches links from a routes file and displays them. The sidebar consists of a component that organize ...

Ensure that the JSON object exists before saving it to local storage

[ { "id": 1, "title": "my First Item", "body": "" }, { "id": 2, "title": "my Second Item", "body": "" }, { "id": 3, "title": "my Third Item", "body": "" } ] Do ...

The canvas doesn't seem to be rotating even after executing the setTransform

Within my HTML5 canvas, I have been experimenting with drawing lines and rotating them around the center point. My goal is to reset the canvas's transformation each time I draw, followed by re-translating/rotating the canvas. However, I have encounter ...

Breaking down JavaScript arrays into smaller parts can be referred to

Our dataset consists of around 40,000 entries that failed to synchronize with an external system. The external system requires the data to be in the form of subarrays sorted by ID and created date ascending, taken from the main array itself. Each ID can ha ...

Utilizing ngblur and ngfocus to toggle visibility of dropdown options in Angular

Whenever I click off the list without selecting anything, the ngblur event isn't triggered when using select and option. I'm searching for a directive that can help me determine when the options in the select are shown or hidden. Any suggestions ...

Utilizing the power of functional programming with Javascript to perform mapping

In order to address a fundamental issue involving list indexes, I am seeking a functional solution. To illustrate this problem in the context of React and ramda, consider the following example. const R = require('ramda'); const array = ["foo", ...

Having trouble accessing the root route in production environment

After creating a basic Node application with 5 routes that serve different HTML documents, I encountered an issue. While all the routes function properly when running on localhost, in production my root route displays a 404 error page, indicating that the ...

What is the best way to streamline this using Javascript or jQuery?

Looking for a way to simplify the code for 4 vertical sliding panels? Check out the following script: $(".sliding-panels").click(function() { var n = $(this).attr("number"); var panels = $(".panel"); panels.not(".panel" + n).removeClass("pa ...

Nuxt: The meta title for dynamic head is not defined during server-side rendering

In my nuxt project, I am encountering an issue with the meta title and description. These values are coming from nuxt/content. The data is fetched asynchronously in the index and passed to a sub component using a getter. When generating the page, the met ...