Eliminating the selected option from the dropdown menu after it has been added with Angular

HTML Code

<!doctype html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8>
  <title>AngularJS Plunker</title>
  <link rel="stylesheet" href="style.css">
  <script>
    document.write("<base href=\"" + document.location + "\" />");
  </script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
  <script src="script.js"></script>
</head>

<body ng-controller="MainCtrl">
  <h1>Custom Dropdown Options</h1>
  <form name="addUser">
    Application:
    <select ng-model="filterAddUser.application" ng-init ="filterAddUser.application = 'STACK'" title="" ng-options="value as value for  (key , value) in applicationStatus">
    </select>
    Roles:
    <select ng-model="filterAddUser.role" title="" ng-init ="filterAddUser.role = 'R'" ng-options="role.value as role.param for role in roleStatus">
    </select>

    <button ng-click="addToCart()">AddItem</button>

    <div class="addCart">
      <ul ng-repeat="item in items">
        <li><b>Application:</b> {{item.application}}</li>
        <li><b>Role:</b> {{item.role}}</li>
        <li class="actionOptions">
          <button ng-click="toggleSelected($index)">removeItem</button>
        </li>
      </ul>
    </div>
  </form>
</body>
</html>

Javascript Code

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

  $scope.items = [];

  $scope.applicationStatus = {
    "TEST App": "TEST",
    "ABC App": "ABC",
    "TRY App": "TRY",
    "SIR App": "SIR",
    "LOPR App": "LOPR",
    "STACK App": "STACK"
  };
  $scope.roleStatus = [{
    "param": "Read",
    "value": "R"
  }, {
    "param": "Write",
    "value": "W"
  }, {
    "param": "Admin",
    "value": "A"
  }, {
    "param": "Super Approver",
    "value": "SA"
  }, {
    "param": "Supervisor",
    "value": "S"
  }];


  $scope.addToCart = function() {

    $scope.items.push({
      application: $scope.filterAddUser.application,
      role: $scope.filterAddUser.role
    });
    // Clear input fields after push
    $scope.filterAddUser['application'] = "";
    $scope.filterAddUser['role'] = "";
  }

  $scope.toggleSelected = function(index) {
    $scope.items.splice(index, 1);
  };

});

All that i am trying to do is when i add the application to the cart that application needs to be removed from the dropdown and also when i click on the remove item that needs to be pushed back to the cart i have included a plunker as well http://plnkr.co/edit/kSsetX?p=preview need help on the same.

Answer №1

Check out the updated version of your plunkr here: http://plnkr.co/edit/QQobh7Jx76r7lDzw7TzV

       var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.items = [];
  var deletedEntries = [];

  $scope.entryStatus = {
    "TEST Entry": "TEST",
    "ABC Entry": "ABC",
    "TRY Entry": "TRY",
    "SIR Entry": "SIR",
    "LOPR Entry": "LOPR",
    "STACK Entry": "STACK"
  };

  $scope.roleStatus = [{
    "param": "Read",
    "value": "R"
  }, {
    "param": "Write",
    "value": "W"
  }, {
    "param": "Admin",
    "value": "A"
  }, {
    "param": "Super Approver",
    "value": "SA"
  }, {
    "param": "Supervisor",
    "value": "S"
  }];

  $scope.filterAddEntry = {
    entry: $scope.entryStatus[0],
    role: $scope.roleStatus[0]
  };


  $scope.addToCart = function() {
    deletedEntries.push([
      $scope.filterAddEntry.entry,  $scope.entryStatus[$scope.filterAddEntry.entry]
    ]);

    delete $scope.entryStatus[$scope.filterAddEntry.entry];

    $scope.items.push({
      entry: $scope.filterAddEntry.entry,
      role: $scope.filterAddEntry.role
    });

    // Clear input fields after adding
    $scope.filterAddEntry['entry'] = $scope.entryStatus[0];
    $scope.filterAddEntry['role'] = $scope.roleStatus[0];

  }

  $scope.toggleSelected = function(index) {
    var addEntry = deletedEntries.filter(function(deletedEnt){
      return deletedEnt[0] === $scope.items[index].entry;
    })[0];

    $scope.entryStatus[addEntry[0]] = addEntry[1];  
    console.log($scope.entryStatus);
    $scope.items.splice(index, 1);
  };

});

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

Could you explain the functionality of the hide button in AngularJS?

Is there a way to conceal my navigation bar with the click of a button? I need two buttons - one to hide and another to show. When I click on the hide button, the navigation bar should disappear, and vice versa. ...

Error: guild is not defined | discord.js

Having trouble with a ReferenceError that says guild is not defined? I recently encountered a similar issue with members but managed to fix it by adding a constant. As someone new to javascript and node.js, I could use some assistance. I've even tried ...

Displaying information in form using ajax within the laravel framework

I am currently utilizing ajax to retrieve data from a database. While I am able to successfully retrieve the data on the backend, I am facing difficulties displaying it in the input field below. I have tried writing some code, but it seems that the JavaScr ...

Is it impossible to modify an enumerable attribute within a JSON.stringify replacer function?

I'm having some trouble trying to serialize non-enumerable properties within the replacer function. Can someone point out what might be going wrong in this code snippet? Any help would be greatly appreciated. var obj = {x:1,y:2}; Object.definePrope ...

Implementing a JSON query with an onkeyup event listener

My main objective with this code is to trigger a json query request by using a textfield to specify the location. It's essentially a quick search feature that searches for specific content on a different website. For example, if I input www.calsolum/ ...

Angular directive for automatically selecting a <select> value when there is only one option available in ngOptions

How can I create a directive that automatically preselects an option if only one item is available in the ngOptions scope? Currently, my code looks like this: <select id="provider" name="provider" class="form-control" ng-model="foo.provider" ...

Iterate over various selection lists and tally the frequency of each option being chosen

I am currently working on a website that requires multiple instances of the same select-option to be created. I want to keep track of how many times each option is selected, and save that count as a variable to be used in a PHP email function. For instanc ...

What are some strategies for retrying an Apollo Client query after a failure?

When working with Next.js server-side rendering (SSR), I often encounter a query failure on the first try due to the backend not being fully ready. In such cases, I would like to retry the fetch using ApolloClient. const PREVIEW = { query: MY_PREVIEW_ ...

Verify if the nested JSON object includes a specific key

Currently, I am grappling with a dilemma on how to determine if within a deeply nested JSON object, featuring numerous unknown arrays and properties, lies a specific property that goes by the name "isInvalid". My objective is to identify this property and, ...

Why is the updated index.html not loading with the root request?

I'm currently working on an Angular app and facing an issue with the index.html file not being updated when changes are made. I have noticed that even after modifying the index.html file, requests to localhost:8000 do not reflect the updates. This pro ...

Angular data is being displayed and then disappears when $scope is utilized

Here is my EJS file: </head> <body data-ng-app="indexHomepage" data-ng-controller="indexController"> <div style="margin:100px;"> <h1>Express!</h1> <table data-ng-repeat="user in users"> <tr> ...

Can one retrieve a catalog of Windows Updates that have been installed by utilizing node.js?

Currently, I am working on a JavaScript/Node.js project where I am seeking to retrieve a comprehensive list of all installed windows updates, similar to how it is done using the C# WUAPI 2.0 Type Library. I have attempted utilizing WMI calls (specifically ...

Struggling to Personalize Kendo Calendar Month templates using Knockout Kendo JS Binding

I have made modifications to the Kendo Calendar Month Template Resource which can be found Here without utilizing knockout-kendo.js. The official Kendo Reference is available Here. The issue arises when I implement the following code in knockout-kendo.js ...

Issue with Codeigniter's AJAX functionality causing div reloading to not function as intended

I am currently facing an issue where I can display text directly from the database on a webpage. However, when I edit the text in the database, I want it to automatically reload and show the updated text without having to refresh the entire page. Unfortun ...

Tips for incorporating an HTML file using ng-include in double curly brace syntax {{ }}

Here is the code snippet I am working with: <div ng-repeat="pTabs in child.productTabs" ng-click="toggleProductTab($index)" ng-if="productTabIsActive(pTabs, $index)"> <div ng-repeat="specs in pTab.additionalSpecs"> <p>{{spec ...

When using Selenium to locate an element, it appears to be returning an unexpected empty object

This question has been bothering me for quite some time. I am currently using Selenium in conjunction with Python to find an element within a webpage. The specific element I am targeting is as follows: <a id="topmenu_adhocQtraditional_Reports" ...

Having trouble with a Reactjs Facebook login library - update the componentClicked function to be async

Currently, I am working on incorporating Facebook login into my React application using Redux. Within my loginUser.js file, the "FacebookLogIn" component appears as follows: <FacebookLogin appId="375026166397978" autoLoad={true} fields="name, ...

Unclear value of button when being passed

In my Welcome.html file, I am attempting to send the value of a button to a function that simply logs that value. This function is located in a functions class that has been imported into my welcome.ts file. <ion-content padding id="page1"> <h1 ...

Even after assigning the class "img-fluid" to my image, it still fails to properly adjust to the screen size

I added the class "img-fluid" to my image in Bootstrap, but it's not fitting on my webpage. What should I do? <img src="images\406201.jpg" class="img-fluid" alt="..."> <div style="margin-top: 0p ...

Can JavaScript be used to determine if any code has been executed from the browser's console?

I'm currently developing a JavaScript game and I want to prevent cheating. Is there a way for me to track and record commands entered in the JavaScript console? Here's a basic outline of what I have in mind: consoleCommands = ""; window.console ...